pdf - 如何从内容流中解码数据
问题描述
我使用如下代码创建了一个 pdf 文档:
// The text parameter equels 'שדג' it is Hebrew. unicode equivalent is '\u05E9\u05D3\u05D2'
private static void createSimplePdf(String filename, String text) throws Exception {
final String path = RunItextApp.class.getResource("/Arial.ttf").getPath();
final PdfFont font = PdfFontFactory.createFont(path, PdfEncodings.IDENTITY_H);
Style hebrewStyle = new Style()
.setBaseDirection(BaseDirection.RIGHT_TO_LEFT)
.setFontSize(14)
.setFont(font);
final PdfWriter pdfWriter = new PdfWriter(filename);
final PdfDocument pdfDocument = new PdfDocument(pdfWriter);
final Document pdf = new Document(pdfDocument);
pdf.add(
new Paragraph(text)
.setFontScript(Character.UnicodeScript.HEBREW)
.addStyle(hebrewStyle)
);
pdf.close();
System.out.println("The document '" + filename + "' has been created.");
}
之后,我尝试使用pdfbox
util 打开这个文档,我得到了以下数据:但是我在特别是标签部分
得到了一个意想不到的结果。我期望字符串如下,但我得到了. 我试图将此字符串转换为普通字符串,并得到以下结果:但我期望那个字符串。
我错了什么?热转换这个字符串并得到?Contents:stream
Tj
05E905D305D2
02b902a302a2
hex
ʹʣʢ
שדג
02b902a302a2
שדג
解决方案
这个答案写在评论@usr2564301 中。谢谢您的帮助!您获得的数字不是 Unicode 字符,而是字体索引。(检查字体是如何嵌入的!)PDF 中的文本并不特别关心 Unicode——它可能是也可能不是。优秀的 PDF 创建者会添加 /ToUnicode 表来帮助解码,但它是可选的。