2

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?

Ennosigaeon
  • 442
  • 7
  • 15
  • You might look at `org.apache.pdfbox.pdfviewer.PageDrawer` for inspiration, a class to *paint a page in a PDF document to a graphics context.* – mkl Apr 03 '14 at 13:45

1 Answers1

3

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.

andras
  • 3,305
  • 5
  • 30
  • 45
  • and if that one doesn't work (the awt classes are not very good), then get the path like done in the DrawPrintTextLocations.java example in the source code download. – Tilman Hausherr May 03 '19 at 11:56