Merge front and back page templates

I have successfully merged a C# List to a .dotx template producing a multi-page output of ‘front’ pages with the correctly merged fields.

However, each of the front page should be followed by a standard, static reverse/back page.

What would be the best way to programmatically achieve this in C#, please?

The final output should be:

Let’s call the front page merges ‘Page’ and the reverse/back template ‘Back’

Page 1, Back, Page 2, Back, Page 3, Back, Page 4, Back and so on…

Hi,

Unfortunately, I’m not exactly sure what you mean by that, what is “static reverse/back page”?
Anyway, can you send us your DOTX file and the multi-page output file that you generate?

There is a way for you to detect where the new page begins, but that calculation comes from GemBox.Document’s rendering engine which might be slightly different from Microsoft Word’s rendering engine.

Each Word application uses its own rendering engine so they all may have slight differences, you can think of this the same as how different browsers will render the same website slightly differently.

So what I’m trying to say is that adding those “Back” pages between the “Front” pages can be done accurately for PDF output, but for Word output, it will depend on the template document.

Regards,
Mario

Hi Mario, thank you for responding. By static back page I mean that it has no merge fields on it, only pre-written text.

What you said in your last paragraph is exactly what I’m trying to achieve. Once the merge is complete, I have a multi-page merged doc of ‘front’ data pages. I then need to add the back page in between each of these pages and output as PDF. The back page can either be a Word doc or PDF doc, whatever will be easier to achieve the output.

Many thanks.

That is not what I was asking, I’m asking if this “Page 1, Back, Page 2, Back, …” document is going to be saved to DOCX or PDF format?

Again, can you please send us your DOTX file and the multi-page output file that you generate?

As mentioned before, the problem is how to find where the automatic page break will occur. You can think of this like following, let’s say you have a website, an HTML, and you want to find out what content is on what page when you print it, that is the exact scenario you have.

You see, Word documents themselves do not have a page concept, they are of a flow document type, the page concept is specific to a Word application(s) that is rendering it. The flow document types (DOC, DOCX, RTF, HTML, etc. formats) define content in a flow-able manner and when you open these documents in some application it must paginate and render its content (there can actually be differences in rendering between different applications).

On the other hand, the fixed document types (like PDF, XPS, etc. formats) do have a page concept because the content is fixed, it is defined on which page and on which page’s location some specific content will be rendered. This is why this document is rendered in any application the same.

So in short, the problem is if/how we could detect the page breaks in your “multi-page output of ‘front’ pages” document. For me to answer that I will need to investigate your document.

Hi Mario, unfortunately I can’t post the document(s) as they contain personal and business information.

From your response, if it’s easier to achieve my desired output, I can create the multi-page document of ‘front’ pages as a .pdf and also have the ‘back’ page as a .pdf, then merge the output as a new .pdf.

The merge of the two should be front page 1, back, front page 2, back, front page 3, back, front page 4, back etc etc.

Hi,

In that case, you can save each page of your multi-page document as a separate PDF file with one page, see Convert Word pages to PDF.

For merging those pages, you’ll need to use GemBox.Pdf, see Merge Files example.

In other words, try something like this:

var document = DocumentModel.Load("template.dotx");

// Fill out the template document...

using var backPdf = PdfDocument.Load("back.pdf");
// Or in case "back" document is provided as DOCX file:
/* var backStream = new MemoryStream();
 * DocumentModel.Load("back.docx").Save(backStream, GemBox.Document.SaveOptions.PdfDefault);
 * using var backPdf = PdfDocument.Load(backStream); */

using var resultPdf = new PdfDocument();

var documentPages = document.GetPaginator().Pages;
for (int pageIndex = 0; pageIndex < documentPages.Count; pageIndex++)
{
    // Save document's page to PDF.
    var documentPage = documentPages[pageIndex];
    var pdfStream = new MemoryStream();
    documentPage.Save(pdfStream, GemBox.Document.SaveOptions.PdfDefault);

    // Add document's page to resulting PDF.
    using var pdfPage = PdfDocument.Load(pdfStream);
    resultPdf.Pages.AddClone(pdfPage.Pages[0]);

    // Add "back" document's page to resulting PDF.
    resultPdf.Pages.AddClone(backPdf.Pages[0]);
}

// Remove last added "back" document's page.
resultPdf.Pages.RemoveAt(resultPdf.Pages.Count - 1);

resultPdf.Save("Output.pdf");

Regards,
Mario

Worked a treat - thanks a lot for your help, Mario!