Reuse a font from an existing PDF

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

31st May 2016
iText PDF

Switch code for this example

ReuseFont.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.  * This example is based on an answer given on StackOverflow:
  10.  * http://stackoverflow.com/questions/22186014/itextsharp-re-use-font-embedded-in-acrofield
  11.  */
  12. package com.itextpdf.samples.sandbox.acroforms;
  13.  
  14. import com.itextpdf.kernel.font.PdfFont;
  15. import com.itextpdf.kernel.font.PdfFontFactory;
  16. import com.itextpdf.kernel.pdf.PdfDictionary;
  17. import com.itextpdf.kernel.pdf.PdfDocument;
  18. import com.itextpdf.kernel.pdf.PdfName;
  19. import com.itextpdf.kernel.pdf.PdfReader;
  20. import com.itextpdf.kernel.pdf.PdfWriter;
  21. import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
  22. import com.itextpdf.samples.GenericTest;
  23. import com.itextpdf.test.annotations.type.SampleTest;
  24.  
  25. import java.io.File;
  26. import java.io.FileInputStream;
  27. import java.io.FileOutputStream;
  28. import java.io.IOException;
  29.  
  30. import org.junit.experimental.categories.Category;
  31.  
  32. @Category(SampleTest.class)
  33. public class ReuseFont extends GenericTest {
  34.     public static final String DEST = "./target/test/resources/sandbox/acroforms/reuse_font.pdf";
  35.     public static final String SRC = "./src/test/resources/pdfs/form.pdf";
  36.  
  37.     public static void main(String[] args) throws Exception {
  38.         File file = new File(DEST);
  39.         file.getParentFile().mkdirs();
  40.         new ReuseFont().manipulatePdf(DEST);
  41.     }
  42.  
  43.     public PdfFont findFontInForm(PdfDocument pdfDoc, PdfName fontName) throws IOException {
  44.         PdfDictionary acroForm = pdfDoc.getCatalog().getPdfObject().getAsDictionary(PdfName.AcroForm);
  45.         if (acroForm == null) {
  46.             return null;
  47.         }
  48.         PdfDictionary dr = acroForm.getAsDictionary(PdfName.DR);
  49.         if (dr == null) {
  50.             return null;
  51.         }
  52.         PdfDictionary font = dr.getAsDictionary(PdfName.Font);
  53.         if (font == null) {
  54.             return null;
  55.         }
  56.         for (PdfName key : font.keySet()) {
  57.             if (key.equals(fontName)) {
  58.                 return PdfFontFactory.createFont(font.getAsDictionary(key));
  59.             }
  60.         }
  61.         return null;
  62.     }
  63.  
  64.     @Override
  65.     protected void manipulatePdf(String dest) throws Exception {
  66.         PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(DEST));
  67.  
  68.         PdfFont font = findFontInForm(pdfDoc, new PdfName("Calibri"));
  69.         PdfCanvas canvas = new PdfCanvas(pdfDoc.getFirstPage());
  70.         canvas.beginText();
  71.         canvas.setFontAndSize(font, 13);
  72.         canvas.moveText(36, 806);
  73.         canvas.showText("Some text in Calibri");
  74.         canvas.endText();
  75.         canvas.stroke();
  76.  
  77.         pdfDoc.close();
  78.     }
  79. }

Resources

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