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 |
---|
Property Getters/Setters Summary | ||
---|---|---|
boolean | getIgnoreHeaderFooter() | |
void | setIgnoreHeaderFooter(booleanvalue) | |
Gets or sets a boolean value that specifies that source formatting of headers/footers content ignored
if true .
|
||
boolean | getIgnoreTextBoxes() | |
void | setIgnoreTextBoxes(booleanvalue) | |
Gets or sets a boolean value that specifies that source formatting of textboxes content ignored
if true .
|
||
boolean | getKeepSourceNumbering() | |
void | setKeepSourceNumbering(booleanvalue) | |
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 | getSmartStyleBehavior() | |
void | setSmartStyleBehavior(booleanvalue) | |
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 .
|
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");
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");
public boolean getKeepSourceNumbering() / public void setKeepSourceNumbering(boolean value)
false
.
Example:
Shows how the numbering will be imported when it clashes in source and destination documents.// Open a document with a custom list numbering scheme and 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(); // Both documents have the same numbering in their lists, but if we set this flag to false and then import one document into the other // the numbering of the imported source document will continue from where it ends in the destination document ImportFormatOptions importFormatOptions = new ImportFormatOptions(); importFormatOptions.setKeepSourceNumbering(false); NodeImporter importer = new NodeImporter(srcDoc, dstDoc, ImportFormatMode.KEEP_DIFFERENT_STYLES, importFormatOptions); for (Paragraph paragraph : (Iterable<Paragraph>) srcDoc.getFirstSection().getBody().getParagraphs()) { Node importedNode = importer.importNode(paragraph, true); dstDoc.getFirstSection().getBody().appendChild(importedNode); } dstDoc.updateListLabels(); dstDoc.save(getArtifactsDir() + "NodeImporter.KeepSourceNumbering.docx");
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");