How to convert PDDocument (the pdf document contains words and images, if it is possible) to Base64 String? Is there any suggestion of code. Please.
Asked
Active
Viewed 3,045 times
0
-
1Do you want to encode the whole file or just the human-readable text inside it? – Sterconium Sep 19 '19 at 09:14
-
1If you want to save the PDF to a byte array, just call `doc.save(baos);` with `baos` being a `ByteArrayOutputStream`. And from there you can convert to base64. – Tilman Hausherr Sep 19 '19 at 09:31
-
I want to encode whole file in format from pdf (PDDocument) to String Base64... – user7327905 Sep 19 '19 at 11:16
-
Problem solved. Thank you. – user7327905 Sep 19 '19 at 11:40
1 Answers
6
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

Tilman Hausherr
- 17,731
- 7
- 58
- 97