简体   繁体   中英

Convert PDFBoxs PDFont into java.awt.Font

I have to read a PDF file and and extract some information from it. Therefor I am using PDFBox. Now I have the problem, that I want to display the results by drawing them on a JPanel. But to do this right, I need the font information of the underlying string.

My problem now is, that I found no good way to convert a PDFont to a java.awt.Font . I thought of create some mapping by using the string representation of the PDFont and extract the relevant information from it, like

Arial -> new Font("Arial", Font.PLAIN, size);
Arial,Bold -> new Font("Arial", Font.BOLD, size);
//and so on

But this does't work, because the string representation differs for every font, for example

Times-Roman -> new Font("Times-Roman", Font.PLAIN, size);
Times-Bold -> new Font("Times-Roman", Font.BOLD, size);

Is there a better way to do the converting?

This is not possible.

Quote from this answer :

be aware that most PDFs do not include to full, complete fontface when they have a font embedded. Mostly they include just the subset of glyphs used in the document.

And indeed, org.apache.pdfbox.pdfviewer.PageDrawer use their own org.apache.pdfbox.rendering.Glyph2D class that acts as bridge between PDFBox and java awt by creating a java.awt.geom.GeneralPath class which can be converted by a transformation to java.awt.Shape that in turn can be drawn by the java.awt.Graphics2D .

No java.awt.Font was used in the process, it is useless to look for it.

Although, if you are 'lucky' about the PDF file and there is actually an entire font embedded inside, then you can grab all PDFont classes and read PDFont -> FontDescriptor -> FontFile2 and output that stream into a file with .ttf extension. (Once you have the .ttf stream you have the java.awt.Font class too.)


That's what I gathered in a couple hours after seeing this abandoned question, hope it will help someone.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM