Reuse a font from an existing PDF

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

7th November 2015
admin-marketing

iText 5 is no longer being developed, except for security fixes. Click the following link to see an updated example for iText 7.

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


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