Clone and Merge
Merging Documents
RadWordsProcessing allows you to merge two RadFlowDocument instances using the Merge() method overloads. The document to which you wish to add content is called target and the document from which you wish to take the content is called source.
Example 1: Merge Two Instances of RadFlowDocument
RadFlowDocument targetDocument;
RadFlowDocument sourceDocument;
DocxFormatProvider docxFormatProvider = new DocxFormatProvider();
byte[] targetByteArray = File.ReadAllBytes("targetDocument.docx");
byte[] sourceByteArray = File.ReadAllBytes("sourceDocument.docx");
targetDocument = docxFormatProvider.Import(targetByteArray, TimeSpan.FromSeconds(10));
sourceDocument = docxFormatProvider.Import(sourceByteArray, TimeSpan.FromSeconds(10));
//targetDocument will contain merged content and styles.
targetDocument.Merge(sourceDocument);
The Merge method performs two distinct operations:
- Adds all sections from the source document into the target document. The sections from the source document are inserted at the end of the target document.
- Adds all styles from the source
StyleRepositoryto the targetStyleRepository.
You can pass the MergeOptions parameter as an argument to the Merge() method to better control the merge process. It provides the following customization options:
-
ConflictingStylesResolutionMode: This option controls how conflicts between styles in target and source style repositories are resolved. Styles are in conflict when they have the same ID (Style.Id). Possible values are:-
UseTargetStyle: In case of conflict, the style from the target document stays in the target repository and the style from the source document is not added to the target repository. -
RenameSourceStyle: In case of conflict, the style from the target document stays in the target repository and the style from the source document is added to the target repository with a changed ID (with "_1" suffix).
-
Example 2 shows how to merge documents by specifying the MergeOptions parameter.
Example 2: Merge Documents with MergeOptions
RadFlowDocument targetDocument = new RadFlowDocument();
RadFlowDocument sourceDocument = new RadFlowDocument();
//...
MergeOptions mergeOptions = new MergeOptions()
{
ConflictingStylesResolutionMode = ConflictingStylesResolutionMode.RenameSourceStyle
};
targetDocument.Merge(sourceDocument, mergeOptions);
You can insert one document into another at a specified position using the
InsertDocument()method ofRadFlowDocumentEditor. For more information, refer to Insert Documents.
Cloning RadFlowDocument
RadFlowDocument provides a Clone() method, which creates a deep copy of the whole document structure, including all document elements and styles:
Example 3: Clone a RadFlowDocument
RadFlowDocument clonedDocument = document.Clone();
Cloning Document Elements
You can create a deep copy of most document elements using the Clone() method overloads. The following list shows document elements that provide such methods:
RadFlowDocumentSectionParagraphRunTableTableRowTableCellImageInlineFloatingImage
The Clone() method has two overloads:
Clone(): Creates a deep copy of the document element and associates it with the same document.Clone(RadFlowDocument): Creates a deep copy of the element and associates it with the providedRadFlowDocument. This allows cloned elements to be added to the element tree of the provided document at a later time and is convenient if you want to move an element from one document to another.
Example 4: Clone a Section
Section clonedSection = section.Clone(document);
With the
DocumentElementImporterclass you can import a document element from one document (source) and insert it into another (target). For more details, refer to Import Document Element.
Cloning Other Objects
The following styling objects also implement the Clone() method, which you can use to create deep copies of them:
StyleListListLevelWatermark