When you copy nodes from one document to another, this option specifies how formatting
is resolved when both documents have a style with the same name, but different formatting.
The formatting is resolved as follows:
- Built-in styles are matched using their locale independent style identifier.
User defined styles are matched using case-sensitive style name.
- If a matching style is not found in the destination document, the style
(and all styles referenced by it) are copied into the destination document
and the imported nodes are updated to reference the new style.
- If a matching style already exists in the destination document, what happens
depends on the
importFormatMode
parameter passed to
Document.ImportNode
as described below.
When using the UseDestinationStyles option, if a matching style already exists
in the destination document, the style is not copied and the imported nodes are updated
to reference the existing style.
The drawback of using UseDestinationStyles is that the imported text might
look different in the destination document comparing to the source document.
For example, the "Heading 1" style in the source document uses Arial 16pt font and
the "Heading 1" style in the destination document uses Times New Roman 14pt font.
When importing text of "Heading 1" style with no other direct formatting, it will
appear as Times New Roman 14pt font in the destination document.
Using the KeepSourceFormatting option allows to make sure the imported
text looks in the destination document exactly like it was in the source document.
If a matching style already exists in the destination document, the source style
is copied and given a unique name by appending a suffix number to it, for example
"Normal_0" or "Heading 1_5".
The drawback of using KeepSourceFormatting is that if you perform several imports,
you could end up with many styles in the destination document and that could make using
consistent style formatting in Microsoft Word difficult for this document.
Using KeepDifferentStyles option allows to reuse destination styles
if the formatting they provide is identical to the styles in the source document.
If the style in destination document is different from the source then it is imported.
Example:
Shows how to manually append the content from one document to the end of another document.
/**
* A manual implementation of the Document.AppendDocument function which shows the general
* steps of how a document is appended to another.
*
* @param dstDoc The destination document where to append to.
* @param srcDoc The source document.
* @param mode The import mode to use when importing content from another document.
*/
public void appendDocument(Document dstDoc, Document srcDoc, int mode) throws Exception
{
// Loop through all sections in the source document.
// Section nodes are immediate children of the Document node so we can just enumerate the Document.
for (Node srcNode : srcDoc)
{
Section srcSection = (Section)srcNode;
// Because we are copying a section from one document to another,
// it is required to import the Section node into the destination document.
// This adjusts any document-specific references to styles, lists, etc.
//
// Importing a node creates a copy of the original node, but the copy
// is ready to be inserted into the destination document.
Node dstSection = dstDoc.importNode(srcSection, true, mode);
// Now the new section node can be appended to the destination document.
dstDoc.appendChild(dstSection);
}
}
Example:
Shows how to manually prepend the content from one document to the beginning of another document.
public static void prependDocumentMain() throws Exception
{
Document dstDoc = new Document(gDataDir + "TestFile.Destination.doc");
Document srcDoc = new Document(gDataDir + "TestFile.Source.doc");
// Append the source document to the destination document. This causes the result to have line spacing problems.
dstDoc.appendDocument(srcDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING);
// Instead prepend the content of the destination document to the start of the source document.
// This results in the same joined document but with no line spacing issues.
prependDocument(srcDoc, dstDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING);
}
/**
* A modified version of the AppendDocument method which prepends the content of one document to the start
* of another.
*
* @param dstDoc The destination document where to prepend the source document to.
* @param srcDoc The source document.
*/
public static void prependDocument(Document dstDoc, Document srcDoc, int mode) throws Exception
{
// Loop through all sections in the source document.
// Section nodes are immediate children of the Document node so we can just enumerate the Document.
ArrayList sections = (ArrayList)Arrays.asList(srcDoc.getSections().toArray());
// Reverse the order of the sections so they are prepended to start of the destination document in the correct order.
Collections.reverse(sections);
for (Section srcSection : (Iterable<Section>) sections)
{
// Import the nodes from the source document.
Node dstSection = dstDoc.importNode(srcSection, true, mode);
// Now the new section node can be prepended to the destination document.
// Note how PrependChild is used instead of AppendChild. This is the only line changed compared
// to the original method.
dstDoc.prependChild(dstSection);
}
}