How to copy the content of one word document into another word document?

后端 未结 3 826
一整个雨季
一整个雨季 2021-01-16 16:47

I have a word document with some Text and Images. I want to copy the content of the word document into another word document using C#.

Thanks.

3条回答
  •  日久生厌
    2021-01-16 17:21

    Try this. It should do the trick. This will copy all contents from first document to second document. Make sure both documents exists.

    using (WordprocessingDocument firstDocument = WordprocessingDocument.Open(@"E:\firstDocument.docx", false))
    using (WordprocessingDocument secondDocument = WordprocessingDocument.Create(@"E:\secondDocument.docx", WordprocessingDocumentType.Document))
    {
        foreach (var part in firstDocument.Parts)
        {
            secondDocument.AddPart(part.OpenXmlPart, part.RelationshipId);
        }
    }
    

提交回复
热议问题