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 styles behavior 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);

// Append text with custom style
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 append this document to the original, the different styles will clash since they are the same name, and we will need to resolve it
Document srcDoc = dstDoc.deepClone();
srcDoc.getStyles().get("MyStyle").getFont().setColor(Color.RED);

// When SmartStyleBehavior is enabled,
// a source style will be expanded into a direct attributes inside a destination document,
// if KeepSourceFormatting importing mode is used
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.
      • 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 formatting in the text boxes of the source destination during the import.
        // Create a document and add text
        Document dstDoc = new Document();
        DocumentBuilder builder = new DocumentBuilder(dstDoc);
        
        builder.writeln("Hello world! Text box to follow.");
        
        // Create another document with a textbox, and insert some formatted text into it
        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.0d);
        builder.write("Textbox contents");
        
        // When we import the document with the textbox as a node into the first document, by default the text inside the text box will keep its formatting
        // Setting the IgnoreTextBoxes flag will clear the formatting during importing of the node
        ImportFormatOptions importFormatOptions = new ImportFormatOptions();
        importFormatOptions.setIgnoreTextBoxes(true);
        
        NodeImporter importer = new NodeImporter(srcDoc, dstDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING, importFormatOptions);
        
        ParagraphCollection srcParas = srcDoc.getFirstSection().getBody().getParagraphs();
        for (int i = 0; i < srcParas.getCount(); i++) {
            Paragraph srcPara = srcParas.get(i);
            Node importedNode = importer.importNode(srcPara, true);
            dstDoc.getFirstSection().getBody().appendChild(importedNode);
        }
        
        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 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_SOURCE_FORMATTING, importFormatOptions);
        
        ParagraphCollection srcParas = srcDoc.getFirstSection().getBody().getParagraphs();
        for (int i = 0; i < srcParas.getCount(); i++) {
            Paragraph srcPara = srcParas.get(i);
            Node importedNode = importer.importNode(srcPara, true);
            dstDoc.getFirstSection().getBody().appendChild(importedNode);
        }
        
        dstDoc.updateListLabels();
        dstDoc.save(getArtifactsDir() + "DocumentBuilder.KeepSourceNumbering.docx");
      • 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 styles behavior 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);
        
        // Append text with custom style
        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 append this document to the original, the different styles will clash since they are the same name, and we will need to resolve it
        Document srcDoc = dstDoc.deepClone();
        srcDoc.getStyles().get("MyStyle").getFont().setColor(Color.RED);
        
        // When SmartStyleBehavior is enabled,
        // a source style will be expanded into a direct attributes inside a destination document,
        // if KeepSourceFormatting importing mode is used
        ImportFormatOptions options = new ImportFormatOptions();
        options.setSmartStyleBehavior(true);
        
        builder.insertDocument(srcDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING, options);
        
        dstDoc.save(getArtifactsDir() + "DocumentBuilder.SmartStyleBehavior.docx");