Java – Using sun PDF Renderer to display PDFs with embedded fonts

javapdf

I'm having trouble using Sun's PDF Renderer package to view PDFs with embedded fonts. I have the following code which creates a BufferedImage out of every page of a PDF for viewing in my application, and it works fine when there are no embedded fonts. However, when the PDF has embedded fonts, it shows no text. Any ideas? Also, it opens fine in Adobe's PDF viewer.

File f = new File("C:\\test.pdf");
FileChannel fc = new RandomAccessFile(f, "r").getChannel();
PDFFile pdfFile = new PDFFile(fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size()));
for(int x=0; x<pdfFile.getNumPages(); x++) {
    try {
        BufferedImage bi = (BufferedImage)pdfFile.getPage(x+1).getImage(
            (int)pdfFile.getPage(x+1).getWidth(),
            (int)pdfFile.getPage(x+1).getHeight(),
            new Rectangle((int)pdfFile.getPage(x+1).getWidth(),
            (int)pdfFile.getPage(x+1).getHeight()),
             null, true, true);
    }
    catch (Exception e) {
        e.printStackTrace();
    }
}

Best Answer

I figured this out by changing PDF renders from PDFRenderer to PDFBox, which works much better. More info is available here.