How to export embedded ttf fonts into ttf files with itext in Java?

  Kiến thức lập trình

I have to export embedded ttf fonts from a PDF file to valid ttf files. I have found a code snippet in the knowledge base of itext which can unembedd fonts in a PDF file. But I can’t find a way how to write the fonts to files in order to save them before unembedding.

I have dived into the itext code and could not figure out how to transform a PdfFont to bytes[] or something. There is a snippet for embedding fonts using a PdfStream based on the bytes from the font file:

// The font file
RandomAccessFile raf = new RandomAccessFile(fontfile, "r");
byte fontbytes[] = new byte[(int) raf.length()];
raf.readFully(fontbytes);
raf.close();

// Create a new stream for the font file
PdfStream stream = new PdfStream(fontbytes);
stream.setCompressionLevel(CompressionConstants.DEFAULT_COMPRESSION);
stream.put(PdfName.Length1, new PdfNumber(fontbytes.length));


...

// Embed the passed font to the pdf document
fontDictionary.put(PdfName.FontFile2, stream.makeIndirect(pdfDoc).getIndirectReference());

I tried to reverse the procedure but cannot figure out how to do it using the PdfIndirectReference.

Can the PdfWriter be used for the task? Or do I really need to work with glyphs?

4

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website Kho Theme wordpress Kho Theme WP Theme WP

LEAVE A COMMENT