aggicd
aggicd

Reputation: 737

C# Append lines to `*.docx` file produced from `*.dotx` file

I am using this code to produce a *.docx file from a *.dotx template file:

Create Document from template and replace words:

Dictionary<string, string> keyValues = new Dictionary<string, string>();
keyValues.Add("xxxxReplacethat1", "replaced1");
keyValues.Add("xxxxReplacethat2", "replaced2");

File.Copy(sourceFile, destinationFile, true);

using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(destinationFile, true))
{
    // Change the document's type here
    wordDoc.ChangeDocumentType(WordprocessingDocumentType.Document);
    string docText = null;

    using (StreamReader sr = new StreamReader(wordDoc.MainDocumentPart.GetStream()))
    {
        docText = sr.ReadToEnd();
    }

    foreach (KeyValuePair<string, string> item in keyValues)
    {
        Regex regexText = new Regex(item.Key);
        docText = regexText.Replace(docText, item.Value);
    }

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

In another function I am trying to append some lines to the *.docx file:

Append Lines:

foreach (var user in usersApproved)
                     File.AppendAllText(Server.MapPath(("..\\Files\\TFFiles\\" + tid + "\\" + file.SiteId + "\\" + file.Type + "\\")) + Path.GetFileName(file.Title), "Document Signed by: " + user.UserName + Environment.NewLine);

But I get this error:

Signature append failed:The process cannot access the file '(path)\destinationFile.docx' because it is being used by another process.

Tried also this solution : OpenAndAddTextToWordDocument but Im getting the same error

Upvotes: 0

Views: 232

Answers (1)

LocEngineer
LocEngineer

Reputation: 2917

This is how you would replace text using your Dictionary of Regexes and replacements:

Dictionary<string, string> keyValues = new Dictionary<string, string>();
keyValues.Add("xxxxReplacethat1", "replaced1");
keyValues.Add("xxxxReplacethat2", "replaced2");

using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(destinationFile, true))
{
    // Change the document's type here
    wordDoc.ChangeDocumentType(WordprocessingDocumentType.Document);

    foreach (Run rText in wordDoc.MainDocumentPart.Document.Descendants<Run>())
    {
        foreach (var text in rText.Elements<Text>())
        {
            foreach (KeyValuePair<string, string> item in keyValues)
            {
                Regex regexText = new Regex(item.Key);
                text.Text = regexText.Replace(text.Text, item.Value);
            }
        }
    }
    wordDoc.Save();
}

And this is how you would append text:

using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(destinationFile, true))
{
    var body = wordDoc.MainDocumentPart.Document.Body;

    var para = body.AppendChild(new Paragraph());
    var run = para.AppendChild(new Run());

    var txt = "Document Signed by: LocEngineer";
    run.AppendChild(new Text(txt));
    wordDoc.Save();
}

Upvotes: 1

Related Questions