0
votes

I know there is alot of posts on it, BUT nothing worked for my problem: Im using OPENxml to create word document, and I am adding some ready files to the document during the creation. I want to change some text in the file that I am adding after the document is ready. So thats what I tried: First creating the document:

fileName = HttpContext.Current.Server.MapPath("~/reports/"+fileName+".docx");
using (var doc = WordprocessingDocument.Create(
    fileName, WordprocessingDocumentType.Document))
{
    ///add files and content inside the document
    addContentFile("template1part1", HttpContext.Current.Server.MapPath("~/templates/template1part1.docx"), mainPart);
}

this is how I am adding the files:

private static void addContentFile(string id,string path, MainDocumentPart mainPart){
    string altChunkId = id;
    AlternativeFormatImportPart chunk = mainPart.AddAlternativeFormatImportPart(
        AlternativeFormatImportPartType.WordprocessingML, altChunkId);
    using (FileStream fileStream = File.Open(path, FileMode.Open))
    {
        chunk.FeedData(fileStream);
        fileStream.Close();
    }
    AltChunk altChunk = new AltChunk();
    altChunk.Id = altChunkId;
    mainPart.Document.Body.Append(altChunk);
    mainPart.Document.Save();
}

And this is how I am trying to replace text AFTER I created the file (after i finished to use WordprocessingDocument)

First try:

using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(document, true))
{
    string docText = null;
    using (StreamReader sr = new StreamReader(wordDoc.MainDocumentPart.GetStream()))
        docText = sr.ReadToEnd();

    docText = new Regex(findText, RegexOptions.IgnoreCase).Replace(docText, replaceText);

    using (StreamWriter sw = new StreamWriter(wordDoc.MainDocumentPart.GetStream(FileMode.Create)))
        sw.Write(docText);
}

Second try:

using ( WordprocessingDocument doc =
    WordprocessingDocument.Open(@"yourpath\testdocument.docx", true))
    {
        var body = doc.MainDocumentPart.Document.Body;
        var paras = body.Elements<Paragraph>();

        foreach (var para in paras)
        {
            foreach (var run in para.Elements<Run>())
            {
                foreach (var text in run.Elements<Text>())
                {
                    if (text.Text.Contains("text-to-replace"))
                    {
                        text.Text = text.Text.Replace("text-to-replace", "replaced-text");
                    }
                }
            }
        }
    }
}

None of them worked, and I tried much more. Its worked for text that I am manually add to the document, but its now working for text that I am adding from the ready files. there is a way to do it?

1
Both of these methods don't work until you open and save the created document in MS Word. When you import wordML content using altChunk, it adds the document as is (as external content), in the first document. At this stage, the created document doesn't contain anything else than a reference to this external content (and no paragraphs, no runs ...) Then when you open the created document in MS Word and save it, MS Word will transform the added file into OpenXml content. Not sure what is the best approach for your need.Chris

1 Answers

0
votes

The way you are adding the files are using altchuncks. But you are trying to replace things as if you are modifying the resulting document's openxml.

When you merge documents as altchuncks you are basically adding them as embedded external files to the original document but not as openxml markup. Which means you cannot treat the additional attached documents as openxml documents.

If you want to achieve what you are trying, you have to merge the documents as explained in my answer here - https://stackoverflow.com/a/18352219/860243 which makes the resulting document a proper openxml document. Which allows you to modify it later as you wish.