如何获取PDF文件中每个单词的字体文件或PDFont?
How do I get the font file or PDFont of each word in a PDF file?
有没有办法使用PDFBox获取PDF文件每个字的字体?我试过了,但它只列出了该页面上使用的所有字体。
PDDocument pdfDocument = PDDocument.load(new File("xxofd.pdf"));
PDPageTree pages = pdfDocument.getDocumentCatalog().getPages();
for (PDPage page : pages) {
PDResources res = page.getResources();
for (COSName fontName : res.getFontNames()) {
PDFont font = null;
try {
font = res.getFont(fontName);
} catch (IOException e) {
e.printStackTrace();
}
}
}
pdf文件中有很多不同的字符,可能不同的字符是不同的字体。我想提取这些字体的一个子集。该子集仅包含pdf文件中出现过的单词的字体。这将使字体文件 smaller.So 我想要获取PDF文件每个单词的字体文件或PDFont结构。有什么办法吗?谢谢
让PDF文件:
然后
PDDocument pdfDocument = PDDocument.load(new File("/home/josejuan/tmp/fonts.pdf"));
PDFTextStripper pdfStripper = new PDFTextStripper() {
@Override
protected void processTextPosition(TextPosition text) {
System.out.println("Text `" + text.getUnicode() + "` with font `" + text.getFont().getName() + "`");
}
};
// force parse
pdfStripper.getText(pdfDocument);
产生预期的输出
Text `E` with font `BAAAAA+LiberationSerif`
Text `x` with font `BAAAAA+LiberationSerif`
Text `a` with font `CAAAAA+CantarellRegular`
Text `m` with font `CAAAAA+CantarellRegular`
Text `p` with font `BAAAAA+LiberationSerif`
....
(当然可以分组)
从该代码您可以描述文本的每个字符,例如,如果您需要字体文件:
text.getFont().getFontDescriptor().getFontFile()
但根据您要查找的内容,使用 PDFont
、PDFontDescriptor
、PDStream
、...
会更好
有没有办法使用PDFBox获取PDF文件每个字的字体?我试过了,但它只列出了该页面上使用的所有字体。
PDDocument pdfDocument = PDDocument.load(new File("xxofd.pdf"));
PDPageTree pages = pdfDocument.getDocumentCatalog().getPages();
for (PDPage page : pages) {
PDResources res = page.getResources();
for (COSName fontName : res.getFontNames()) {
PDFont font = null;
try {
font = res.getFont(fontName);
} catch (IOException e) {
e.printStackTrace();
}
}
}
pdf文件中有很多不同的字符,可能不同的字符是不同的字体。我想提取这些字体的一个子集。该子集仅包含pdf文件中出现过的单词的字体。这将使字体文件 smaller.So 我想要获取PDF文件每个单词的字体文件或PDFont结构。有什么办法吗?谢谢
让PDF文件:
然后
PDDocument pdfDocument = PDDocument.load(new File("/home/josejuan/tmp/fonts.pdf"));
PDFTextStripper pdfStripper = new PDFTextStripper() {
@Override
protected void processTextPosition(TextPosition text) {
System.out.println("Text `" + text.getUnicode() + "` with font `" + text.getFont().getName() + "`");
}
};
// force parse
pdfStripper.getText(pdfDocument);
产生预期的输出
Text `E` with font `BAAAAA+LiberationSerif`
Text `x` with font `BAAAAA+LiberationSerif`
Text `a` with font `CAAAAA+CantarellRegular`
Text `m` with font `CAAAAA+CantarellRegular`
Text `p` with font `BAAAAA+LiberationSerif`
....
(当然可以分组)
从该代码您可以描述文本的每个字符,例如,如果您需要字体文件:
text.getFont().getFontDescriptor().getFontFile()
但根据您要查找的内容,使用 PDFont
、PDFontDescriptor
、PDStream
、...