Clone and Merge
Merging Documents
RadWordsProcessing allows you to merge two RadFlowDocument instance 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 target = new RadFlowDocument();
RadFlowDocument source = new RadFlowDocument();
//...
// target will contain merged content and styles.
target.Merge(source);
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 StyleRepository to the target StyleRepository.
The MergeOptions parameter can be passed 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 changed ID (with "_1" suffix).
Example 2 shows how to merge documents by specifying the MergeOptions parameter.
Example 2: Merge documents with MergeOptions
RadFlowDocument target = new RadFlowDocument();
RadFlowDocument source = new RadFlowDocument();
//...
MergeOptions mergeOptions = new MergeOptions()
{
ConflictingStylesResolutionMode = ConflictingStylesResolutionMode.RenameSourceStyle
};
target.Merge(source, mergeOptions);
You could insert one document into another at a specified position using the InsertDocument() method of RadFlowDocumentEditor. More information is available here.
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 of the document elements using the Clone() method overloads. Below is a list of document elements that provide such methods:
- RadFlowDocument
- Section
- Paragraph
- Run
- Table
- TableRow
- TableCell
- ImageInline
- FloatingImage
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 provided RadFlowDocument. This allows cloned elements to be added to the element tree of the provided RadFlowDocument 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(radFlowDocument);
With the DocumentElementImporter class you can import a document element from one document (source) and insert it into another (target). For more details, check this article.
Cloning Other Objects
The following styling objects also implement the Clone() method, which can be used to create deep copies of them:
- Style
- List
- ListLevel
- Watermark