Managing Word Documents With Spire.Doc Library Functions

A few days ago, one of my readers emailed me asking for help with some functions in the Spire.Doc library. In this post I explain various Spire.Doc Library Functions that eases the life of a developer.

For beginners, you can download the Spire.Doc library from here. The best part of the library is that it is an independent Word .NET component and doesn’t require Microsoft Word to be installed on the machine. Spire.Doc is present in Free as well as Paid edition. You can use the free version for reading/writing 100 paragraphs and 5 tables only. If your content is more than this, you will need to purchase the paid version.

Let’s get started with the library. We’ll use this Word file for our examples. A screenshot below,

Modify an Existing Word Document:

This library divides the Word document into paragraphs & sections. In the screenshot above, you see 4 headings and 4 paragraphs. When we read this document using the library, we get 8 paragraphs. The below code will add some text to the 1st heading & 1st paragraph of the code.

  1. Document document = new Document();  
  2. document.LoadFromFile(@"SpireDocTest.docx");  
  3. Paragraph heading1 = document.Sections[0].Paragraphs[0];  
  4. Paragraph para1 = document.Sections[0].Paragraphs[1];  
  5. heading1.AppendText(" - Modified by Nitesh");  
  6. para1.AppendText(" - Modified Text and added these words");  
  7. document.SaveToFile("SpireDocTestModified.docx", FileFormat.Docx2013);  
In the code above, we load the file using LoadFromFile() function on Line 2. On Line 4 & 5, we get references to the 1st heading and the 1st paragraphs. On Line 7 & 8, we append text to the existing paragraphs and finally save it on Line 10 to a new file. It is important to provide the format of the file being saved. There are various options provided by the library such as Doc, Docx, Doc2010, Doc2013, PDF etc. Below is a screenshot of the edited file. 

Spire.Doc Library Functions


You would see an Evaluation warning with the trial version. This warning will go as soon as you purchase the licensed version. If you want to purchase it, you can do so from here.

Find & Replace in Word Document:

Let’s move to another example where we will find & replace some text in the entire word document. The code goes as below,

  1. Document document = new Document();  
  2. document.LoadFromFile(@"SpireDocTest.docx");   
  3. document.Replace("Lorem Ipsum""Lorem Ipsum by Nitesh"truetrue);              
  4. document.SaveToFile("SpireDocTestModified.docx", FileFormat.Docx2013);   

In the above code, the magic line is Line No. 4. As you can see by calling just single Replace() function on the document, we can find and replace text in the document. this function takes 4 parameters as below,

  • String To Find
  • String To Replace
  • Do a “Case Sensitive” replace
  • Replace only “Whole Words”.

In above code, we are replacing only whole words that matches the text case. See the output below and you will notice that when doing a Replace on the word document, the style of the replaced text is already preserved by the code. Isn’t it awesome?

Spire.Doc Library Functions

Convert Word To PDF Document:

This code snippet will allow us to convert a Word file to PDF document with an easy function. The code goes as below,

  1. Document document = new Document();  
  2. document.LoadFromFile(@"SpireDocTest.docx");  
  3. //Perform a ny kind of changes to Word document  
  4. document.SaveToFile("SpireDocTest.docx", FileFormat.PDF);  
In the above code, when we are saving the document on Line No. 5, we pass the FileFormat as PDF and the library automatically saves the file into PDF format.

Spire.Doc Library Functions

Hope this post helps you analyze some more exciting features and different Spire.Doc Library Functions. If you have any questions about the product, post in comments.

Read more articles on .NET: