Document Solutions for Word
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.

    DsWord 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");
    Back to Top

    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 DsWord, 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");

    Back to Top

    To copy content between documents and keep source document's formatting:

    1. Define a style in the target document by using various properties like Font and ParagraphFormat. Add a paragraph using this style by using Styles method of StyleCollection type.
    2. Similarly, define a style with the same name in the source document but with different set of properties. Add a paragraph using this style by using Styles method of StyleCollection type.
    3. Copy the paragraph from source document to target document using KeepSource value of FormattingCopyStrategy enumeration.       
      C#
      Copy Code
      // The name of the style used in both documents
      const string styleName = "X-style";
      
      // The target document
      var doc = new GcWordDocument();
      
      // Add a paragraph style to the target document
      var xStyleTgt = doc.Styles.Add(styleName, StyleType.Paragraph);
      xStyleTgt.Font.Color.RGB = Color.Blue;
      xStyleTgt.Font.Bold = true;
      xStyleTgt.ParagraphFormat.Indentation.RightIndent += 72;
      // Add a paragraph with that style
      var textTgt = $"This paragraph in the target document " +
          $"is associated with a custom paragraph style named \"{styleName}\". " +
          $"That style specifies the font to be blue and bold, and the whole paragraph " +
          $"is indented by 1\" on the right.";
      doc.Body.Paragraphs.Add(textTgt, doc.Styles[styleName]);
      
      // The source document, will copy from it to the target document keeping formatting
      var docSrc = new GcWordDocument();
      // Add a style with the same name to the source
      var xStyleSrc = docSrc.Styles.Add(styleName, StyleType.Paragraph);
      xStyleSrc.Font.Color.RGB = Color.Red;
      xStyleSrc.Font.Italic = true;
      xStyleSrc.ParagraphFormat.Alignment = ParagraphAlignment.Right;
      xStyleSrc.ParagraphFormat.Indentation.LeftIndent += 72;
      // Add a paragraph with the style, this paragraph will be copied to target
      var textSrc = "This paragraph is copied from the source to the target DOCX. " +
          $"In the source DOCX, this paragraph was associated with a custom style also named \"{styleName}\". " +
          $"That style specifies the font to be red and italic, the whole paragraph is right-aligned " +
          $"and indented 1\" from the left. " +
          $"Due to a conflict with the same-named but different style in the target document " +
          $"its name is changed to \"{styleName}1\".";
      var paraSrc = docSrc.Body.Paragraphs.Add(textSrc, docSrc.Styles[styleName]);
      
      // Copy paragraph from source to target using FormattingCopyStrategy.KeepSource
      paraSrc.GetRange().CopyTo(doc.Body, InsertLocation.End, FormattingCopyStrategy.KeepSource);
      
      //Save target document
      doc.Save("keepsourceformatting.docx");

    Back to Top

    The output of above code will look like below in the target Word document:

    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");
    Back to Top

    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 DsWord, 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");
    Back to Top

    For more information on how to copy or move document content using DsWord, see DsWord sample browser.