qq_38902274
小鹿鹿鹿
采纳率88.9%
2020-07-25 16:27

PDFRender将PDF文件转为图片出现空白页

我在使用PDFRender将PDF文件转为图片时,发现部分出现空白页,并且提示错误:

java.lang.NullPointerException

    at com.sun.pdfview.font.TTFFont.getOutline(TTFFont.java:170)
    at com.sun.pdfview.font.CIDFontType2.getOutline(CIDFontType2.java:270)
    at com.sun.pdfview.font.OutlineFont.getGlyph(OutlineFont.java:130)
    at com.sun.pdfview.font.PDFFont.getCachedGlyph(PDFFont.java:308)
    at com.sun.pdfview.font.PDFFontEncoding.getGlyphFromCMap(PDFFontEncoding.java:155)
    at com.sun.pdfview.font.PDFFontEncoding.getGlyphs(PDFFontEncoding.java:115)
    at com.sun.pdfview.font.PDFFont.getGlyphs(PDFFont.java:274)
    at com.sun.pdfview.PDFTextFormat.doText(PDFTextFormat.java:269)
    at com.sun.pdfview.PDFParser.iterate(PDFParser.java:752)
    at com.sun.pdfview.BaseWatchable.run(BaseWatchable.java:101)
    at java.lang.Thread.run(Thread.java:745)

打开TTFFont.java:170处是内容:

protected synchronized GeneralPath getOutline(int glyphId, float width) {
        GlyfTable glyf = (GlyfTable)this.font.getTable("glyf");
        Glyf g = glyf.getGlyph(glyphId);
        GeneralPath gp = null;
        if (g instanceof GlyfSimple) {
            gp = this.renderSimpleGlyph((GlyfSimple)g);
        } else if (g instanceof GlyfCompound) {
            gp = this.renderCompoundGlyph(glyf, (GlyfCompound)g);
        } else {
            gp = new GeneralPath();
        }

        HmtxTable hmtx = (HmtxTable)this.font.getTable("hmtx");
        float advance = (float)hmtx.getAdvance(glyphId) / this.unitsPerEm;
        float widthfactor = width / advance;
        AffineTransform at = AffineTransform.getScaleInstance((double)(1.0F / this.unitsPerEm), (double)(1.0F / this.unitsPerEm));
        at.concatenate(AffineTransform.getScaleInstance((double)widthfactor, 1.0D));
        gp.transform(at);
        return gp;
    }

请大神指教应该怎么修改呢?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • Jazz_z Jazz_z 8月前

    试一试用Free Spire.PDF for Java来转换PDF到图片呢,参考文章

        import java.awt.image.BufferedImage;
        import java.io.File;
        import java.io.IOException;
        import com.spire.pdf.PdfDocument;
        import javax.imageio.ImageIO;
    
        public class toImage {
    
                        public static void main(String[] args) throws IOException {
                                //加载PDF文件
                                PdfDocument doc = new PdfDocument();
                                doc.loadFromFile("Sample.pdf");
    
                                //保存PDF的每一页到图片
                                BufferedImage image;
                                for (int i = 0; i < doc.getPages().getCount(); i++) {
                                        image = doc.saveAsImage(i);
                                        File file = new File( String.format("ToImage-img-%d.png", i));
                                        ImageIO.write(image, "PNG", file);
                                }
    
                                doc.close();
            }
    
        }
    
    点赞 评论 复制链接分享
  • dabocaiqq dabocaiqq 9月前

    看看是不是pdf缺少字体方面的问题。

    点赞 评论 复制链接分享