Getting total page count from pdf created with interactive communication | Community
Skip to main content
Level 2
January 11, 2024

Getting total page count from pdf created with interactive communication

  • January 11, 2024
  • 2 replies
  • 3245 views

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?

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.

2 replies

pulkitvashisth
Community Advisor
Community Advisor
January 11, 2024

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-user
    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

hugo_wuseAuthor
Level 2
January 11, 2024

Thanks @pulkitvashisth ,

 

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

 

pulkitvashisth
Community Advisor
Community Advisor
January 11, 2024

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.

kautuk_sahni
Community Manager
Community Manager
January 15, 2024

@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