Mendix Document Template - Dynamic Page Numbers

0
We have created multiple document templates (as we can’t place one document template inside another one  like parent-child) and at the end of the process, we are exporting them to PDF and using Community Common MergePDF java action merging them into one PDF.   The issue is – Each document template has page number and when we are generating multiple PDF, we are getting the same page number starting from 1.   Is there any way, we can use the page number continuation in multiple documents or dynamic page numbers?
asked
2 answers
5

You could leave out the page numbers on the documents, then merge the documents and wrtie a simpe java action that adds the pagenumbers afterwards.

An example in pure java is added below, with some small tweaks you should be able to create the Mendix java action for this using the Apache PDFBox library:

import java.io.File;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDType1Font;

public class AddPageNumbers {

    public static void main(String[] args) {
        try {
            // Load an existing PDF document
            File file = new File("existing_document.pdf");
            PDDocument document = PDDocument.load(file);

            // Get the total number of pages
            int totalPages = document.getNumberOfPages();

            // Iterate through all pages
            for (int i = 0; i < totalPages; i++) {
                PDPage page = document.getPage(i);

                // Start a new content stream which will "hold" the to be created content
                PDPageContentStream contentStream = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.APPEND, true);

                // Define the font to use
                PDFont font = PDType1Font.HELVETICA;

                // Set the font size
                float fontSize = 10;

                // Calculate x and y coordinates for the page number
                float x = page.getMediaBox().getWidth() - fontSize - 5;
                float y = fontSize + 5;

                // Begin the Content stream
                contentStream.beginText();

                // Setting the font to the Content stream
                contentStream.setFont(font, fontSize);

                // Setting the position for the line
                contentStream.newLineAtOffset(x, y);

                // Adding text
                String text = String.format("Page %d of %d", i+1, totalPages);
                contentStream.showText(text);

                // Ending the content stream
                contentStream.endText();

                // Closing the content stream
                contentStream.close();
            }

            // Save the modified document
            document.save("modified_document.pdf");

            // Close the document
            document.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Additonal error handling might be needed depending on your solution in the java action.

Hope this helps in finding a solution for your requiement.

answered
0

how to connect Mendix and Apache?

answered