简体   繁体   English

如何将 PDDocument 转换为 Java 中的 Base64 字符串?

[英]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 Base64 String?如何将 PDDocument(pdf 文档包含文字和图像,如果可能的话)转换为 Base64 字符串? Is there any suggestion of code.是否有任何代码建议。 Please.请。

The answer assumes that you are using jdk8 or higher, if not, please see here .答案假设您使用的是 jdk8 或更高版本,如果没有,请参阅此处

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM