Appending a document on the same page

Hi everybody!

I was trying to create a document by appending several documents to an empty one (just created with the new Document() constructor), but when I append the other documents to this empty one, even if I set the SectionStart.CONTINUOUS flag in the documents being appended, the first append is on a new page, while the next ones are correctly continuous.

How can I obtain that the first append also is continuous?

Hi

Thanks for your request. I think in your case, you can just remove first section from the empty document. For example you can try using code like the following:

// Open documents
Document doc1 = new Document("C:\\Temp\\in1.doc");
Document doc2 = new Document("C:\\Temp\\in2.doc");
Document doc3 = new Document("C:\\Temp\\in3.doc");
// Create an empty document
Document mainDoc = new Document();
mainDoc.removeAllChildren();
// Append document to the main document
mainDoc.appendDocument(doc1, ImportFormatMode.KEEP_SOURCE_FORMATTING);
mainDoc.appendDocument(doc2, ImportFormatMode.KEEP_SOURCE_FORMATTING);
mainDoc.appendDocument(doc3, ImportFormatMode.KEEP_SOURCE_FORMATTING);
// Save output document
mainDoc.save("C:\\Temp\\out.doc")

Hope this helps.
Best regards.

That worked! Thank you!

I’ve noticed that sometimes the library seems to completely ignore the SectionStart flag. Take for example this code:

com.aspose.words.Document empty = new com.aspose.words.Document();
empty.removeAllChildren();
com.aspose.words.Document doc = new com.aspose.words.Document(pathAmbiente + "DVR/header.doc");
fillHeader(doc, request, response);
doc.getFirstSection().getPageSetup().setSectionStart(SectionStart.CONTINUOUS);
com.aspose.words.Document doc2 = new com.aspose.words.Document(pathAmbiente + "DVR/Report_mod_" + xmlNomeFile + ".doc");
doc2.getFirstSection().getPageSetup().setSectionStart(SectionStart.CONTINUOUS);
empty.appendDocument(doc, ImportFormatMode.KEEP_SOURCE_FORMATTING);
empty.appendDocument(doc2, ImportFormatMode.KEEP_SOURCE_FORMATTING);
empty.save("C:/Report_mod_" + xmlNomeFile + ".doc");

“empty” is a completely empty document, “doc” opens a document which contains only an header which is filled in the fill header method and lastly “doc2” is a full document without header or footer. Even though I’ve set the CONTINUOUS flag for the SectionStart parameter, and even though the “empty” document is really empty, the appendDocument method creates a document with a first blank page which contains only the header.

Why does this happens? Am I mistaking something? The same solution worked on another program!

Hi

Thanks for your inquiry. This might occur because sections have different paper size. As a workaround, you can try setting the same page size of sections.
Best regards.

How do I do that? Since doc2 is a document generated using Aspose.Words for JasperReports, I’ve set the file containing the header to have no margin, just like the documents generated by your jasperreports library.

Hello!
Thank you for your experiments.
My colleague is right. Documents can be concatenated continuously, without page breaks, if only page setup of the sections is the same. You can compare/manipulate with page setup via this class:
https://reference.aspose.com/words/java/com.aspose.words/pagesetup/
I’ll also consult with the developer of Aspose.Words for JasperReports on this case. He might help us.
If you experience difficulties then please attach the DOC files you are trying to concatenate here in the forum. I’ll try myself.
Regards,

Here is an archive containing the header document and the one generated using jasperreports.

Hi again!
Thank you for providing these materials. I have tried concatenating and got the same result: document with report goes to the next page in spite of the fact that Microsoft Word shows section start from the same page. As I inspected this occurs because page width slightly differs. In the first document it is 21cm, but in the second one it is 20.99cm. You can see this in Microsoft Word using Show Formatting facility or fetch these values programmatically.
To work-around the problem you can copy size form the second section to the first one before you save:

empty.getSections().get(1).getPageSetup().setPageWidth(empty.getSections().get(0).getPageSetup().getPageWidth());

Maybe in some cases you’ll need the same for height:

empty.getSections().get(1).getPageSetup().setPageHeight(empty.getSections().get(0).getPageSetup().getPageHeight());

I will ask responsible developer why these values might vary in Aspose.Words for JasperReports.
Regards,

Thank you for the information. I’ll try the workaround right away!

Aspose.Words for JasperReports produces pages whose size is exactly equal to the designed one. That is, it cannot vary unless the input size varies. So just make sure page size (as well as other section properties) are the same when concatenating documents.
BTW, starting with the version 1.2.0 that is being released, documents will contain section breaks, not page breaks, because we support headers/footers from now on and we need to place them into different sections. Maybe I will add an option to control this in the future.
Thanks.

I’d like to resurrect this topic to ask a new question about appending documents: I’ve two template documents filled using mail merge, which I append to a third completely empty document.

