com.aspose.words

Class ImportFormatOptions

  • java.lang.Object
    • com.aspose.words.ImportFormatOptions
public class ImportFormatOptions 
extends java.lang.Object

Allows to specify various import options to format output.

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");

Constructor Summary
 
Property Getters/Setters Summary
booleangetIgnoreHeaderFooter()
void
setIgnoreHeaderFooter(booleanvalue)
           Gets or sets a boolean value that specifies that source formatting of headers/footers content ignored if ImportFormatMode.KEEP_SOURCE_FORMATTING mode is used. The default value is true.
booleangetIgnoreTextBoxes()
void
setIgnoreTextBoxes(booleanvalue)
           Gets or sets a boolean value that specifies that source formatting of textboxes content ignored if ImportFormatMode.KEEP_SOURCE_FORMATTING mode is used. The default value is true.
booleangetKeepSourceNumbering()
void
           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.
booleangetSmartStyleBehavior()
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.
 

    • Constructor Detail

      • ImportFormatOptions

        public ImportFormatOptions()
    • Property Getters/Setters Detail

      • getIgnoreHeaderFooter/setIgnoreHeaderFooter

        public boolean getIgnoreHeaderFooter() / public void setIgnoreHeaderFooter(boolean value)
        
        Gets or sets a boolean value that specifies that source formatting of headers/footers content ignored if ImportFormatMode.KEEP_SOURCE_FORMATTING mode is used. The default value is 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)
        
        Gets or sets a boolean value that specifies that source formatting of textboxes content ignored if ImportFormatMode.KEEP_SOURCE_FORMATTING mode is used. The default value is 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)
        
        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.

        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 together.
        // 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());
        }
      • getSmartStyleBehavior/setSmartStyleBehavior

        public boolean getSmartStyleBehavior() / public 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.

        When this option is enabled, the source style will be expanded into a direct attributes inside a destination document, if ImportFormatMode.KEEP_SOURCE_FORMATTING importing mode is used.

        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");