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.
To copy content within a document with or without formatting:
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:
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"); |
To copy content between documents and keep source document's formatting:
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"); |
The output of above code will look like below in the target Word document:
To move content within a document with or without formatting:
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:
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"); |
For more information on how to copy or move document content using DsWord, see DsWord sample browser.