Java PDF字体嵌入无法使用PDFBox

Java PDF字体嵌入无法使用PDFBox,java,pdf,pdfbox,Java,Pdf,Pdfbox,我正在努力嵌入那些没有嵌入到PDF的字体。为此,我使用PDFBox库使用PDFont类来识别缺少的字体。使用这个,我能够识别丢失字体的列表。但是,当我试图使用我的本地机器字体(从我的本地机器字体文件夹抓取TTF文件)嵌入它们时,我无法做到这一点,结果如下 我使用以下代码来获取字体列表(非嵌入字体) private static List checkAllFontsEmbeddedOrNot(PDDocument PDDocument)引发异常{ List notEmbFonts=null; 试

我正在努力嵌入那些没有嵌入到PDF的字体。为此,我使用PDFBox库使用
PDFont
类来识别缺少的字体。使用这个,我能够识别丢失字体的列表。但是,当我试图使用我的本地机器字体(从我的本地机器字体文件夹抓取TTF文件)嵌入它们时,我无法做到这一点,结果如下

我使用以下代码来获取字体列表(非嵌入字体)

private static List checkAllFontsEmbeddedOrNot(PDDocument PDDocument)引发异常{
List notEmbFonts=null;
试一试{
if(null!=pdDocument){
PDPageTree pageTree=pdDocument.getDocumentCatalog().getPages();
notEmbFonts=newArrayList();
用于(PDPage PDPage:pageTree){
PDResources=pdPage.getResources();
Iterable cosNameIte=resources.getFontNames();
迭代器名称=cosname.Iterator();
while(names.hasNext()){
COSName name=names.next();
PDFont=resources.getFont(名称);
布尔值isEmbedded=font.isEmbedded();
如果(!isEmbedded){
FontsDetails FontsDetails=新FontsDetails();
setFontName(font.getName().toString());
setFontSubType(font.getSubType());
添加(fontsDetails);
}
}
}
}
}捕获(异常){
logger.error(“验证字体时发生异常:”,异常);
抛出新的PDFutilException(“验证字体时发生异常:”,异常);
}
返回notEmbFonts;
}
下面是我用来嵌入字体的代码,我从上面的列表中得到了这些字体

public List<FontsDetails> embedFontToPdf(File pdf, FontsDetails fontToEmbed) {
    ArrayList<FontsDetails> notSupportedFonts = new ArrayList<>();
    try (PDDocument pdDocument = PDDocument.load(pdf)) {
        LOGGER.info("Embedding font : " + fontToEmbed.getFontName());
        InputStream ttfFileStream =  PDFBoxOperationsUtility.class.getClassLoader()
                .getResourceAsStream(fontToEmbed.getFontName() + ".ttf");//loading ttf file
        if (null != ttfFileStream) {
            PDFont font = PDType0Font.load(pdDocument, ttfFileStream);
            PDPage pdfPage = new PDPage();
            PDResources pdfResources = new PDResources();
            pdfPage.setResources(pdfResources);

            PDPageContentStream contentStream = new PDPageContentStream(pdDocument, pdfPage);
            if (fontToEmbed.getFontSize() == 0) {
                fontToEmbed.setFontSize(DEFAULT_FONT_SIZE);
            }
            font.encode("ANSI");
            contentStream.setFont(font, fontToEmbed.getFontSize());
            contentStream.close();

            pdDocument.addPage(pdfPage);
            pdDocument.save(pdf);
        } else {
            LOGGER.info("Font : " + fontToEmbed.getFontName() + " not supported");
            notSupportedFonts.add(fontToEmbed);
        }
    } catch (Exception exception) {
        notSupportedFonts.add(fontToEmbed);
        LOGGER.error("Error ocurred while embedding font to pdf : " + pdf.getName(), exception);
    }
    return notSupportedFonts;
}
公共列表EmbeddeFontTopDF(文件pdf,FontsDetails fontToEmbed){
ArrayList notSupportedFonts=新建ArrayList();
try(PDDocument PDDocument=PDDocument.load(pdf)){
info(“嵌入字体:+fontToEmbed.getFontName());
InputStream ttfFileStream=PDFBoxOperationsUtility.class.getClassLoader()
.getResourceAsStream(fontToEmbed.getFontName()+“.ttf”);//正在加载ttf文件
if(null!=ttfFileStream){
PDFont font=PDType0Font.load(pdDocument,ttfFileStream);
PDPage pdfPage=新PDPage();
PDResources pdfResources=新的PDResources();
pdfPage.setResources(pdfResources);
PDPageContentStream contentStream=新的PDPageContentStream(pdDocument,pdfPage);
如果(fontToEmbed.getFontSize()==0){
fontToEmbed.setFontSize(默认字体大小);
}
字体编码(“ANSI”);
setFont(font,fontToEmbed.getFontSize());
contentStream.close();
pdDocument.addPage(pdfPage);
pdDocument.save(pdf);
}否则{
LOGGER.info(“字体:“+fontToEmbed.getFontName()+”不受支持”);
添加(fontToEmbed);
}
}捕获(异常){
添加(fontToEmbed);
LOGGER.error(“将字体嵌入pdf时出错:“+pdf.getName(),异常”);
}
返回不支持的字体;
}

有人能帮我确定我犯了什么错误或者我需要采取的任何其他方法吗。

看不到真正的重复,但应该会给出一些提示。当然@TilmanHausherr我会尝试一下,会让你知道的。@TilmanHausherr我使用的是pdfbox 2.0.17版,但无法在PDFont类中获取getEncoding()方法,我应该使用不同的版本吗?通常使用最新的版本<代码>getEncoding()在某些子类中可用。(没有完美的解决方案,即链接答案可能会或可能不起作用,但它显示了如何替换字体中的资源)也考虑到不仅有资源在页面级,而且XObjor,模式等可能有自己的资源与字体更新。
public List<FontsDetails> embedFontToPdf(File pdf, FontsDetails fontToEmbed) {
    ArrayList<FontsDetails> notSupportedFonts = new ArrayList<>();
    try (PDDocument pdDocument = PDDocument.load(pdf)) {
        LOGGER.info("Embedding font : " + fontToEmbed.getFontName());
        InputStream ttfFileStream =  PDFBoxOperationsUtility.class.getClassLoader()
                .getResourceAsStream(fontToEmbed.getFontName() + ".ttf");//loading ttf file
        if (null != ttfFileStream) {
            PDFont font = PDType0Font.load(pdDocument, ttfFileStream);
            PDPage pdfPage = new PDPage();
            PDResources pdfResources = new PDResources();
            pdfPage.setResources(pdfResources);

            PDPageContentStream contentStream = new PDPageContentStream(pdDocument, pdfPage);
            if (fontToEmbed.getFontSize() == 0) {
                fontToEmbed.setFontSize(DEFAULT_FONT_SIZE);
            }
            font.encode("ANSI");
            contentStream.setFont(font, fontToEmbed.getFontSize());
            contentStream.close();

            pdDocument.addPage(pdfPage);
            pdDocument.save(pdf);
        } else {
            LOGGER.info("Font : " + fontToEmbed.getFontName() + " not supported");
            notSupportedFonts.add(fontToEmbed);
        }
    } catch (Exception exception) {
        notSupportedFonts.add(fontToEmbed);
        LOGGER.error("Error ocurred while embedding font to pdf : " + pdf.getName(), exception);
    }
    return notSupportedFonts;
}