Expand my Community achievements bar.

Getting total page count from pdf created with interactive communication

Avatar

Level 2

Hello,

 

I am generating pdf with bathc API . I need to get also total page number created pdf. 

 

BatchInput batchInput = batchBuilderFactory.getBatchInputBuilder().setData(inputJSONArray).setTemplatePath(templatePath).build();
BatchConfig batchConfig = batchBuilderFactory.getBatchConfigBuilder().setBatchType(BatchType.PRINT).build();

BatchResult batchResult = batchGeneratorService.generateBatch(batchInput, batchConfig);

RecordResult recordResult = checkOneElement(batchResult.getRecordResults(), "generated record");
RenditionResult renditionResult = checkOneElement(recordResult.getRenditionResults(), "generated result");   

 

 

I tried to get PAGE_COUNT of Document  object but is null, i also checked if rendition result has some meta data but it is null also.

 

Document doc = renditionResult.getDocumentStream();

log.info("doc PAGE_COUNT [{}]", doc.getAttribute("PAGE_COUNT"));


if (renditionResult.getMetadata() != null) {
for (Entry<String, Object> e : renditionResult.getMetadata().entrySet()) {
log.info("Entry {} : {}", e.getKey(), e.getValue());
}
}

  

Is there any meta data or api for getting total number of page for pdf please?

6 Replies

Avatar

Community Advisor

It seems like you’re trying to get the total number of pages in a PDF document. Unfortunately, the batch API you’re using might not provide this feature directly. However, there are a few workarounds you can consider:

  1. Using PDF.js: If you’re open to using JavaScript, the pdf.js library provides a way to get the number of pages in a PDF file.
    https://stackoverflow.com/questions/10253669/how-to-get-the-number-of-pages-of-a-pdf-uploaded-by-use...
    Here’s a sample code snippet in javascript&colon;
 
const pdfjsLib = require('pdfjs-dist');
...
pdfjsLib.getDocument(pdfPath).then(function (doc) {
    var numPages = doc.numPages;
    console.log('# Document Loaded');
    console.log('Number of Pages: ', numPages);
});

2. Using Power Platform: If you’re using Microsoft Power Platform, you can get the file content of a PDF file and then create a compose action with an expression that returns the count of pages within the PDF file
https://powerusers.microsoft.com/t5/Building-Flows/Number-of-pages-in-a-PDF-file/td-p/811707

Avatar

Level 2

Thanks @pulkitvashisth ,

 

Dou you know if any other aem Api can get get page number from Document or InputStream?

 

Avatar

Community Advisor

Hi @hugo_wuse 
You can also probably try out the Apache PDFBox library.

import org.apache.pdfbox.pdmodel.PDDocument;

public class GetPageCount {
    public static void main(String[] args) {
        try {
            PDDocument document = PDDocument.load(new File("path/to/your/document.pdf"));
            int numberOfPages = document.getNumberOfPages();
            System.out.println("The PDF document has " + numberOfPages + " pages.");
            document.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Import dependency for the same:
 
<dependency>
    <groupId>org.apache.pdfbox</groupId>
    <artifactId>pdfbox</artifactId>
    <version>2.0.24</version>
</dependency>
You can update the version number based on compatibility.

Avatar

Level 2

I could not find a solution for getting total page number of generated pdf with batch Api.  The answer given by @pulkitv56841125 works but this solution is not what i need.  I dont understand how batch Api can not give the number of page for un generated pdf. Thsi is a basic futur. 

Avatar

Administrator

@hugo_wuse Did you find the suggestions from users helpful? Please let us know if more information is required. Otherwise, please mark the answer as correct for posterity. If you have found out solution yourself, please share it with the community.



Kautuk Sahni