Managing Word Documents With Spire.Doc Library Functions
Around a year ago, I wrote a post on How To Create a Word Document using Spire.Doc library. A few days back, one of my readers emailed me asking to help with some other functions in the library. Hence, this post is here where I explain various Spire.Doc Library Functions that eases the life of a developer.
For a quick recap, 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 –
Create a New Word Document:
This has already been covered here.
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);
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.
NOTE: You would see an Evaluation warning with the trial version. This warning will go as soon as you purchase the license version. If you want to purchase, 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);
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.
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 or contact me here. Cheers!