Documents for Word, .NET Edition Documentation
Features / Document / Copy or Move Document Content
In This Topic
    Copy or Move Document Content
    In This Topic

    Copying or moving content in documents helps in organizing the document content in a relevant manner.

    GcWord allows you to perform the copy or move operations on document content within the document or between documents. The CopyTo and MoveTo methods in the RangeBase class are used to achieve the same. While performing the copy or move operations, the formatting settings of the content can also be copied or cleared using the FormattingCopyStrategy enumeration.

    Copy Document Content

    To copy content within a document with or without formatting:

    1. Load a document using the Load method of GcWordDocument class.
    2. Copy the second paragraph after the third paragraph using the CopyTo method of the RangeBase class. To keep the formatting of content control, the Copy value of FormattingCopyStrategy enumeration has been passed as a parameter to the CopyTo method.
    3. Copy the table after the fourth paragraph in the document. To clear the formatting of table, the Clear value of FormattingCopyStrategy enumeration has been passed as a parameter to this method.
      C#
      Copy Code
      var doc = new GcWordDocument();
      doc.Load("Test.docx");
      
      //Copy the second paragraph after the third paragraph with formatting
      doc.Body.Paragraphs[1].GetRange().CopyTo(doc.Body.Paragraphs[2].GetRange(), InsertLocation.After, FormattingCopyStrategy.Copy);
      doc.Save("Copy_WithinDoc_WithFormatting.docx");
      
      //Copy table after the fourth paragraph without formatting
      doc.Body.Tables[0].GetRange().CopyTo(doc.Body.Paragraphs[3].GetRange(), InsertLocation.After, FormattingCopyStrategy.Clear);
      doc.Save("Copy_WithinDoc_WithoutFormatting.docx");
      

     

    To copy content between documents with or without formatting:

    1. Load a document using the Load method of GcWordDocument class.
    2. Create a new document in which you want to copy the content and add a new paragraph using the Add method of ParagraphCollection class.
    3. Copy the table from first document, in the second document, using the CopyTo method of the RangeBase class. To keep the formatting of table, the Copy value of FormattingCopyStrategy enumeration has been passed as a parameter to the CopyTo method.
    4. Copy the paragraph from first document, at the start of second document. To clear the formatting of paragraph, the Clear value of FormattingCopyStrategy enumeration has been passed as a parameter to this method.
      C#
      Copy Code
      var doc1 = new GcWordDocument();
      doc1.Load("Test.docx");
      
      var doc2 = new GcWordDocument();
      doc2.Body.Sections[0].GetRange().Paragraphs.Add("Using GcWord, content can be copied with or without formatting in single document or between documents");
      
      //Copy table from first document after the first paragraph of second document with formatting
      doc1.Body.Tables[0].GetRange().CopyTo(doc2.Body.Paragraphs.First.GetRange(), InsertLocation.After, FormattingCopyStrategy.Copy);
      doc2.Save("Copy_BetweenDocs_WithFormatting.docx");
      
      //Copy second paragraph of first document at the start of the second document without formatting
      doc1.Body.Paragraphs[1].GetRange().CopyTo(doc2.Body, InsertLocation.Start, FormattingCopyStrategy.Clear);
      doc2.Save("Copy_BetweenDocs_WithoutFormatting.docx");
      

    Move Document Content

    To move content within a document with or without formatting:

    1. Load a document using the Load method of GcWordDocument class.
    2. Move the second paragraph after the third paragraph using the MoveTo method of the RangeBase class. To keep the formatting of paragraph, the Copy value of FormattingCopyStrategy enumeration has been passed as a parameter to the MoveTo method.
    3. Move table before the first paragraph in the document. To clear the formatting of content control, the Clear value of FormattingCopyStrategy enumeration has been passed as a parameter to this method.   
      C#
      Copy Code
      var doc = new GcWordDocument();
      doc.Load("Test.docx");
      
      //Move second paragraph after the third paragraph with formatting  
      doc.Body.Paragraphs[1].GetRange().MoveTo(doc.Body.Paragraphs[2].GetRange(), InsertLocation.After, FormattingCopyStrategy.Copy);
      doc.Save("Move_WithinDoc_WithFormatting.docx");
      
      //Move table before the first paragraph without formatting
      doc.Body.Tables[0].GetRange().MoveTo(doc.Body.Paragraphs.First.GetRange(), InsertLocation.Before, FormattingCopyStrategy.Clear);
      doc.Save("Move_BetweenDocs_WithoutFormatting.docx");
      

     

    To move content between documents with or without formatting:

    1. Load a document using the Load method of GcWordDocument class.
    2. Create a new document in which you want to move the content and add a new paragraph using the Add method of ParagraphCollection class.
    3. Move the table from first document, in the second document, using the MoveTo method of the RangeBase class. To keep the formatting of table, the Copy value of FormattingCopyStrategy enumeration has been passed as a parameter to the MoveTo method.
    4. Move an image from first document, at the end of the last paragraph's run of the second document. To clear the formatting of image, the Clear value of FormattingCopyStrategy enumeration has been passed as a parameter to this method.   
      C#
      Copy Code
      var doc1 = new GcWordDocument();
      doc1.Load("Test.docx");
      
      var doc2 = new GcWordDocument();
      doc2.Body.Paragraphs.Add("Using GcWord, content can be moved with or without formatting in single document or between documents");
      
      //Move table from first document before the first paragraph of second document with formatting
      doc1.Body.Tables[0].GetRange().MoveTo(doc2.Body.Paragraphs.First.GetRange(), InsertLocation.Before, FormattingCopyStrategy.Copy);
      doc2.Save("Move_BetweenDocs_WithFormatting.docx");
      
      //Move image from first document at the end of the last paragraph's run of the second document without formatting
      doc1.Body.Pictures.First.GetRange().MoveTo(doc2.Body.Paragraphs.Last.GetRange().Runs.First.GetRange(), InsertLocation.End, FormattingCopyStrategy.Clear);
      doc2.Save("Move_BetweenDocs_WithoutFormatting.docx");