Duplicating Word document using OpenXml and C#

前端 未结 6 1793
野趣味
野趣味 2020-12-06 06:27

I am using Word and OpenXml to provide mail merge functionality in a C# ASP.NET web application:

1) A document is uploaded with a number of pre-defined strings for s

6条回答
  •  情深已故
    2020-12-06 06:51

    This piece of code should copy all parts from an existing document to a new one.

    using (var mainDoc = WordprocessingDocument.Open(@"c:\sourcedoc.docx", false))
    using (var resultDoc = WordprocessingDocument.Create(@"c:\newdoc.docx",
      WordprocessingDocumentType.Document))
    {
      // copy parts from source document to new document
      foreach (var part in mainDoc.Parts)
        resultDoc.AddPart(part.OpenXmlPart, part.RelationshipId);
      // perform replacements in resultDoc.MainDocumentPart
      // ...
    }
    

提交回复
热议问题