Remove sentence from word doc using c#

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
poojasureshkumarAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Shaun VermaakTechnical Specialist/DeveloperCommented:
You need to buy a license for that. Do you have one?

Alternatively, you can use the limited free one
0
Chinmay PatelChief Technical NinjaCommented:
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.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
poojasureshkumarAuthor Commented:
thanks
0
Chinmay PatelChief Technical NinjaCommented:
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.