srinannapa srinannapa - 6 months ago 40
Java Question

Modify the font of existing PDF in Itext

Can I modify the font of existing PDF. I've some Type 3 (Custom) fonts with out any font descriptor. I would like to replace those to some meaningful Font. How can I do this using Itext or PDFBox ?

Answer

A Type3 font is also known as a user defined font. Characters such as a, b, c,... are mapped to glyphs that are defined by a person. For instance a corresponds with the Apple logo, b corresponds with a glyph shaped as a banana, c looks like a coconut.

  • The shape of the apple, banana, coconut,... is stored in the PDF using PDF syntax. A PDF viewer such as Adobe Reader can draw the apple, banana, coconut,... by executing the font program that in this case consists of PDF path-constructing and path-painting operators and operands.
  • A PDF viewer doesn't have the intelligence to recognize these shapes as being the representation of an apple, banana, coconut,... A PDF viewer only knows that the user mapped these glyphs to characters such as a, b, c,... See also my answer to the question Why can't I extract text added using a Type3 font correctly?

Sometimes, people will match characters and glyphs, the way I did when I created a Type3 font for the iText logo: read Creating the iText logo witha Type3 font. However you shouldn't count on that. Any character can be mapped to any glyph.

Now that you know this, you should understand that you're trying to achieve something that is impossible. Suppose that you have a PDF with a Type3 font where the a character corresponds with an apple, the b character with a banana and the c character with a coconut, it won't be possible to automatically replace such a user-defined font with a custom encoding with another "normal" font that doesn't know how to draw apples, bananas and coconuts, and that uses a completely different encoding.