Automated content replication within Word documents is critical for enterprise document management systems. Manual copying introduces formatting inconsistencies and ineffficiencies in bulk operations. This guide demonstrates programmatic approaches for duplicating entire documents, specific paragraphs, and individual sections using Free Spire.Doc for .NET without Microsoft Office dependencies.
Install the library via NuGet Package Manager Console:
Install-Package FreeSpire.Doc
Complete Document Duplication
To create an independent copy of an entire Word document including all embedded elements and formatting, utilize the Clone method. This approach preserves all structural components while establishing a separate document instance.
using Spire.Doc;
class DocumentDuplicator
{
static void Main()
{
try
{
Document inputDoc = new Document();
inputDoc.LoadFromFile("source_document.docx");
Document clonedDoc = inputDoc.Clone() as Document;
clonedDoc.SaveToFile("full_duplicate.docx", FileFormat.Docx);
inputDoc.Dispose();
clonedDoc.Dispose();
System.Diagnostics.Process.Start("full_duplicate.docx");
}
catch (System.IO.IOException ioEx)
{
System.Console.Error.WriteLine($"File access error: {ioEx.Message}");
}
catch (System.Exception ex)
{
System.Console.Error.WriteLine($"Duplication failed: {ex.Message}");
}
}
}
Key implemantation details:
- The
Cloneoperation creates a deep copy with isolated memory references - Resource disposal via
Disposeprevents file handle leaks - Granular exception handling distinguishes I/O errors from general exceptions
Targeted Paragraph Extraction
When extracting specific paragraphs while maintaining original formatting, access the paragraph collection through document sections. The following demonstrates copying a designated paragraph to another document's final section.
using Spire.Doc;
class ParagraphExtractor
{
static void Main()
{
const int TARGET_PARAGRAPH_INDEX = 3;
Document source = new Document("input.docx");
Document destination = new Document("target.docx");
Section sourceSection = source.Sections[0];
Paragraph selectedPara = sourceSection.Paragraphs[TARGET_PARAGRAPH_INDEX];
Section destinationSection = destination.LastSection;
destinationSection.Paragraphs.Add(selectedPara.Clone() as Paragraph);
destination.SaveToFile("paragraph_extraction.docx", FileFormat.Docx);
source.Dispose();
destination.Dispose();
}
}
Implementation notes:
- Paragraph indexing starts at zero (index 3 references the 4th paragraph)
- Explicit casting ensures type safety during object cloning
- Direct section access avoids unnecessary collection iterations
Section-Level Content Replication
For duplicating entire sections including mixed content types (tables, images, paragraphs), iterate through the body's child objects. This approach provides granular control over element inclusion.
using Spire.Doc;
class SectionCopier
{
static void Main()
{
Document sourceDoc = new Document("source.docx");
Document targetDoc = new Document("destination.docx");
int SECTION_INDEX = 1;
Section sourceSection = sourceDoc.Sections[SECTION_INDEX];
Section targetSection = targetDoc.LastSection;
foreach (DocumentObject contentItem in sourceSection.Body.ChildObjects)
{
targetSection.Body.ChildObjects.Add(contentItem.Clone());
}
targetDoc.SaveToFile("section_replication.docx", FileFormat.Docx);
sourceDoc.Dispose();
targetDoc.Dispose();
}
}
Advanced usage considerations:
- Modify the iteration loop to filter specific content types (e.g., skip empty paragraphs)
- Adjust SECTION_INDEX to target different document sections
- ChildObjects collection maintains original rendering sequence