iText pdf library
Website search

Creating a custom Type 3 font

Logo.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2020 iText Group NV
    Authors: iText Software.
 
    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
/**
 * Example written by Bruno Lowagie.
 */
package com.itextpdf.samples.sandbox.fonts;
 
import com.itextpdf.kernel.colors.DeviceRgb;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.font.PdfType3Font;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
import com.itextpdf.kernel.pdf.canvas.PdfCanvasConstants;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;
 
import java.io.File;
 
public class Logo {
    public static final String DEST = "./target/sandbox/fonts/logo.pdf";
 
    public static final String FONT = "./src/test/resources/font/FreeSans.ttf";
 
    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
 
        new Logo().manipulatePdf(DEST);
    }
 
    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
        Document doc = new Document(pdfDoc);
 
        /* Create a custom type 3 font.
         * The second argument defines whether the glyph color is specified
         * in the glyph description in the font.
         */
        PdfType3Font t3 = PdfFontFactory.createType3Font(pdfDoc, true);
        float linewidth = 125;
 
        /* Define I letter of the custom font.
         * If the character was already defined, addGlyph() method will return the same content.
         * If the colorized parameter of PdfType3Font instance is set to true,
         * then parameters of the glyph bounding box will be ignored.
         */
        PdfCanvas i = t3.addGlyph('I', 700, 0, 0, 1200, 600);
        i.setLineWidth(10);
        i.setStrokeColor(new DeviceRgb(0xf9, 0x9d, 0x25));
        i.setLineWidth(linewidth);
        i.setLineCapStyle(PdfCanvasConstants.LineCapStyle.ROUND);
        i.moveTo(600, 36);
        i.lineTo(600, 564);
        i.stroke();
 
        // Define T letter of the custom font
        PdfCanvas t = t3.addGlyph('T', 1170, 0, 0, 1200, 600);
        t.setLineWidth(10);
        t.setStrokeColor(new DeviceRgb(0x08, 0x49, 0x75));
        t.setLineWidth(linewidth);
        t.setLineCapStyle(PdfCanvasConstants.LineCapStyle.ROUND);
        t.moveTo(144, 564);
        t.lineTo(1056, 564);
        t.moveTo(600, 36);
        t.lineTo(600, 564);
        t.stroke();
 
        // Define E letter of the custom font
        PdfCanvas e = t3.addGlyph('E', 1150, 0, 0, 1200, 600);
        e.setLineWidth(10);
        e.setStrokeColor(new DeviceRgb(0xf8, 0x9b, 0x22));
        e.setLineWidth(linewidth);
        e.setLineCapStyle(PdfCanvasConstants.LineCapStyle.ROUND);
        e.moveTo(144, 36);
        e.lineTo(1056, 36);
        e.moveTo(144, 300);
        e.lineTo(1056, 300);
        e.moveTo(144, 564);
        e.lineTo(1056, 564);
        e.stroke();
 
        // Define X letter of the custom font
        PdfCanvas x = t3.addGlyph('X', 1160, 0, 0, 1200, 600);
        x.setStrokeColor(new DeviceRgb(0x10, 0x46, 0x75));
        x.setLineWidth(10);
        x.setLineWidth(linewidth);
        x.setLineCapStyle(PdfCanvasConstants.LineCapStyle.ROUND);
        x.moveTo(144, 36);
        x.lineTo(1056, 564);
        x.moveTo(144, 564);
        x.lineTo(1056, 36);
        x.stroke();
 
        Paragraph p = new Paragraph("ITEXT")
                .setFont(t3)
                .setFontSize(20);
        doc.add(p);
 
        p = new Paragraph("I\nT\nE\nX\nT")
                .setFixedLeading(20)
                .setFont(t3)
                .setFontSize(20);
        doc.add(p);
 
        doc.close();
    }
}
Logo.cs
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2020 iText Group NV
    Authors: iText Software.
 
    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
 
using System;
using System.IO;
using iText.Kernel.Colors;
using iText.Kernel.Font;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Canvas;
using iText.Layout;
using iText.Layout.Element;
 
namespace iText.Samples.Sandbox.Fonts
{
    public class Logo
    {
        public static readonly String DEST = "results/sandbox/fonts/logo.pdf";
 
        public static readonly String FONT = "./src/test/resources/font/FreeSans.ttf";
 
        public static void Main(String[] args)
        {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();
 
            new Logo().ManipulatePdf(DEST);
        }
 
        protected void ManipulatePdf(String dest)
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
            Document doc = new Document(pdfDoc);
 
            /* Create a custom type 3 font.
             * The second argument defines whether the glyph color is specified
             * in the glyph description in the font.
             */
            PdfType3Font t3 = PdfFontFactory.CreateType3Font(pdfDoc, true);
            float linewidth = 125;
 
            /* Define I letter of the custom font.
             * If the character was already defined, addGlyph() method will return the same content.
             * If the colorized parameter of PdfType3Font instance is set to true,
             * then parameters of the glyph bounding box will be ignored.
             */
            PdfCanvas i = t3.AddGlyph('I', 700, 0, 0, 1200, 600);
            i.SetLineWidth(10);
            i.SetStrokeColor(new DeviceRgb(0xf9, 0x9d, 0x25));
            i.SetLineWidth(linewidth);
            i.SetLineCapStyle(PdfCanvasConstants.LineCapStyle.ROUND);
            i.MoveTo(600, 36);
            i.LineTo(600, 564);
            i.Stroke();
 
            // Define T letter of the custom font
            PdfCanvas t = t3.AddGlyph('T', 1170, 0, 0, 1200, 600);
            t.SetLineWidth(10);
            t.SetStrokeColor(new DeviceRgb(0x08, 0x49, 0x75));
            t.SetLineWidth(linewidth);
            t.SetLineCapStyle(PdfCanvasConstants.LineCapStyle.ROUND);
            t.MoveTo(144, 564);
            t.LineTo(1056, 564);
            t.MoveTo(600, 36);
            t.LineTo(600, 564);
            t.Stroke();
 
            // Define E letter of the custom font
            PdfCanvas e = t3.AddGlyph('E', 1150, 0, 0, 1200, 600);
            e.SetLineWidth(10);
            e.SetStrokeColor(new DeviceRgb(0xf8, 0x9b, 0x22));
            e.SetLineWidth(linewidth);
            e.SetLineCapStyle(PdfCanvasConstants.LineCapStyle.ROUND);
            e.MoveTo(144, 36);
            e.LineTo(1056, 36);
            e.MoveTo(144, 300);
            e.LineTo(1056, 300);
            e.MoveTo(144, 564);
            e.LineTo(1056, 564);
            e.Stroke();
 
            // Define X letter of the custom font
            PdfCanvas x = t3.AddGlyph('X', 1160, 0, 0, 1200, 600);
            x.SetStrokeColor(new DeviceRgb(0x10, 0x46, 0x75));
            x.SetLineWidth(10);
            x.SetLineWidth(linewidth);
            x.SetLineCapStyle(PdfCanvasConstants.LineCapStyle.ROUND);
            x.MoveTo(144, 36);
            x.LineTo(1056, 564);
            x.MoveTo(144, 564);
            x.LineTo(1056, 36);
            x.Stroke();
 
            Paragraph p = new Paragraph("ITEXT")
                .SetFont(t3)
                .SetFontSize(20);
            doc.Add(p);
 
            p = new Paragraph("I\nT\nE\nX\nT")
                .SetFixedLeading(20)
                .SetFont(t3)
                .SetFontSize(20);
            doc.Add(p);
 
            doc.Close();
        }
    }
}

Click the following link to see the legacy example for iText 5. Except for security fixes, iText 5 is no longer being developed.



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