Free Support Forum - aspose.com

Copying entire document into new document with 2 column layout

AsposeWords.zip (182.1 KB)
public static void main(String[] args) {
try {

        License license = new License();
        license.setLicense("src\\asposewords\\Aspose.Words.lic");

        Document srcDoc = new Document("src\\asposewords\\SampleTwoColumnOutput.doc");

// DocumentBuilder srcBuilder = new DocumentBuilder(srcDoc);
// SectionCollection sections = srcDoc.getSections();
// for (Iterator iterator = sections.iterator(); iterator.hasNext():wink: {
// Section sec = (Section) iterator.next();
// sec.clearHeadersFooters();
// }
//
// srcDoc.save(“src\asposewords\SampleTwoColumnOutput.doc”);

        Document destDoc = new Document();
        DocumentBuilder destBuilder = new DocumentBuilder(destDoc);
        TextColumnCollection destColumns = destBuilder.getPageSetup().getTextColumns();
        destColumns.setCount(2);
        destBuilder.writeln("1 2 ka 4");
        NodeCollection childNodes = destDoc.getChildNodes();
        CompositeNode container = childNodes.getContainer();
        Node newNode = null;
        NodeImporter importer = new NodeImporter(srcDoc.getDocument(), destDoc.getDocument(), ImportFormatMode.KEEP_SOURCE_FORMATTING);
        final NodeCollection srcParas = srcDoc.getChildNodes(NodeType.PARAGRAPH, true);
        for (Iterator<Node> iterator = srcParas.iterator(); iterator.hasNext();) {
            Node childNode = iterator.next();
            newNode = importer.importNode(childNode, true);                

// destBuilder.writeln(newNode.getText());
destBuilder.insertNode(newNode);
// container.appendChild(newNode);
}
// destBuilder.insertDocument(srcDoc, ImportFormatMode.USE_DESTINATION_STYLES);
destDoc.save(“src\asposewords\2columndocument.docx”);

    } catch (Exception ex) {
        Logger.getLogger(TwoColumnDoc.class.getName()).log(Level.SEVERE, null, ex);
    }
}

I tried different approaches to destination document with 2 column page setup

  1. documentBuilder.insertDocument - 2 column page setup is not retained.
  2. documentBuilder.insertNode(srcNode) - java.lang.IllegalArgumentException: Cannot insert a node of this type at this location.
  3. compositeNode.appendChild(srcNode) - java.lang.IllegalArgumentException: Cannot insert a node of this type at this location.

Can you point where I am going wrong.

@wrushu2004

Thanks for your inquiry. Please use the following code example to get the desired output.

Document doc = new Document(MyDir + "SampleTwoColumnInput.doc");
doc.getFirstSection().getPageSetup().getTextColumns().setCount(2);

doc.save(MyDir + "18.10.doc");