Reuse a font from an existing PDF

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

7th November 2015
admin-marketing

Switch code for this example

ReuseFont.java
  1. /**
  2.  * This example is based on an answer given on StackOverflow:
  3.  * http://stackoverflow.com/questions/22186014/itextsharp-re-use-font-embedded-in-acrofield
  4.  */
  5. package sandbox.acroforms;
  6.  
  7. import com.itextpdf.text.DocumentException;
  8. import com.itextpdf.text.Element;
  9. import com.itextpdf.text.Font;
  10. import com.itextpdf.text.Phrase;
  11. import com.itextpdf.text.pdf.BaseFont;
  12. import com.itextpdf.text.pdf.ColumnText;
  13. import com.itextpdf.text.pdf.PRIndirectReference;
  14. import com.itextpdf.text.pdf.PdfContentByte;
  15. import com.itextpdf.text.pdf.PdfDictionary;
  16. import com.itextpdf.text.pdf.PdfName;
  17. import com.itextpdf.text.pdf.PdfReader;
  18. import com.itextpdf.text.pdf.PdfStamper;
  19.  
  20. import java.io.File;
  21. import java.io.FileOutputStream;
  22. import java.io.IOException;
  23.  
  24. import sandbox.WrapToTest;
  25.  
  26. @WrapToTest
  27. public class ReuseFont {
  28.  
  29.     public static final String SRC = "resources/pdfs/form.pdf";
  30.     public static final String DEST = "results/acroforms/form_stamped.pdf";
  31.    
  32.     public static void main(String[] args) throws DocumentException, IOException {
  33.         File file = new File(DEST);
  34.         file.getParentFile().mkdirs();
  35.         new ReuseFont().manipulatePdf(SRC, DEST);
  36.     }
  37.  
  38.     public BaseFont findFontInForm(PdfReader reader, PdfName fontname) {
  39.         PdfDictionary root = reader.getCatalog();
  40.         PdfDictionary acroform = root.getAsDict(PdfName.ACROFORM);
  41.         if (acroform == null) return null;
  42.         PdfDictionary dr = acroform.getAsDict(PdfName.DR);
  43.         if (dr == null) return null;
  44.         PdfDictionary font = dr.getAsDict(PdfName.FONT);
  45.         if (font == null) return null;
  46.         for (PdfName key : font.getKeys()) {
  47.             if (key.equals(fontname)) {
  48.                 return BaseFont.createFont((PRIndirectReference)font.getAsIndirectObject(key));
  49.             }
  50.         }
  51.         return null;
  52.     }
  53.    
  54.     public void manipulatePdf(String src, String dest) throws DocumentException, IOException {
  55.         PdfReader reader = new PdfReader(src);
  56.         PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
  57.         BaseFont bf = findFontInForm(reader, new PdfName("Calibri"));
  58.         PdfContentByte canvas = stamper.getOverContent(1);
  59.         Phrase phrase = new Phrase("Some text in Calibri", new Font(bf, 13));
  60.         ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, phrase, 36, 806, 0);
  61.         stamper.close();
  62.     }
  63. }
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