user7327905
user7327905

Reputation: 15

How to convert PDDocument to Base64 String in Java?

How to convert PDDocument (the pdf document contains words and images, if it is possible) to a Base64 string?

Upvotes: 0

Views: 4048

Answers (1)

Tilman Hausherr
Tilman Hausherr

Reputation: 18851

The answer assumes that you are using jdk8 or higher, if not, please see here.

import java.util.Base64;

...

ByteArrayOutputStream baos = new ByteArrayOutputStream();
doc.save(baos);
String base64String = Base64.getEncoder().encodeToString(baos.toByteArray());
doc.close(); // don't forget to close your document

Upvotes: 6

Related Questions