- Document Creation
- Header Updation
- Footer Updation
- Image set up in Header and Footer
In this article, am going to explain you the ways how you need to work with Microsoft word using Spire.Doc. Yes, Spire.Doc is a wonderful library for the developers to perform document operations like Document Creation,Page Setup,Page Background,Setting Header and Footer,Changing Font etc. Here am not going bake you everything I listed above but you will have a clear session on Document Creation, Document update,Header and Footer creation,Header image upload. Let’s discuss this in detail
You can’t find any software without reports, Yes, Reports are the major source to understand what exactly the current trend is? Where were we lacking this month? How are we going to improve the trend? Reports will give one a better idea to align ourselves towards the organizational goal. Creating the reports in word/pdf should also be easy, in that case, e-iceblue provides us with a wonderful library Spire.Doc, using which one can operate the Microsoft word easily and effectively. Okay, Let me explain you the steps to create the document
As a first step, download the Spire.Doc from this link, After that refer the Spire.Doc DLLs into your project
To create the document using Spire.Doc ,We should remember the five important steps mentioned below
- Use Spire.Doc namespaces in code
- Creating Document instance
- Creating Section instance from the Document
- Creating Paragraph instance and Append the text
- Save the Document
First, use the Spire.Doc namespaces into the code as mentioned in the below snapshot.If we didn’t mention the namespace,we would not be able to use the Spire.Doc classes and methods.
Next,create the document as mentioned in the below snapshot, using the doc reference we are going to add the paragraphs,sections into the file and save it
Then create the sections from the document instance and assign into a Section instance
As we need to write some text into the document ,create the paragraph instance from the section instance and append the text into the paragraph using Append Text Method.
So far,we have created the Document,adding sections ,paragraph instances and AppendText into the paragraph.Finally, we need to save the document as mentioned in the below snapshot
In this section, you will see how to insert the header into the document. When you are creating the document it’s very important to define the header and footer.The Header will be used to display the name of the article or document with the image. Readers whoever reading the document gets the information from header/footer when he is moving from one page to other. Let’s see how we can insert the header using Spire.doc library. Here we need to remember five important steps to define the header into the document
- Creating Section
- Creating Header
- Creating Paragraph
- Appending Header text in Paragraph
As a first step,we need to create the section in the document
With the section instance, you need to create Header or Footer instance based on your requirement,In our case, we need to create the Header object from the section object called section. Refer the below-mentioned snapshot for details
Once you define the header , we need to update the Header text, for which we need to create the Paragraph object from the header object
After that , Append the header text into the paragraph object and assign into TextRange Object. Refer the below snapshot for the details. Text Range instance holds the HeaderText information.
As like we do formatting for the document text we can also do the formatting for the header text as well. You can change the Font name,Font size and Text colour of the Header Text.Refer the snapshot below you can find that Font name “Cambria” has been set for the header text and Font size as 11,Text colour as Orange. Likewise you can change the header texts according to your requirements
Friend,Do you want to change the border style,colour of the border? of course,you can also do that by assigning your required border style to the header paragraph. Please look at the below snapshot,the source code itself explain you everything.
In our case, I have updated my border style as ThickThinMediumGap and space as 0.05f.
We are done with the header update, In this section,we shall see Footer update. Footer configuration/update is almost same like header update. Only a few parameters will be different compared to the header configuration. As usual, you need to define the Footer from the section instance from which ,create the paragraph from the footer instance . Then you need to use the AppendText Method from the paragraph object and assign into TextRange instance.
Please find the above snapshot,paragraph instance has been created and appended the footer text
Image set up in Header and Footer
In the Header, most of the documents contain their company logo’s. Updating the image file in the header/footer is effective using Spring.Doc. You just need to select the AppendPicture method from the paragraph(header) instance. AppendPicture method will take only the image parameter,where you just need to supply the path of the image.Look at the below snapshot for more details
Please download the source code from this link
Spire.Doc is a .net library created for document operations like PageSet up,Header & Footer ,Page Background,Font. Using that developer can easily update the Microsoft word document and release the products effectively and efficiently.