Remove sentence from word doc using c#

poojasureshkumar used Ask the Experts™
Hi All,
Advance thanks
Am using c#, mvc
Have created a file using spire.doc. The problem is it creates with the text "Evaluation Warning: The document was created with Spire.Doc for .NET." always. I need to remove the text and save the file with it's original name and extension. Please help..

Kind regards,
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Shaun VermaakTechnical Specialist
Awarded 2017
Distinguished Expert 2018

You need to buy a license for that. Do you have one?

Alternatively, you can use the limited free one
Chief Technology Ninja
Distinguished Expert 2018
Hi Pooja,

With reference to your earlier question(and accepted answer) editing a word document is not replacing a string inside a file as modern office document i.e. DOCX/XSLX/PPTX and so on are nothing but Zip files having multiple child files. So if you replace a text inside a zip files by simple string swap you will end up corrupting the entire document.

Now, coming to your query, ideally you should use OpenXML SDK: It is the ultimate solution if you are willing to take some pain in learning its APIs. OpenXML SDK is completely free and you can use it in Server side document generation as well.

1.Create a copy of the template and open it for editing
2.Change the document type to WordProcessingDocumentType.Document
3.Create an AttachedTemplate object
4.Append the AttachedTemplateobject to the DocumentSettingsPart.Settings.
5.Add an External Relationship of type AttachedTemplate to the DocumentSettingsPart.Settings.
6.Save the document.

You can get the SDK from here:

Sample code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DocumentFormat.OpenXml.Wordprocessing;
using DocumentFormat.OpenXml.Packaging;
using System.IO;

    class Program
        static void Main(string[] args)
            string destinationFile = Path.Combine(Environment.CurrentDirectory, "SampleDocument.docx");
            string sourceFile = Path.Combine(Environment.CurrentDirectory, "Sample.dotx");
                // Create a copy of the template file and open the copy
                File.Copy(sourceFile, destinationFile, true);
                using (WordprocessingDocument document = WordprocessingDocument.Open(destinationFile, true))
                    // Change the document type to Document

                    // Get the MainPart of the document
                    MainDocumentPart mainPart = document.MainDocumentPart;

                    // Get the Document Settings Part
                    DocumentSettingsPart documentSettingPart1 = mainPart.DocumentSettingsPart;

                    // Create a new attachedTemplate and specify a relationship ID
                    AttachedTemplate attachedTemplate1 = new AttachedTemplate() { Id = "relationId1" };

                    // Append the attached template to the DocumentSettingsPart

                    // Add an ExternalRelationShip of type AttachedTemplate.
                    // Specify the path of template and the relationship ID
                    documentSettingPart1.AddExternalRelationship("", new Uri(sourceFile, UriKind.Absolute), "relationId1");

                    // Save the document
                   Console.WriteLine("Document generated at " + destinationFile);
            catch (Exception ex)
                Console.WriteLine("\nPress Enter to continue…");

Open in new window

Reference Article:

Let me know if you have any questions/concerns.



Chinmay PatelChief Technology Ninja
Distinguished Expert 2018

One more request, you should first try the code/example and if it works then and only you should accept the solution. I hope you already tried/tested the code I have posted. Please do let me know if you face any challenge.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial