News Category

Copy Word Paragraph from One Document to Another in C#, VB.NET

2013-02-18 07:52:54 Written by  support iceblue
Rate this item
(1 Vote)

When copying Word paragraph, users can choose copy text only or copy all elements of paragraph, such as formatting, image, hyperlink etc. Solution in this guide presents how to copy Word paragraph (text and format) from one document to another in C# and VB.NET via Spire.Doc for .NET.

Spire.Doc for .NET, a stand-alone .NET Word component, provides a method, Paragraph.Clone() to allow users to copy paragraph. The following screenshot shows the result after copying.

Copy Word Paragraph

Download and install Spire.Doc for .NET and follow the steps to copy Word paragraphs. Firstly, initialize a Document instance doc1 from specified document and a new Document instance doc2. Secondly, initialize two Paragraph instances p1 and p2 which are the title and first paragraph of doc1. Thirdly, instance a new Section instance and two Paragraph instances. Assign the result after invoking Paragraph.Clone() as value for these two instances. Then, invoke Section.Paragraph.Add method with parameter paragraph to add the copied paragraphs in new document. Code as following:

[C#]
using Spire.Doc;
using Spire.Doc.Documents;
using System.Drawing;

namespace CopyParagraph
{
    class Program
    {
        static void Main(string[] args)
        {
            //Load doc1
            Document doc1 = new Document();
            doc1.LoadFromFile(@"E:\Work\Document\A GOOD MAN IS HARD TO FIND.docx");

            //Create New doc2
            Document doc2 = new Document();

            //Get Paragraph 1(the Title) and Paragraph 2(body) in Doc1
            Section s = doc1.Sections[0];
            Paragraph p1 = s.Paragraphs[0];
            Paragraph p2 = s.Paragraphs[1]; 

            //Copy p1 and p2 to doc2
            Section s2 = doc2.AddSection();
            Paragraph NewPara1 = (Paragraph)p1.Clone();
            s2.Paragraphs.Add(NewPara1);
            Paragraph NewPara2 = (Paragraph)p2.Clone();
            s2.Paragraphs.Add(NewPara2);

            //Add Watermark
            PictureWatermark WM = new PictureWatermark();
            WM.Picture = Image.FromFile(@"E:\Work\Document\Rose.jpg");
            doc2.Watermark = WM;

            //Save and Launch
            doc2.SaveToFile("copy.docx", FileFormat.Docx2010);
            System.Diagnostics.Process.Start("copy.docx");
        }
    }
}
[VB.NET]
Imports Spire.Doc
Imports Spire.Doc.Documents
Imports System.Drawing

Namespace CopyParagraph
    Friend Class Program
        Shared Sub Main(ByVal args() As String)
            'Load doc1
            Dim doc1 As New Document()
            doc1.LoadFromFile("E:\Work\Document\A GOOD MAN IS HARD TO FIND.docx")

            'Create New doc2
            Dim doc2 As New Document()

            'Get Paragraph 1(the Title) and Paragraph 2(body) in Doc1
            Dim s As Section = doc1.Sections(0)
            Dim p1 As Paragraph = s.Paragraphs(0)
            Dim p2 As Paragraph = s.Paragraphs(1)

            'Copy p1 and p2 to doc2
            Dim s2 As Section = doc2.AddSection()
            Dim NewPara1 As Paragraph = CType(p1.Clone(), Paragraph)
            s2.Paragraphs.Add(NewPara1)
            Dim NewPara2 As Paragraph = CType(p2.Clone(), Paragraph)
            s2.Paragraphs.Add(NewPara2)

            'Add Watermark
            Dim WM As New PictureWatermark()
            WM.Picture = Image.FromFile("E:\Work\Document\Rose.jpg")
            doc2.Watermark = WM

            'Save and Launch
            doc2.SaveToFile("copy.docx", FileFormat.Docx2010)
            System.Diagnostics.Process.Start("copy.docx")
        End Sub
    End Class
End Namespace

Note: Because type of paragraph.Clone() is DocumentObject, cast its type as Paragraph when initializing paragraph instance in new Word document doc2.

Spire.Doc, as professional Word component, is very powerful on fast generating, loading, writing, modifying and saving Word documents in .NET, WPF, Silverlight without Word automation and any third party tools.

Additional Info

  • tutorial_title: Copy Word Paragraph
Last modified on Friday, 03 September 2021 03:34