C # Copy Word (copy all content, partial content, header footer)

Source: Internet
Author: User

This example describes C # methods for copying Word documents. Depending on the needs, we will copy the Word document in three different situations to tell, including the following points:

    • Copy an entire Word document
    • Copy some of the contents of a document
    • Copy the header or footer tool using
    • Free Spire.doc for. NET 6.3
    • Visual Studio
      PS: In the following example, you need to add a reference class library Spire.Doc.dll. (DLLs can be obtained in the Bin folder under the installation path)
Example Demo 1. Copy all document contents

Source Document:

You need to copy to the following destination document:

"C #"

using Spire.Doc;namespace CopyWord_Doc{    class Program    {        static void Main(string[] args)        {            //新建Word文档1,用于加载源文档            Document sourceDoc = new Document("sample.docx");            //新建Word文档2,用于加载复制内容的目标文档            Document destinationDoc = new Document("target.docx");            //遍历源word文档中的所有section,并把内容复制到目标word文档            foreach (Section sec in sourceDoc.Sections)            {                foreach (DocumentObject obj in sec.Body.ChildObjects)                {                    destinationDoc.Sections[0].Body.ChildObjects.Add(obj.Clone());                }            }            //保存并打开复制的目标文档            destinationDoc.SaveToFile("target.docx", FileFormat.Docx2010);            System.Diagnostics.Process.Start("target.docx");        }    }}

Copy the result:

2. Copy part of content (formatted copy)

"C #"

  using spire.doc;using spire.doc.documents;namespace copypara_doc{class Program {static void Main        (string[] args)            {//Create Word Document 1, load source Document Doc1 = new documents (); Doc1.            LoadFromFile ("Sample.docx");            Creates a blank document as the target document for the copied content doc2 = new document (); Get Word Document 1 The first and second paragraphs of section s = Doc1.            Sections[0];            Paragraph p1 = s.paragraphs[0];            Paragraph P2 = s.paragraphs[1]; You add a section in Word Document 2, and you copy the contents of the first to second paragraph in document 1 to document 2 section s2 = doc2.            AddSection (); Paragraph NewPara1 = (Paragraph) p1.            Clone (); S2.            Paragraphs.add (NEWPARA1); Paragraph NewPara2 = (Paragraph) P2.            Clone (); S2.            Paragraphs.add (NEWPARA2); Save and open the copied document DOC2.            SaveToFile ("Copy.docx", fileformat.docx2010);        System.Diagnostics.Process.Start ("Copy.docx"); }    }}

Source Document:

Copy the result:

3. Copy the header or footer

Here is an example of copying a header
Header effects in the source document:

"C #"

using Spire.Doc;namespace CopyHeaderAndFooter_Doc{    class Program    {        static void Main(string[] args)        {            //新建Word文档1,并加载带页眉的源文档            Document doc1 = new Document();            doc1.LoadFromFile("test1.docx");            //获取文档1的页眉            HeaderFooter Header = doc1.Sections[0].HeadersFooters.Header;            //新建文档2,并加载目标文档            Document doc2 = new Document("test2.docx");            //遍历文档2中的所有Section            foreach (Section section in doc2.Sections)            {                foreach (DocumentObject obj in Header.ChildObjects)                {                    //将复制的页眉对象添加到section                    section.HeadersFooters.Header.ChildObjects.Add(obj.Clone());                }            }            //保存并打开文档            doc2.SaveToFile("copyHeader.docx", FileFormat.Docx2013);            System.Diagnostics.Process.Start("copyHeader.docx");        }    }}

Copy the result:

It is also possible to copy the footer.

The above is the full description of this example.
If you want to reprint, please indicate the source.

C # Copy Word (copy all content, partial content, header footer)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.