Knowledgebase (2330)
Children categories
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.
- Copy Specified Paragraphs from One Word Document to Another
- Copy a Section from One Word Document to Another
- Copy the Entire Document and Append it to Another
- Create a Copy of a Word Document
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();
}
}
}

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.
Developers can use PDF layer to set some content to be visible and others to be invisible in the same PDF file. It makes the PDF Layer widely be used to deal with related contents within the same PDF. Now developers can easily add page layers by using class PdfPageLayer offered by Spire.PDF. This article will focus on showing how to add layers to a PDF file in C# with the help of Spire.PDF.
Note: Before Start, please download the latest version of Spire.PDF and add Spire.PDF.dll in the bin folder as the reference of Visual Studio.
Here comes to the details:
Step 1: Create a new PDF document
PdfDocument pdfdoc = new PdfDocument();
Step 2: Add a new page to the PDF document.
PdfPageBase page = pdfdoc.Pages.Add();
Step 3: Add a layer named "red line" to the PDF page.
PdfLayer layer = doc.Layers.AddLayer("red line", PdfVisibility.On);
Step 4: Draw a red line to the added layer.
// Create a graphics context for drawing on the specified page's canvas using the created layer PdfCanvas pcA = layer.CreateGraphics(page.Canvas); // Draw a red line on the graphics context using a pen with thickness 2, starting from (100, 350) to (300, 350) pcA.DrawLine(new PdfPen(PdfBrushes.Red, 2), new PointF(100, 350), new PointF(300, 350));
Step 5: Use the same method above to add the other two layers to the PDF page.
layer = doc.Layers.AddLayer("blue line");
PdfCanvas pcB = layer.CreateGraphics(doc.Pages[0].Canvas);
pcB.DrawLine(new PdfPen(PdfBrushes.Blue, 2), new PointF(100, 400), new PointF(300, 400));
layer = doc.Layers.AddLayer("green line");
PdfCanvas pcC = layer.CreateGraphics(doc.Pages[0].Canvas);
pcC.DrawLine(new PdfPen(PdfBrushes.Green, 2), new PointF(100, 450), new PointF(300, 450));
Step 6: Save the document to file.
pdfdoc.SaveToFile("AddLayers.pdf", FileFormat.PDF);
Effective screenshot:

Full codes:
using Spire.Pdf;
using Spire.Pdf.Graphics;
using System.Drawing;
namespace AddLayer
{
class Program
{
static void Main(string[] args)
{
// Create a new PdfDocument object
PdfDocument doc = new PdfDocument();
// Load an existing PDF document from the specified file path
doc.LoadFromFile(@"..\..\..\..\..\..\Data\AddLayers.pdf");
// Get the first page of the loaded document
PdfPageBase page = doc.Pages[0];
// Create a new layer named "red line" with visibility set to "On"
PdfLayer layer = doc.Layers.AddLayer("red line", PdfVisibility.On);
// Create a graphics context for drawing on the specified page's canvas using the created layer
PdfCanvas pcA = layer.CreateGraphics(page.Canvas);
// Draw a red line on the graphics context using a pen with thickness 2, starting from (100, 350) to (300, 350)
pcA.DrawLine(new PdfPen(PdfBrushes.Red, 2), new PointF(100, 350), new PointF(300, 350));
// Create a new layer named "blue line" without specifying visibility (default is "Off")
layer = doc.Layers.AddLayer("blue line");
// Create a graphics context for drawing on the first page's canvas using the newly created layer
PdfCanvas pcB = layer.CreateGraphics(doc.Pages[0].Canvas);
// Draw a blue line on the graphics context using a pen with thickness 2, starting from (100, 400) to (300, 400)
pcB.DrawLine(new PdfPen(PdfBrushes.Blue, 2), new PointF(100, 400), new PointF(300, 400));
// Create a new layer named "green line" without specifying visibility (default is "Off")
layer = doc.Layers.AddLayer("green line");
// Create a graphics context for drawing on the first page's canvas using the newly created layer
PdfCanvas pcC = layer.CreateGraphics(doc.Pages[0].Canvas);
// Draw a green line on the graphics context using a pen with thickness 2, starting from (100, 450) to (300, 450)
pcC.DrawLine(new PdfPen(PdfBrushes.Green, 2), new PointF(100, 450), new PointF(300, 450));
// Specify the output file name for the modified PDF
string output = "AddLayers.pdf";
// Save the modified PDF document to the specified output file
doc.SaveToFile(output);
}
}
}
Spire.PDFViewer is a powerful and independent ASP.NET library, by which users can easily achieve functions such as load and view pdf files on website, switch to target page, fit page, page down/up, zoom in/out pdf files etc.
Then how to use Spire.PDFViewer for ASP.NET? This article will introduce the usage of Spire.PDFViewer for ASP.NET to you.
Before start, please download Spire.PDFViewer for ASP.NET and install it on your system.
Detail steps overview:
Step 1: Create a new ASP.NET Empty Web Application in Visual Studio.

Add a new web Form to Test1.


Then add the .dll files from the bin folder as the references of Test1.

Now the three .dll files have been added into the References.

Step 2: Add the PDFViewer control and the PDFDocumentViewer control into toolbox.
First, right-click toolbox, select add tab to add a new tab named SpirePDFViewer.

Second, add the PDFViewer control and the PDFDocumentViewer control into SpirePDFViewer.


Now all of the controls have been added into SpirePDFViewer.

Step 3: Right-click Default.aspx, select view designer, and then drag the PDFViewer control from toolbox into Deafault.aspx.

Step 4: Double-click Default.aspx.cs, add the code below to load a PDF file, Note that you have to add the following if statement and !IsPostBack property before loading the pdf file.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//load the sample PDF file
this.pdfViewer1.CacheInterval = 1000;
this.pdfViewer1.CacheTime = 1200;
this.pdfViewer1.CacheNumberImage = 1000;
this.pdfViewer1.ScrollInterval = 300;
this.pdfViewer1.ZoomFactor = 1f;
this.pdfViewer1.CustomErrorMessages = "";
this.pdfViewer1.LoadFromFile("PDF file/Test.pdf");
}
}
Effect screenshot:
