Skip to content
Advertisement

number of page of document in Java

I have the base64 and the byte[] of a pdf document, and I need to obtain from this number of page of the document how I could do it.

PdfReader reader = Here my question 
int ret = reader.getNumberOfPages();

Advertisement

Answer

Your question is not quite clear. What do you mean that you have the base64 encoded byte[]? Normally you either have a base64 encoded string or an byte[].

import java.util.Base64;

 //[...]

 String yourBase64encodedPdf = "JVBERi0xLjIgCjkgMCBvYmoKPDwKPj4Kc3RyZWFtCkJULyA5IFRmKFRlc3QpJyBFVAplbmRzdHJlYW0KZW5kb2JqCjQgMCBvYmoKPDwKL1R5cGUgL1BhZ2UKL1BhcmVudCA1IDAgUgovQ29udGVudHMgOSAwIFIKPj4KZW5kb2JqCjUgMCBvYmoKPDwKL0tpZHMgWzQgMCBSIF0KL0NvdW50IDEKL1R5cGUgL1BhZ2VzCi9NZWRpYUJveCBbIDAgMCA5OSA5IF0KPj4KZW5kb2JqCjMgMCBvYmoKPDwKL1BhZ2VzIDUgMCBSCi9UeXBlIC9DYXRhbG9nCj4+CmVuZG9iagp0cmFpbGVyCjw8Ci9Sb290IDMgMCBSCj4+CiUlRU9G";
 //do this if it is base64 encoded otherwise directly use the byte[]
 byte[] decodedPdf = Base64.getDecoder().decode(yourBase64encodedPdf);

 PdfReader reader = new PdfReader(decodedPdf);
 int numOfPages = reader.getNumberOfPages();
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement