java.lang.Object
com.aspose.words.ImportFormatOptions
public class ImportFormatOptions
Example:
Document dstDoc = new Document();
DocumentBuilder builder = new DocumentBuilder(dstDoc);
Style myStyle = builder.getDocument().getStyles().add(StyleType.PARAGRAPH, "MyStyle");
myStyle.getFont().setSize(14.0);
myStyle.getFont().setName("Courier New");
myStyle.getFont().setColor(Color.BLUE);
builder.getParagraphFormat().setStyleName(myStyle.getName());
builder.writeln("Hello world!");
// Clone the document and edit the clone's "MyStyle" style, so it is a different color than that of the original.
// If we insert the clone into the original document, the two styles with the same name will cause a clash.
Document srcDoc = dstDoc.deepClone();
srcDoc.getStyles().get("MyStyle").getFont().setColor(Color.RED);
// When we enable SmartStyleBehavior and use the KeepSourceFormatting import format mode,
// Aspose.Words will resolve style clashes by converting source document styles.
// with the same names as destination styles into direct paragraph attributes.
ImportFormatOptions options = new ImportFormatOptions();
options.setSmartStyleBehavior(true);
builder.insertDocument(srcDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING, options);
dstDoc.save(getArtifactsDir() + "DocumentBuilder.SmartStyleBehavior.docx");
| Constructor Summary |
|---|
ImportFormatOptions()
|
| Property Getters/Setters Summary | ||
|---|---|---|
boolean | getIgnoreHeaderFooter() | |
void | setIgnoreHeaderFooter(boolean value) | |
Gets or sets a boolean value that specifies that source formatting of headers/footers content ignored
if true.
|
||
boolean | getIgnoreTextBoxes() | |
void | setIgnoreTextBoxes(boolean value) | |
Gets or sets a boolean value that specifies that source formatting of textboxes content ignored
if true.
|
||
boolean | getKeepSourceNumbering() | |
void | setKeepSourceNumbering(boolean value) | |
Gets or sets a boolean value that specifies how the numbering will be imported when it clashes in source and
destination documents.
The default value is false.
|
||
boolean | getMergePastedLists() | |
void | setMergePastedLists(boolean value) | |
Gets or sets a boolean value that specifies whether pasted lists will be merged with surrounding lists.
The default value is false.
|
||
boolean | getSmartStyleBehavior() | |
void | setSmartStyleBehavior(boolean value) | |
Gets or sets a boolean value that specifies how styles will be imported
when they have equal names in source and destination documents.
The default value is false.
|
||
| Constructor Detail |
|---|
public ImportFormatOptions()
| Property Getters/Setters Detail |
|---|
getIgnoreHeaderFooter/setIgnoreHeaderFooter | |
public boolean getIgnoreHeaderFooter() / public void setIgnoreHeaderFooter(boolean value) | |
true.
Example:
Shows how to specifies ignoring or not source formatting of headers/footers content.Document dstDoc = new Document(getMyDir() + "Document.docx"); Document srcDoc = new Document(getMyDir() + "Header and footer types.docx"); ImportFormatOptions importFormatOptions = new ImportFormatOptions(); importFormatOptions.setIgnoreHeaderFooter(false); dstDoc.appendDocument(srcDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING, importFormatOptions); dstDoc.save(getArtifactsDir() + "DocumentBuilder.DoNotIgnoreHeaderFooter.docx");
getIgnoreTextBoxes/setIgnoreTextBoxes | |
public boolean getIgnoreTextBoxes() / public void setIgnoreTextBoxes(boolean value) | |
true.
Example:
Shows how to manage text box formatting while appending a document.
// Create a document that will have nodes from another document inserted into it.
Document dstDoc = new Document();
DocumentBuilder builder = new DocumentBuilder(dstDoc);
builder.writeln("Hello world!");
// Create another document with a text box, which we will import into the first document.
Document srcDoc = new Document();
builder = new DocumentBuilder(srcDoc);
Shape textBox = builder.insertShape(ShapeType.TEXT_BOX, 300.0, 100.0);
builder.moveTo(textBox.getFirstParagraph());
builder.getParagraphFormat().getStyle().getFont().setName("Courier New");
builder.getParagraphFormat().getStyle().getFont().setSize(24.0);
builder.write("Textbox contents");
// Set a flag to specify whether to clear or preserve text box formatting
// while importing them to other documents.
ImportFormatOptions importFormatOptions = new ImportFormatOptions();
importFormatOptions.setIgnoreTextBoxes(ignoreTextBoxes);
// Import the text box from the source document into the destination document,
// and then verify whether we have preserved the styling of its text contents.
NodeImporter importer = new NodeImporter(srcDoc, dstDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING, importFormatOptions);
Shape importedTextBox = (Shape) importer.importNode(textBox, true);
dstDoc.getFirstSection().getBody().getParagraphs().get(1).appendChild(importedTextBox);
if (ignoreTextBoxes) {
Assert.assertEquals(12.0d, importedTextBox.getFirstParagraph().getRuns().get(0).getFont().getSize());
Assert.assertEquals("Times New Roman", importedTextBox.getFirstParagraph().getRuns().get(0).getFont().getName());
} else {
Assert.assertEquals(24.0d, importedTextBox.getFirstParagraph().getRuns().get(0).getFont().getSize());
Assert.assertEquals("Courier New", importedTextBox.getFirstParagraph().getRuns().get(0).getFont().getName());
}
dstDoc.save(getArtifactsDir() + "DocumentBuilder.IgnoreTextBoxes.docx");getKeepSourceNumbering/setKeepSourceNumbering | |
public boolean getKeepSourceNumbering() / public void setKeepSourceNumbering(boolean value) | |
false.
Example:
Shows how resolve a clash when importing documents that have lists with the same list definition identifier.Document srcDoc = new Document(getMyDir() + "List with the same definition identifier - source.docx"); Document dstDoc = new Document(getMyDir() + "List with the same definition identifier - destination.docx"); ImportFormatOptions importFormatOptions = new ImportFormatOptions(); // Set the "KeepSourceNumbering" property to "true" to apply a different list definition ID // to identical styles as Aspose.Words imports them into destination documents. importFormatOptions.setKeepSourceNumbering(true); dstDoc.appendDocument(srcDoc, ImportFormatMode.USE_DESTINATION_STYLES, importFormatOptions); dstDoc.updateListLabels();
Example:
Shows how to import a document with numbered lists.
Document srcDoc = new Document(getMyDir() + "List source.docx");
Document dstDoc = new Document(getMyDir() + "List destination.docx");
Assert.assertEquals(2, dstDoc.getLists().getCount());
ImportFormatOptions options = new ImportFormatOptions();
// If there is a clash of list styles, apply the list format of the source document.
// Set the "KeepSourceNumbering" property to "false" to not import any list numbers into the destination document.
// Set the "KeepSourceNumbering" property to "true" import all clashing
// list style numbering with the same appearance that it had in the source document.
options.setKeepSourceNumbering(isKeepSourceNumbering);
dstDoc.appendDocument(srcDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING, options);
dstDoc.updateListLabels();
if (isKeepSourceNumbering)
Assert.assertEquals(3, dstDoc.getLists().getCount());
else
Assert.assertEquals(2, dstDoc.getLists().getCount());Example:
Shows how to resolve list numbering clashes in source and destination documents.
// Open a document with a custom list numbering scheme, and then clone it.
// Since both have the same numbering format, the formats will clash if we import one document into the other.
Document srcDoc = new Document(getMyDir() + "Custom list numbering.docx");
Document dstDoc = srcDoc.deepClone();
// When we import the document's clone into the original and then append it,
// then the two lists with the same list format will join.
// If we set the "KeepSourceNumbering" flag to "false", then the list from the document clone
// that we append to the original will carry on the numbering of the list we append it to.
// This will effectively merge the two lists into one.
// If we set the "KeepSourceNumbering" flag to "true", then the document clone
// list will preserve its original numbering, making the two lists appear as separate lists.
ImportFormatOptions importFormatOptions = new ImportFormatOptions();
importFormatOptions.setKeepSourceNumbering(keepSourceNumbering);
NodeImporter importer = new NodeImporter(srcDoc, dstDoc, ImportFormatMode.KEEP_DIFFERENT_STYLES, importFormatOptions);
for (Paragraph paragraph : srcDoc.getFirstSection().getBody().getParagraphs()) {
Node importedNode = importer.importNode(paragraph, true);
dstDoc.getFirstSection().getBody().appendChild(importedNode);
}
dstDoc.updateListLabels();
if (keepSourceNumbering) {
Assert.assertEquals(
"6. Item 1\r\n" +
"7. Item 2 \r\n" +
"8. Item 3\r\n" +
"9. Item 4\r\n" +
"6. Item 1\r\n" +
"7. Item 2 \r\n" +
"8. Item 3\r\n" +
"9. Item 4", dstDoc.getFirstSection().getBody().toString(SaveFormat.TEXT).trim());
} else {
Assert.assertEquals(
"6. Item 1\r\n" +
"7. Item 2 \r\n" +
"8. Item 3\r\n" +
"9. Item 4\r\n" +
"10. Item 1\r\n" +
"11. Item 2 \r\n" +
"12. Item 3\r\n" +
"13. Item 4", dstDoc.getFirstSection().getBody().toString(SaveFormat.TEXT).trim());
}getMergePastedLists/setMergePastedLists | |
public boolean getMergePastedLists() / public void setMergePastedLists(boolean value) | |
false.
Example:
Shows how to merge lists from a documents.
Document srcDoc = new Document(getMyDir() + "List item.docx");
Document dstDoc = new Document(getMyDir() + "List destination.docx");
ImportFormatOptions options = new ImportFormatOptions(); { options.setMergePastedLists(true); }
// Set the "MergePastedLists" property to "true" pasted lists will be merged with surrounding lists.
dstDoc.appendDocument(srcDoc, ImportFormatMode.USE_DESTINATION_STYLES, options);
dstDoc.save(getArtifactsDir() + "Document.MergePastedLists.docx");getSmartStyleBehavior/setSmartStyleBehavior | |
public boolean getSmartStyleBehavior() / public void setSmartStyleBehavior(boolean value) | |
false.
When this option is enabled, the source style will be expanded into a direct attributes inside a
destination document, if
When this option is disabled, the source style will be expanded only if it is numbered. Existing destination attributes will not be overridden, including lists.
Example:
Shows how to resolve duplicate styles while inserting documents.
Document dstDoc = new Document();
DocumentBuilder builder = new DocumentBuilder(dstDoc);
Style myStyle = builder.getDocument().getStyles().add(StyleType.PARAGRAPH, "MyStyle");
myStyle.getFont().setSize(14.0);
myStyle.getFont().setName("Courier New");
myStyle.getFont().setColor(Color.BLUE);
builder.getParagraphFormat().setStyleName(myStyle.getName());
builder.writeln("Hello world!");
// Clone the document and edit the clone's "MyStyle" style, so it is a different color than that of the original.
// If we insert the clone into the original document, the two styles with the same name will cause a clash.
Document srcDoc = dstDoc.deepClone();
srcDoc.getStyles().get("MyStyle").getFont().setColor(Color.RED);
// When we enable SmartStyleBehavior and use the KeepSourceFormatting import format mode,
// Aspose.Words will resolve style clashes by converting source document styles.
// with the same names as destination styles into direct paragraph attributes.
ImportFormatOptions options = new ImportFormatOptions();
options.setSmartStyleBehavior(true);
builder.insertDocument(srcDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING, options);
dstDoc.save(getArtifactsDir() + "DocumentBuilder.SmartStyleBehavior.docx");