C#: Copy Content from One Word Document to Another

2024-08-05 02:31:00 Written by  support iceblue
Rate this item
(0 votes)

Transferring content between Microsoft Word documents is a frequent need for many users. Whether you're consolidating information from multiple sources or reorganizing the structure of a document, being able to effectively copy and paste text, graphics, and formatting is crucial.

This article demonstrates how to copy content from one Word document to another using C# and Spire.Doc for .NET.

Install Spire.Doc for .NET

To begin with, you need to add the DLL files included in the Spire.Doc for.NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.Doc

Copy Specified Paragraphs from One Word Document to Another in C#

With Spire.Doc library for .NET, you can clone individual paragraphs using the Paragraph.Clone() method, and then add the cloned paragraphs to a different Word document using the ParagraphCollection.Add() method. This allows you to selectively copy and transfer content between documents.

To copy specified paragraphs from one Word document to another, follow these steps:

  • Create a Document object to load the source file.
  • Create another Document object to load the target file.
  • Get the specified paragraphs from the source file.
  • Clone these paragraphs using Paragraph.Clone() method.
  • Add the cloned paragraphs to the target file using ParagraphCollection.Add() method.
  • Save the target file to a different Word file.
  • C#
using Spire.Doc;
using Spire.Doc.Documents;

namespace CopyParagraphs
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create a Document object
            Document sourceDoc = new Document();

            // Load the source file
            sourceDoc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\source.docx");

            // Get the specified paragraphs from the source file
            Paragraph p1 = sourceDoc.Sections[0].Paragraphs[8];
            Paragraph p2 = sourceDoc.Sections[0].Paragraphs[9];

            // Create another Document object
            Document targetDoc = new Document();

            // Load the target file
            targetDoc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\target.docx");

            // Get the last section
            Section lastSection = targetDoc.LastSection;

            // Add the paragraphs from the soure file to the target file
            lastSection.Paragraphs.Add((Paragraph)p1.Clone());
            lastSection.Paragraphs.Add((Paragraph)p2.Clone());

            // Save the target file to a different Word file
            targetDoc.SaveToFile("CopyParagraphs.docx", FileFormat.Docx2019);

            // Dispose resources
            sourceDoc.Dispose();
            targetDoc.Dispose();
        }
    }
}

C#: Copy Content from One Word Document to Another

Copy a Section from One Word Document to Another in C#

A section in a Word document can contain not only paragraphs, but also other elements such as tables. To copy an entire section from one Word document to another, you need to iterate through all the child objects within the section and add them individually to a specified section in the target document.

The steps to copy a section between different Word documents are:

  • Create Document objects to load the source file and the target file, respectively.
  • Get the specified section from the source file.
  • Iterate through the child objects in the section, and clone these objects using DocumentObject.Clone() method.
  • Add the cloned child objects to a specified section of the target file using DocumentObjectCollection.Add() method.
  • Save the updated target file to a new file.
  • C#
using Spire.Doc;

namespace CopySection
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create a Document object
            Document sourceDoc = new Document();

            // Load the source file
            sourceDoc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\source.docx");

            // Get the specified section from the source file
            Section section = sourceDoc.Sections[0];

            // Create another Document object
            Document targetDoc = new Document();

            // Load the target file
            targetDoc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\target.docx");

            // Get the last section of the target file
            Section lastSection = targetDoc.LastSection;

            // Iterate through the child objects in the selected section
            foreach (DocumentObject obj in section.Body.ChildObjects)
            {
                // Add the child object to the last section of the target file
                lastSection.Body.ChildObjects.Add(obj.Clone());
            }

            // Save the target file to a different Word file
            targetDoc.SaveToFile("CopySection.docx", FileFormat.Docx2019);

            // Dispose resources
            sourceDoc.Dispose();
            targetDoc.Dispose();
        }
    }
}

Copy the Entire Document and Append it to Another in C#

To copy the full contents from one Word document into another, you can use the Document.InsertTextFromFile() method. This method appends the source document's contents to the target document, starting on a new page.

The steps to copy the entire document and append it to another are as follows:

  • Create a Document object.
  • Load a Word file (target file) from the given file path.
  • Insert content of a different Word document to the target file using Document.InsertTextFromFile() method.
  • Save the updated target file to a new Word document.
  • C#
using Spire.Doc;

namespace CopyEntireDocument
{
    class Program
    {
        static void Main(string[] args)
        {
            // Specify the path of the source document
            String sourceFile = "C:\\Users\\Administrator\\Desktop\\source.docx";

            // Create a Document object
            Document targetDoc = new Document();

            // Load the target file
            targetDoc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\target.docx");

            // Insert content of the source file to the target file
            targetDoc.InsertTextFromFile(sourceFile, FileFormat.Docx);

            // Save the target file to a different Word file
            targetDoc.SaveToFile("CopyEntireDocuemnt.docx", FileFormat.Docx2019);

            // Dispose resources
            targetDoc.Dispose();
        }
    }
}

Create a Copy of a Word Document in C#

Using Spire.Doc library for .NET, you can leverage the Document.Clone() method to easily duplicate a Word document.

Here are the steps to create a copy of a Word document:

  • Create a Document object.
  • Load a Word file from the given file path.
  • Clone the file using Document.Clone() method.
  • Save the cloned document to a new Word file.
  • C#
using Spire.Doc;

namespace CloneWordDocument
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create a new document object
            Document sourceDoc = new Document();

            // Load a Word file
            sourceDoc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\sample.docx");

            // Clone the document
            Document newDoc = sourceDoc.Clone();

            // Save the cloned document as a docx file
            newDoc.SaveToFile("Copy.docx", FileFormat.Docx);

            // Dispose resources
            sourceDoc.Dispose();
            newDoc.Dispose();
        }
    }
}

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Additional Info

  • tutorial_title:
Last modified on Monday, 05 August 2024 01:15