The two templates have the “Calibri” font, but when I open the result of the append, it has been transformed to “Times New Roman”. I’ve tried to use both KEEP_SOURCE_FORMATTING and USE_DESTINATION_STYLE as ImportFormatMode for the append, but the result remains the same.

What am I doing wrong?

Hello

Thanks for your request. I cannot reproduce the problem on my side using the latest version of Aspose.Words (9.1.0). Could you please attach the input and the output documents and the code which will allow me to reproduce the problem on my side?
Best regards,

I’m attaching a .zip file containing 3 files: test2.doc and sub1.doc are the two templates I mentioned, while report_c.doc is the result after the merge.

As you can see, both templates are written using Calibri font, while the result has the Times New Roman font.

BTW, I use Aspose.Words for Java versione 4.0.2 beta.

Hello

Thanks for additional information. I cannot reproduce the problem on my side using the latest version of Aspose.Words for Java (4.0.2). I use the following code for testing:

Document dst = new Document("C:\\Temp\\sub1.doc");
Document src = new Document("C:\\Temp\\test2.doc");
// Set SectionStart to Continuous
src.getFirstSection().getPageSetup().setSectionStart(SectionStart.CONTINUOUS);
dst.appendDocument(src, ImportFormatMode.KEEP_SOURCE_FORMATTING);
dst.save("C:\\Temp\\out.doc");

Best regards,

here is my own piece of code which does the merging of the merge fields and then the appending:

Document report = new Document();
report.removeAllChildren();

Statement s = conn.createStatement();
String q = "SELECT descrizione_edificio, descrizione_piano, descrizione_vano, id_vano FROM lin_98_6_vani WHERE id_azienda="+request.getParameter("id_azienda")+
        " AND id_sede=" + request.getParameter("id_sede") + " AND revisione_documento=" + request.getParameter("revisione_documento");

ResultSet r = s.executeQuery(q);
while (r.next())
{
    Document tmp2 = new Document(testDir + "test2.doc");
    tmp2.getMailMerge().execute(new String[]
                    {
                            "descrizione_edificio",
                            "descrizione_piano",
                            "descrizione_vano"
                    },
            new Object[]
                    {
                            r.getObject("descrizione_edificio"), r.getObject("descrizione_piano"), r.getObject("descrizione_vano")
                    }
    );

    Statement s2 = conn.createStatement();
    q = "SELECT IF(data_rimozione IS NOT NULL,‘Rimosso’,‘Installato’) as status, nome, IF(size_x IS NOT NULL,CONCAT(size_x,’ x ',size_y),‘N.D’) as dimensioni," +
            " materiale, installazione, quantita FROM lin_98_6_sgs_segnali WHERE id_azienda=" + request.getParameter("id_azienda") +
            " AND id_sede=" + request.getParameter("id_sede") + " AND revisione_documento=" + request.getParameter("revisione_documento") +
            " AND id_vano=" + r.getString("id_vano");
    ResultSet r2 = s2.executeQuery(q);
    if (r2.next())
    {
        r2.beforeFirst();
        Document tmp3 = new Document(testDir + "sub1.doc");
        tmp3.getMailMerge().executeWithRegions("segnali", r2);
        tmp3.getFirstSection().getPageSetup().setSectionStart(SectionStart.CONTINUOUS);
        tmp2.appendDocument(tmp3, ImportFormatMode.KEEP_SOURCE_FORMATTING);
    }

    tmp2.getFirstSection().getPageSetup().setSectionStart(SectionStart.CONTINUOUS);
    report.appendDocument(tmp2, ImportFormatMode.KEEP_SOURCE_FORMATTING);
}

report.save(testDir + "report_c.doc");

Could the problem be due to the fact that the two documents are merged into a completely empty one?

Hello

Thanks for your inquiry. I still cannot reproduce the problem. Could you please create simple application, which will allow me to reproduce the problem on my side? I will investigate the issue and provide you more information.
Best regards,

At this moment I really haven’t the time to create a sort of sandbox… If I may ask, have you tried doing a simple merge with fixed values to fill the mergefield? I think it might be that operation which somehow modifies the font…

Hello

Thank you for additional information. Yes, I have tried executing mail merge and mail merge with regions, but still cannot reproduce the problem:

Document dst = new Document("C:\\Temp\\sub1.doc");
Document src = new Document("C:\\Temp\\test2.doc");
// Get ResultSet
ResultSet testTable = GetTestTable();
dst.getMailMerge().executeWithRegions("segnali", testTable);
String[] fields = {
    "descrizione_edificio",
    "descrizione_piano",
    "descrizione_vano"
};
String[] values = {
    "value1",
    "value2",
    "value3"
};
src.getMailMerge().execute(fields, values);
// Set SectionStart to Continuous
src.getFirstSection().getPageSetup().setSectionStart(SectionStart.CONTINUOUS);
dst.appendDocument(src, ImportFormatMode.KEEP_SOURCE_FORMATTING);
dst.save("C:\\Temp\\out.doc");

Best regards,