Problem merging two .doc templates (header + content)

Hi everybody,

I have a problem while trying to merge two documents.

I have attached three files:
- header_sgi.doc, which contains page header and footer, and no content
- frontespizio_a.doc, which contains the content of the document and absolutely NO header/footer
- merged.doc, which is the result of mailmerge on the two templates and then merging together

Document merging is done like this:

public static Document merge(Document[] documents, int sectionStart, int formatting) throws Exception {
    Document merged = new Document();
    merged.removeAllChildren();

    for (int i = 0; i < documents.length; ++i) {
        documents[i].getFirstSection().getPageSetup().setSectionStart(sectionStart);
        merged.appendDocument(documents[i], formatting);
    }

    return merged;
}

But even though I call the function above with

sectionStart = SectionStart.CONTINUOUS

it gets merged like this. What am I doing wrong? Also, would it be possibile to separate header and footer in two distinct templates?

Thanks in advance for your help.

Hi Matteo,
Thanks for your inquiry. The problem occurs because some page setup settings are different across your documents causing documents to appear on new pages. To ensure this does not happen when the documents have different page setup settings; please make sure the settings are identical between the last section of the previous document and the first section of the next document. Please try running the following code snippet on your side:

public static Document merge(Document[] documents, int sectionStart, int formatting) throws Exception {
    Document merged = new Document();
    merged.removeAllChildren();
    for (int i = 0; i < documents.length; ++i) {
        PageSetup ps = documents[0].getLastSection().getPageSetup();
        PageSetup tempPs = documents[i].getFirstSection().getPageSetup();
        tempPs.setPageWidth(ps.getPageWidth());
        tempPs.setPageHeight(ps.getPageHeight());
        tempPs.setOrientation(ps.getOrientation());
        tempPs.setSectionStart(sectionStart);
        merged.appendDocument(documents[i], formatting);
    }
    return merged;
}

I hope, this helps.
Best regards,

Thanks for your answer.

However, the two templates were created exactly with this purpose in mind, that is, to be mergeable, in fact they have the very same setup, as you could see, so why is that forcing page width/height/orientation necessary?

EDIT: I just tried your solutions, it did not work, there is still an empty page at the beginning.

EDIT 2: my mistake! I was calling the “merge” method with the wront sectionStart parameter…

EDIT 3:

ok, I’ve tried some more times, and I’ve been able to obtain some results but there are still some problems.

I’ve attached a test I made with that header template merged with some other content, it works but it also places an inconvenient section break at the very beginning of the document, is there a way to get rid of it, since it adds quite some space on the top?

Hi Matteo,

Thanks for your inquiry. In your case, removing the last empty Paragraph from the HeaderPrimary will reduce a bit of empty vertical space.

Document doc = new Document(@"C:\Temp\Report_mod_98_47.doc");
doc.FirstSection.HeadersFooters[HeaderFooterType.HeaderPrimary].LastParagraph.Remove();
doc.Save(@"C:\Temp\out.doc");

Best regards,

That will certainly help in reducing the vertical spacing a bit, also that last paragraph is not removeable using word itself.

However I’d much prefer to avoid/remove the first section break if possibile, since that adds much more space than that paragraph.

Hi Matteo,

Thanks for the additional information. I am afraid, you can’t remove the only Paragraph containing the first secion break too. However, you can further reduce the space by reducing the Font size as follows:

doc.FirstSection.Body.FirstParagraph.ParagraphBreakFont.Size = 1;

I hope, this helps.

Best regards,

Ok, your suggestion solved my problem, in fact setting the section break size to 1 made it nearly disappear, thank you!

Hi Matteo,

Thanks for your feedback. It’s great you were able to achieve what you were looking for. Please let us know any time you have any further queries.

Best regards,