The operation of copying Word content to another Word introduced in this article is divided into two cases, one is to copy part of the original Word document content to another Word document, including formatting, pictures, hyperlinks, etc.; the other is to copy the document All content (except headers and footers) to another Word. It will be explained in detail below.
Tools used: Free Spire.Doc for .NET
(After installation, reference the Sprie.Doc.dll file to the project)
1. Copy part of the contentC#
//Create a new word document object doc1 and load the word document to be copied. Document doc1 = new Document(); doc1.LoadFromFile("sample.docx"); //Create a new word document object doc2 Document doc2 = new Document(); //Add a section to doc2, and copy the content and format of the first and second paragraphs of doc1 to doc2 Section s2 = doc2.AddSection(); Paragraph NewPara1 = (Paragraph)p1.Clone(); s2.Paragraphs.Add(NewPara1); Paragraph NewPara2 = (Paragraph)p2.Clone(); s2.Paragraphs.Add(NewPara2); //save and reopen the document doc2.SaveToFile("copy.docx", FileFormat.Docx2010); System.Diagnostics.Process.Start("copy.docx");
operation result:
2. Copy all content (except header and footer)
C#
//Create two word document objects, and load the source word document and the target word document to be copied Document sourceDoc = new Document("sample.docx"); Document destinationDoc = new Document("target.docx"); // Traverse all sections in the source word document and copy their contents to the target word document foreach (Section sec in sourceDoc.Sections) { foreach (DocumentObject obj in sec.Body.ChildObjects) { destinationDoc.Sections[0].Body.ChildObjects.Add(obj.Clone()); } } //Save and run the target word document destinationDoc.SaveToFile("target.docx"); System.Diagnostics.Process.Start("target.docx");
operation result:
The above content about copying a Word document to another Word document is transferred from the blog http://www.cnblogs.com/Yesi/p/5142418.html
Details can be found in the original source.
Thanks for reading.