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.
- Document document = new Document();
- document.LoadFromFile(@"SpireDocTest.docx");
- Paragraph heading1 = document.Sections[0].Paragraphs[0];
- Paragraph para1 = document.Sections[0].Paragraphs[1];
- heading1.AppendText(" - Modified by Nitesh");
- para1.AppendText(" - Modified Text and added these words");
- document.SaveToFile("SpireDocTestModified.docx", FileFormat.Docx2013);

NOTE:
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,
- Document document = new Document();
- document.LoadFromFile(@"SpireDocTest.docx");
- document.Replace("Lorem Ipsum", "Lorem Ipsum by Nitesh", true, true);
- 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?

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,
- Document document = new Document();
- document.LoadFromFile(@"SpireDocTest.docx");
- //Perform a ny kind of changes to Word document
- document.SaveToFile("SpireDocTest.docx", FileFormat.PDF);
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.

madhusmita pandaPosted Mar 16, 2018, 4:01 AM
How do i remove the evaluation warning that comes in the created file!
Humayun Kabir MamunPosted May 5, 2016, 1:54 AM
Nice...
Mohammed IbrahimPosted May 4, 2016, 11:57 AM
nice
Kuppurasu NagarajPosted May 3, 2016, 9:47 PM
Nice Sharing..
Muhammad Aqib ShehzadPosted May 3, 2016, 11:37 AM
nice share
Debasis SahaPosted May 3, 2016, 9:32 AM
Good One..
Vignesh ManiPosted May 3, 2016, 8:39 AM
nice
Bhuvanesh MohankumarPosted May 3, 2016, 8:10 AM
Good one
Amit DavePosted May 3, 2016, 6:51 AM
Nice one!