[DocumentTemplate] add calculated page number

0
Hi,   I am creating a pdf using Document template and I would like to add a page number over a total page count but the page number should be increased of 1 and the total page count of 5   Is it possible to achieve this ? I have tryed  many combination with no success : Page [%pageNumber% + 1] of [%totalPageCount% + 5]     Thanks a lot
asked
3 answers
0

Hi,

I could achieve adding page numbers with a java action 

page-number-with-javaaction.png

 

Here is the code of the javaaction:

// This file was generated by Mendix Studio Pro.
//
// WARNING: Only the following code will be retained when actions are regenerated:
// - the import list
// - the code between BEGIN USER CODE and END USER CODE
// - the code between BEGIN EXTRA CODE and END EXTRA CODE
// Other code you write will be lost the next time you deploy the project.
// Special characters, e.g., é, ö, à, etc. are supported in comments.

package esperanto.actions;

import com.mendix.systemwideinterfaces.core.IContext;
import com.mendix.webui.CustomJavaAction;
import com.mendix.systemwideinterfaces.core.IMendixObject;
import com.mendix.core.Core;
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.ByteArrayOutputStream;
import java.io.ByteArrayInputStream;
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;
import com.mendix.systemwideinterfaces.core.IEntityProxy;
import org.apache.pdfbox.text.PDFTextStripper;

public class JA_Offer_DistributorPdf_ReplaceTokens extends CustomJavaAction<IMendixObject>
{
	/** @deprecated use InputFileDocument.getMendixObject() instead. */
	@java.lang.Deprecated(forRemoval = true)
	private final IMendixObject __InputFileDocument;
	private final system.proxies.FileDocument InputFileDocument;

	public JA_Offer_DistributorPdf_ReplaceTokens(
		IContext context,
		IMendixObject _inputFileDocument
	)
	{
		super(context);
		this.__InputFileDocument = _inputFileDocument;
		this.InputFileDocument = _inputFileDocument == null ? null : system.proxies.FileDocument.initialize(getContext(), _inputFileDocument);
	}

	@java.lang.Override
	public IMendixObject executeAction() throws Exception
	{
		// BEGIN USER CODE
		InputStream inputStream = Core.getFileDocumentContent(getContext(), InputFileDocument.getMendixObject());
		PDDocument document = PDDocument.load(inputStream);
        int totalPages = document.getNumberOfPages();
		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 - 60;
            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();
		}
		// Write the content to an InputStream
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
		document.save(outputStream);
		document.close();
		InputStream inputStreamResult = new ByteArrayInputStream(outputStream.toByteArray());
		
		// Create a new FileDocument object
        IMendixObject fileDocument = Core.instantiate(getContext(), "System.FileDocument");		
		
		// Store the content in the FileDocument
		Core.storeFileDocumentContent(getContext(), fileDocument, "offer.pdf", inputStreamResult);
		return fileDocument;
		// END USER CODE
	}

	/**
	 * Returns a string representation of this action
	 * @return a string representation of this action
	 */
	@java.lang.Override
	public java.lang.String toString()
	{
		return "JA_Offer_DistributorPdf_ReplaceTokens";
	}

	// BEGIN EXTRA CODE
	// END EXTRA CODE
}

 

answered
0

other way, use Docmosis module any way we need an account and I prefer java because it is free

answered
-1

Hi Sebastian,

     Mendix does not support dynamic arithmetic operations within the built-in placeholders like [%pageNumber% + 1] and [%totalPageCount% + 5] in Document Templates. Try adding attributes and the set the values in microflows and add a placeholder in document template

answered