2

I have content of a PDF file in base64 like JVBERi0xLjIgDSXi48/T....

How can I parse it to get base64 of each page of it?

Assuming the PDF file has 5 pages. How can I get the content of each page in base64? I already google it but could not find anything. Any help is appreciated.

Kurt Pfeifle
  • 86,724
  • 23
  • 248
  • 345
Sara
  • 2,308
  • 11
  • 50
  • 76
  • 1
    http://stackoverflow.com/questions/4784825/how-to-read-pdf-files-using-java After that just base64 encode all of the read content. – Round Potato Jan 11 '15 at 03:19
  • Thanks @RoundPotato. I prefer not to use any library for that. Do you know any other solution? – Sara Jan 11 '15 at 03:21

2 Answers2

5

In general, it is not even possible to separate the contents of a native PDF file page by page (making it impossible to do so when the file is base64 encoded, as you will see).

The most general structure of a PDF file is, in this order:

  1. PDF header
  2. PDF objects (file body)
  3. PDF xref table (table of contents, giving file offset location for each PDF object)
  4. PDF trailer

You cannot assume that the PDF objects appear in the same order inside the file as the pages do appear inside a PDF viewer.

If you extract a single page, this page itself needs to be a valid PDF document: containing (in this same order) header, objects, xref and trailer, where xref and trailer need to be re-constructed newly so they match the new document (xref and trailer cannot simply be copied from the original document).

For this reason you need to de-code the base64-encoded file completely before you can even think of accessing a single page of the resulting PDF.

To get -- from a 5-page PDF document that has been encoded with base64 -- all individual PDF pages as base64, you have to follow these steps:

  1. De-code the complete base64 file into a valid 5-page PDF document.
  2. Split the 5-page PDF document into 5 separate 1-page PDF documents.
    (you need to know the "rules of the PDF game" for this, or make use of a PDF library that does know)
  3. Encode each 1-page PDF document with base64.
Kurt Pfeifle
  • 86,724
  • 23
  • 248
  • 345
1

You might want to clarify your answer. It is not obvious from your wording whether you want to encode in base64 or decode from it.

Assuming you want to decode(since you said you have base64), there are standard libraries available: Decode Base64 data in Java

Community
  • 1
  • 1
  • I have Base64 of the whole file. Is there a way to split it page-by-page without decoding it? – Sara Jan 11 '15 at 03:30
  • 1
    @Sara No, unless it has special delimeters, delimiting each page chunk of base64. – Round Potato Jan 11 '15 at 03:33
  • yes that is exactly what I am looking for. There should be some standard delimiter which separate each page – Sara Jan 11 '15 at 03:34
  • 1
    @Sara either you should know it or add one yourself that does not collide with the base64 character set. You can see the standard set here: http://en.wikipedia.org/wiki/Base64 – Round Potato Jan 11 '15 at 03:40
  • 4
    There are no special delimiters in between pages for PDF. Hence there cannot be such a thing for base64 encoded PDFs either. See my answer about the PDF file structure. – Kurt Pfeifle Jan 11 '15 at 10:49