C # Copy a word part or all of the contents of a document to another word Document
I recently liked to toss out office software-related stuff, and wanted to use. NET for many of the features provided by Office software, which would be even better if I could use it later to develop my own small application.
Pull away and get back to the chase. Copying the contents of a document is so common that it's easy to do this in Microsoft Word, just copy and paste. The main purpose of this article is to record how to use C # to replicate part or all of a Word document to another Word document, not much nonsense, to begin with.
Part I: Copy part of the content:
In my example, copying part of the text refers to copying a section of a Word document, including formatting, pictures, and hyperlinks, to another Word document.
Original document:
Step reference:
First step: Create a new Word Document Object Doc1 and load the Word document that you want to copy.
Document Doc1 = new Document ();d Oc1. LoadFromFile ("Sample.docx");
Step Two: create a new Word Document Object doc2.
Document DOC2 = new document ();
Step three: get the first section of the copied document Doc1 and the first to second paragraph (picture and title).
Section s = Doc1. Sections[0]; Paragraph p1 = s.paragraphs[0]; Paragraph P2 = s.paragraphs[1];
Fourth Step: Add a section to Doc2, and copy the contents and format of the 12th paragraph 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);
Fifth Step: save and reopen the document.
Doc2. SaveToFile ("Copy.docx", fileformat.docx2010); System.Diagnostics.Process.Start ("Copy.docx");
Target Document:
Part II: Copy all content
Copying all content refers to copying everything except headers and footer to another document.
Step reference:
Step One: create a new two Word document object and load the source word and target Word documents you want to copy.
Document SourceDoc = new Document ("Sample.docx");D ocument Destinationdoc = new Document ("Target.docx");
Step Two: iterate through all the 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 ());} }
Step three: save and restart the target Word document.
Destinationdoc.savetofile ("Target.docx"); System.Diagnostics.Process.Start ("Target.docx");
Separate headers and footer between Word documents can also be implemented, if necessary, you can leave a message below.
Original document with:
:
Conclusion:
The article is relatively simple to write, here I still use the E-iceblue company's free word control, because it is easy to use, and I also used more skilled. Next I'll look at other features of it and use C # to implement other features of the Office software.
C # copies part or all of a Word document to another Word document