Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.

How to Count the total number of pages in PDF attachment ?

Avatar

Level 1

Hi All,

We have a requirement to count the total number of pages in PDF attachment which we are uploading through our PDF form.

I tried to use "numPages" in the "getDataObject" response but the output is coming as "undefined" .

Please help me in this regards

Thanks

Harshit

2 Replies

Avatar

Level 10

Hi,

you have to open each attachment to determine its page count.

Here's a script you can use in a button event.


var that = event.target,


  pageCount = 0,


    isPDF = 0,


    attmt = that.dataObjects,


    attmtPath = "",


    attmtDoc;


if (attmt !== null) {


  for (var i = 0, j = attmt.length; i < j; i += 1) {


    if (attmt[i].MIMEType === "application/pdf") {


        isPDF += 1;


        attmtDoc = that.openDataObject(attmt[i].name);


        pageCount += attmtDoc.numPages;


        attmtDoc.closeDoc();


    }  


  }


  if (isPDF === 0) {


    xfa.host.messageBox("There are file attached to this file, but none is a PDF.", "No PDF found", 0, 0);


  } else {


      xfa.host.messageBox("There " + (isPDF === 1 ? "is ":"are ") + isPDF.toString() + (isPDF === 1 ? " PDF file ":" PDF files ") + "attached to this file.\nThe total page count is: " + pageCount + " pages.", "Page Count of attached PDFs", 0, 0);


  }


} else {


  xfa.host.messageBox("No files attached to this file.", "No attachments found", 0, 0);


}


Avatar

Level 1

Thanks Radzmar,

The code is working fine. we are able to get the total number of pages through this code.

But, the pdf is getting crashed because of closeDoc() function.

Please help.

Thanks in advance