Free Support Forum - aspose.com

Put several copies in one document

Hello!

I need to write a document to another document several times (to have several copies). Currently we have the following realization:

private Document processCopies(Document document, Integer copies) {
    if (copies == null || copies <= 1) {
        return document;
    }
    List<Document> documents = new ArrayList<>();
    //TODO: there is an empty page in the beginning, to think how to avoid it
    documents.add(new Document());//adding an empty doc in the beginning, copying of the doc to itself leads to freezing Aspose
    for(int i=0; i<copies; i++) {
        documents.add(document);
    }
    return mergeDocuments(documents);
}

private Document mergeDocuments(Iterable<Document> documents) {
    Iterator<Document> iterator = documents.iterator();
    Document document = iterator.next();

    while (iterator.hasNext()) {
        document.appendDocument(iterator.next(), ImportFormatMode.USE_DESTINATION_STYLES);
        document.getLastSection().getHeadersFooters().linkToPrevious(false);
    }
    return document;
}

As a result of that, we get the final document with not only initial document copies, but also with an empty first page.
Is there any other more convenient way to put several copies of the doc to a new one?

Many thanks!

@rubcs

Thanks for your inquiry. We suggest you please read the following article.
Joining Documents

You can use DocumentBuilder.InsertDocument method to insert the content of the document into the current position of DocumentBuilder’s cursor.

You are getting the first page empty because the section break type is NEW_PAGE. Please use PageSetup.SectionStart property to set it as SectionStart.CONTINUOUS. Please check the code example shared in the following link.
Specifying the Source Document to Flow Continuously or Start from a New Page

Please let us know if you have any more queries.