Remove sentence from word doc using c#

poojasureshkumar
poojasureshkumar used Ask the Experts™
on
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,
Pooja
PHCP_Data8-29-2018--100029--AM.docx
Comment
Watch Question

Do more with

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

Commented:
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
Commented:
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: https://docs.microsoft.com/en-us/office/open-xml/open-xml-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: https://www.microsoft.com/en-in/download/details.aspx?id=30425

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;

namespaceGenerateDocumentFromTemplate
{
    class Program
    {
        static void Main(string[] args)
        {
            string destinationFile = Path.Combine(Environment.CurrentDirectory, "SampleDocument.docx");
            string sourceFile = Path.Combine(Environment.CurrentDirectory, "Sample.dotx");
            try
            {
                // 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
                    document.ChangeDocumentType(DocumentFormat.OpenXml.WordprocessingDocumentType.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
                    documentSettingPart1.Settings.Append(attachedTemplate1);

                    // Add an ExternalRelationShip of type AttachedTemplate.
                    // Specify the path of template and the relationship ID
                    documentSettingPart1.AddExternalRelationship("http://schemas.openxmlformats.org/officeDocument/2006/relationships/attachedTemplate", new Uri(sourceFile, UriKind.Absolute), "relationId1");

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

Open in new window


Reference Article: https://blogs.msdn.microsoft.com/vsod/2012/02/17/how-to-create-a-document-from-a-template-dotx-dotm-and-attach-to-it-using-open-xml-sdk/

Let me know if you have any questions/concerns.

Regards,
Chinmay.

Author

Commented:
thanks
Chinmay PatelChief Technology Ninja
Distinguished Expert 2018

Commented:
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