Import Document Element
This article explains how you could import a document element from one document into another using the API of RadWordsProcessing.
DocumentElementImporter Class
DocumentElementImporter class represents a utility class, which is used to import document elements from one document to another. It also handles the style repository merging.
Create a DocumentElementImporter
The constructor of the DocumentElementImporter class accepts three parameters:
- targetDocument: The RadFlowDocument instance for which the elements will be prepared for inserting.
- sourceDocument: The RadFlowDocument from where the elements will be imported.
- conflictingStylesResolutionMode: Represents the resolution mode, which will be used if a style conflict appears during the style repositories merging. Could be set to one of the values of the ConflictingStylesResolutionMode enumeration.
Example 1: Create DocumentElementImporter
DocumentElementImporter importer = new DocumentElementImporter(targetDocument, sourceDocument, ConflictingStylesResolutionMode.UseTargetStyle);
Use DocumentElementImporter
The DocumentElementImporter class exposes the Import<T>() method, which is used to prepare a document element from the source document for import into the target document. Example 2 demonstrates how you could work with this method, using the DocumentElementImporter instance, created in Example 1.
Example 2: Import a document element
Run run = sourceDocument.EnumerateChildrenOfType<Run>().First();
Run importedRun = importer.Import<Run>(run);
Style Repositories Merging
The merging of the styles between the two documents (target and source) is executed when the Import() method is invoked for the first time.
Use Cases
In Table 1 is described the behavior of the Import<T>() method of DocumentElementImporter in different scenarios.
Table 1
Action | Result |
---|---|
Invoke Import() method with a document element. | Returns the document element cloned. |
Invoke Import() method with a paragraph, which contains unpaired annotation marker (e.g. there is a Bookmark, which is spanned between two paragraphs, one of which is passed as a parameter). | Returns the paragraph and all its inlines cloned. The unpaired annotation marker is cleared. |
Invoke Import() method with a section, which contains paragraphs which contain by their side unpaired annotations (e.g. there is a Bookmark, which is spanned between two paragraphs and their parent section is passed as a parameter). | Returns the section and all its children cloned. The annotation markers are not cleared. |
Invoke Import() method with a paragraph, which has a style, renamed during the styles merging. | Returns the paragraph and all its children cloned with the correctly renamed StyleId property. |
Invoke Import() method with a paragraph, which has a style, renamed during the styles merging multiple times. | Returns the paragraph and all its children cloned with the correctly renamed StyleId property. |
Invoke Import() method with a document element, which is not a child of the source document. | Throws an InvalidOperationException. |