Importing Nodes with NodeImporter
Importing Nodes with NodeImporter
This guide shows how to move content between two different Document instances in Aspose.Words FOSS for .NET using the dedicated NodeImporter class and fine-grained ImportFormatOptions, for scenarios where you need to import many nodes efficiently or control import behavior beyond the basic ImportFormatMode choice. For the simpler Document.ImportNode, Document.AppendDocument, and DocumentBuilder.InsertDocument convenience methods, see the Working with Documents guide.
NodeImporter: Repeated Imports Without Repeated Setup
NodeImporter is built for repeated import of nodes from one document to another, avoiding the per-call setup cost of a single-use import. Construct one with new NodeImporter(srcDoc, dstDoc, importFormatMode) — or the overload new NodeImporter(srcDoc, dstDoc, importFormatMode, importFormatOptions) for finer control — once, then call ImportNode(srcNode, isImportChildren) on that same instance for every node you need to bring across. Reusing one NodeImporter instance across many calls avoids the setup cost that a per-call convenience import incurs each time.
Fine-Grained Import Control with ImportFormatOptions
ImportFormatOptions supplements the basic ImportFormatMode choice (UseDestinationStyles, KeepSourceFormatting, KeepDifferentStyles) with finer switches: SmartStyleBehavior (bool) controls how styles with equal names in the source and destination are imported. KeepSourceNumbering (bool) controls how numbering is imported when it clashes between source and destination. IgnoreTextBoxes and IgnoreHeaderFooter (bool) suppress source formatting for textbox and header/footer content specifically when KeepSourceFormatting is in effect. MergePastedLists (bool) merges pasted lists with surrounding lists instead of keeping them separate. ForceCopyStyles (bool) copies conflicting styles instead of reusing the destination’s version, in KeepSourceFormatting mode. AdjustSentenceAndWordSpacing (bool) automatically adjusts spacing. AppendDocumentWithNewPage (bool) forces the first imported section’s type to NewPage.
Tips and Best Practices
- Create a single
NodeImporterinstance and reuse it across a loop ofImportNodecalls when importing many nodes — constructing a new importer per node repeats setup work unnecessarily. - Pass an
ImportFormatOptionsinstance to theNodeImporterconstructor overload when the plainImportFormatModechoice isn’t precise enough — for example, to keep source list numbering withKeepSourceNumberingwhile still usingKeepSourceFormattingoverall. - Set
IgnoreTextBoxesorIgnoreHeaderFooterwhenKeepSourceFormattingpulls in more source styling than you want for just those content types. NodeImporter.ImportNode(srcNode, isImportChildren)mirrors the document-level import call in signature and behavior; the difference is reuse efficiency, not import semantics.
Common Issues
| Issue | Cause | Fix |
|---|---|---|
| Repeated imports of many nodes are slow | A convenience import call is used per node instead of one reused NodeImporter | Construct a single NodeImporter and call ImportNode on it for each node |
| List numbering resets unexpectedly after import | ImportFormatOptions.KeepSourceNumbering was not set | Construct NodeImporter with an ImportFormatOptions instance that sets KeepSourceNumbering as needed |
| Textbox or header/footer content picks up unwanted source formatting | KeepSourceFormatting mode applies source styling broadly by default | Set IgnoreTextBoxes or IgnoreHeaderFooter on the ImportFormatOptions passed to NodeImporter |
FAQ
When should I use NodeImporter instead of a single import call?
When importing many nodes in a loop — construct one NodeImporter up front and call ImportNode repeatedly, rather than paying setup cost on each call.
What does ImportFormatOptions add beyond ImportFormatMode?
Finer switches such as SmartStyleBehavior, KeepSourceNumbering, IgnoreTextBoxes, IgnoreHeaderFooter, MergePastedLists, ForceCopyStyles, AdjustSentenceAndWordSpacing, and AppendDocumentWithNewPage, layered on top of the base ImportFormatMode choice.
Does NodeImporter.ImportNode behave differently from importing at the document level?
No — both take a source node and an isImportChildren flag and import it into the destination document; NodeImporter exists so that setup is done once and reused across many calls.
How do I keep the source document’s list numbering intact when importing?
Construct NodeImporter with an ImportFormatOptions where KeepSourceNumbering is true.
API Reference Summary
| Class/Method | Description |
|---|---|
NodeImporter | Performs repeated import of nodes from one document to another, reusing setup across calls |
NodeImporter(srcDoc, dstDoc, importFormatMode) | Constructor for basic import-format-mode-only imports |
NodeImporter(srcDoc, dstDoc, importFormatMode, importFormatOptions) | Constructor accepting fine-grained ImportFormatOptions |
NodeImporter.ImportNode(srcNode, isImportChildren) | Imports a node from the source document into the destination document |
ImportFormatOptions | Fine-grained import settings layered on top of ImportFormatMode |
ImportFormatOptions.SmartStyleBehavior | How equally-named styles are imported |
ImportFormatOptions.KeepSourceNumbering | How clashing numbering is imported |
ImportFormatOptions.IgnoreTextBoxes / IgnoreHeaderFooter | Suppresses source formatting for textboxes / headers-footers under KeepSourceFormatting |
ImportFormatOptions.MergePastedLists | Merges pasted lists with surrounding lists |
ImportFormatOptions.ForceCopyStyles | Copies conflicting styles instead of reusing the destination’s version |
ImportFormatOptions.AppendDocumentWithNewPage | Forces the first imported section’s type to NewPage |