YASR - Another Search and Replace Question

Environment: asp.net c # openxml

Okay, so I read a ton of fragments and tried to recreate the wheel, but I hope someone can help me get to my resolution faster. I have several documents that I need to combine together ... check ... I can do this with openxml sdk. Birds are singing, the sun is still shining. Now that I have the document as I want it, I need to look for and replace the controls with text and / or content.

I tried using my own text - {replace this}, but when I look at xml (rename docx to zip and view the file), {nowhere near the text. Therefore, I either need to know how to protect this within the program so that they do not diverge, or I need to find another way to search and replace.

I can search / replace if it is an xml file, but then I returned to the fact that I could not easily combine things.

The code below ... and, as I mentioned ... document merging works great ... just need to replace the material.

* Update * changed my replacement call to go after the tag instead of regex. I have the correct information now, but the .Replace call does not seem to want to work. The last four lines are intended to verify that I see the correct contents of the tag. I just want to replace this content now.

    protected void exeProcessTheDoc(object sender, EventArgs e)
    {
        string doc1 = Server.MapPath("~/Templates/doc1.docx");
        string doc2 = Server.MapPath("~/Templates/doc2.docx");
        string final_doc = Server.MapPath("~/Templates/extFinal.docx");

        File.Delete(final_doc);
        File.Copy(doc1, final_doc);

        using (WordprocessingDocument myDoc = WordprocessingDocument.Open(final_doc, true))
        {
            string altChunkId = "AltChunkId2";

            MainDocumentPart mainPart = myDoc.MainDocumentPart;
            AlternativeFormatImportPart chunk = mainPart.AddAlternativeFormatImportPart(
            AlternativeFormatImportPartType.WordprocessingML, altChunkId);
            using (FileStream fileStream = File.Open(doc2, FileMode.Open))
            chunk.FeedData(fileStream);
            AltChunk altChunk = new AltChunk();
            altChunk.Id = altChunkId;
            mainPart.Document.Body.InsertAfter(altChunk, mainPart.Document.Body.Elements<Paragraph>().Last());
            mainPart.Document.Save();
        }
        exeSearchReplace(final_doc);
    }

    public static void GetPropertyFromDocument(string document, string outdoc)
    {
        XmlDocument xmlProperties = new XmlDocument();

        using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(document, false))
        {
            ExtendedFilePropertiesPart appPart = wordDoc.ExtendedFilePropertiesPart;

            xmlProperties.Load(appPart.GetStream());
        }
        XmlNodeList chars = xmlProperties.GetElementsByTagName("Company");
        chars.Item(0).InnerText.Replace("{ClientName}", "Penn Inc.");

        StreamWriter sw;
        sw = File.CreateText(outdoc);
        sw.WriteLine(chars.Item(0).InnerText);
        sw.Close();
     }    
}

}

+3
source share
2 answers

, - "{replace me}" .docx, , XML, , <t>{replace</t><t> me</><t>}</t> - . , XML, , "{replace me}".

, , , . Word. , Word . , "isDirty" .

:

+1

All Articles