Unembed a font

This is a code example of iText PDF, discover more.

31st May 2016
admin-marketing

Switch code for this example

UnembedFont.java
  1. /*
  2.  
  3.     This file is part of the iText (R) project.
  4.     Copyright (c) 1998-2016 iText Group NV
  5.  
  6. */
  7.  
  8. /**
  9.  * Example written by Bruno Lowagie in answer to a question by a customer.
  10.  */
  11. package com.itextpdf.samples.sandbox.fonts;
  12.  
  13. import com.itextpdf.io.font.PdfEncodings;
  14. import com.itextpdf.kernel.font.PdfFont;
  15. import com.itextpdf.kernel.font.PdfFontFactory;
  16. import com.itextpdf.kernel.pdf.*;
  17. import com.itextpdf.layout.Document;
  18. import com.itextpdf.layout.element.Paragraph;
  19. import com.itextpdf.samples.GenericTest;
  20. import com.itextpdf.test.annotations.type.SampleTest;
  21.  
  22. import org.junit.experimental.categories.Category;
  23.  
  24. import java.io.File;
  25.  
  26. @Category(SampleTest.class)
  27. public class UnembedFont extends GenericTest {
  28.     public static final String DEST = "./target/test/resources/sandbox/fonts/unembed_font.pdf";
  29.     public static final String SRC = "./target/test/resources/sandbox/fonts/withSerifFont.pdf";
  30.  
  31.     public static void main(String[] args) throws Exception {
  32.         File file = new File(DEST);
  33.         file.getParentFile().mkdirs();
  34.         new UnembedFont().manipulatePdf(DEST);
  35.     }
  36.  
  37.     /**
  38.      * Creates a PDF with an embedded font.
  39.      */
  40.     public void createPdf(String file) throws Exception {
  41.         PdfDocument pdfDoc = new PdfDocument(new PdfWriter(file));
  42.         Document doc = new Document(pdfDoc);
  43.         PdfFont font = PdfFontFactory.createFont("./src/test/resources/font/PT_Serif-Web-Regular.ttf", PdfEncodings.WINANSI, true);
  44.         doc.add(new Paragraph("This is a test with Times New Roman.").setFont(font));
  45.         doc.close();
  46.     }
  47.  
  48.     /**
  49.      * Processes a dictionary.
  50.      * In case of font dictionaries, the dictionary is processed.
  51.      */
  52.     public void unembedTTF(PdfDictionary dict) {
  53.         // we ignore all dictionaries that aren't font dictionaries
  54.         if (!PdfName.Font.equals(dict.getAsName(PdfName.Type))) {
  55.             return;
  56.         }
  57.         // we only remove TTF fonts
  58.         if (dict.getAsDictionary(PdfName.FontFile2) != null) {
  59.             return;
  60.         }
  61.         // check if a subset was used (in which case we remove the prefix)
  62.         PdfName baseFont = dict.getAsName(PdfName.BaseFont);
  63.         if (baseFont.getValue().getBytes()[6] == '+') {
  64.             baseFont = new PdfName(baseFont.getValue().substring(7));
  65.             dict.put(PdfName.BaseFont, baseFont);
  66.         }
  67.         // we check if there's a font descriptor
  68.         PdfDictionary fontDescriptor = dict.getAsDictionary(PdfName.FontDescriptor);
  69.         if (fontDescriptor == null)
  70.             return;
  71.         // is there is, we replace the fontname and remove the font file
  72.         fontDescriptor.put(PdfName.FontName, baseFont);
  73.         fontDescriptor.remove(PdfName.FontFile2);
  74.     }
  75.  
  76.     @Override
  77.     protected void manipulatePdf(String dest) throws Exception {
  78.         createPdf(SRC);
  79.         PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(DEST));
  80.  
  81.         // / we loop over all objects
  82.         PdfObject obj;
  83.         for (int i = 1; i < pdfDoc.getNumberOfPdfObjects(); i++) {
  84.             obj = pdfDoc.getPdfObject(i);
  85.             // we skip all objects that aren't a dictionary
  86.             if (obj == null || !obj.isDictionary())
  87.                 continue;
  88.             // we process all dictionaries
  89.             unembedTTF((PdfDictionary) obj);
  90.         }
  91.         // The unused objects will be removed automatically
  92.  
  93.         // we persist the altered document
  94.         pdfDoc.close();
  95.     }
  96. }
Contact

Still have questions? 

We're happy to answer your questions. Reach out to us and we'll get back to you shortly.

Contact us
Stay updated

Join 11,000+ subscribers and become an iText PDF expert by staying up to date with our new products, updates, tips, technical solutions and happenings.

Subscribe Now