PDF jet

Embedding Unicode fonts


Example_07.pdf

Note #1: Make sure that the font license allows embedding!

Note #2: Embedding of Chinese, Japanese and Korean fonts is not supported!

import java.io.*;

import com.pdfjet.*;


/**
 *  Example_07.java
 *
 */
public class Example_07 {

    public Example_07() throws Exception {

        FileOutputStream fos = new FileOutputStream("Example_07.pdf");

        PDF pdf = new PDF(fos, Compliance.PDF_A_1B);

        Font f1 = new Font(pdf,
                new BufferedInputStream(getClass().getResourceAsStream(
                        "fonts/DroidFonts/DroidSerif-Regular.otf")),
                CodePage.UNICODE,
                Embed.YES);

        Font f2 = new Font(pdf,
                new BufferedInputStream(getClass().getResourceAsStream(
                        "fonts/DroidFonts/DroidSerif-Italic.ttf")),
                CodePage.UNICODE,
                Embed.YES);

        Page page = new Page(pdf, Letter.PORTRAIT);

        f1.setSize(15f);
        f2.setSize(15f);
        
        int x_pos = 70;
        int y_pos = 70;
        TextLine text = new TextLine(f1);
        text.setPosition(x_pos, y_pos);
        StringBuilder buf = new StringBuilder();

        for (int i = 0x20; i < 0x7F; i++) {
            if (i % 16 == 0) {
                text.setText(buf.toString());
                text.setPosition(x_pos, y_pos += 24);
                text.drawOn(page);
                buf = new StringBuilder();
            }
            buf.append((char) i);
        }

        text.setText(buf.toString());
        text.setPosition(x_pos, y_pos += 24);
        text.drawOn(page);

        y_pos += 24;
        buf = new StringBuilder();
        for (int i = 0x390; i < 0x3EF; i++) {
            if (i % 16 == 0) {
                text.setText(buf.toString());
                text.setPosition(x_pos, y_pos += 24);
                text.drawOn(page);
                buf = new StringBuilder();
            }
            if (i == 0x3A2
                    || i == 0x3CF
                    || i == 0x3D0
                    || i == 0x3D3
                    || i == 0x3D4
                    || i == 0x3D5
                    || i == 0x3D7
                    || i == 0x3D8
                    || i == 0x3D9
                    || i == 0x3DA
                    || i == 0x3DB
                    || i == 0x3DC
                    || i == 0x3DD
                    || i == 0x3DE
                    || i == 0x3DF
                    || i == 0x3E0
                    || i == 0x3EA
                    || i == 0x3EB
                    || i == 0x3EC
                    || i == 0x3ED
                    || i == 0x3EF) {
                // Replace .notdef with space to generate PDF/A compliant PDF
                buf.append((char) 0x0020);
            }
            else {
                buf.append((char) i);
            }
        }

        y_pos += 24;
        buf = new StringBuilder();
        for (int i = 0x410; i <= 0x46F; i++) {
            if (i % 16 == 0) {
                text.setText(buf.toString());
                text.setPosition(x_pos, y_pos += 24);
                text.drawOn(page);
                buf = new StringBuilder();
            }
            buf.append((char) i);
        }


        x_pos = 370;
        y_pos = 70;
        text = new TextLine(f2);
        text.setPosition(x_pos, y_pos);
        buf = new StringBuilder();
        for (int i = 0x20; i < 0x7F; i++) {
            if (i % 16 == 0) {
                text.setText(buf.toString());
                text.setPosition(x_pos, y_pos += 24);
                text.drawOn(page);
                buf = new StringBuilder();
            }
            buf.append((char) i);
        }
        text.setText(buf.toString());
        text.setPosition(x_pos, y_pos += 24);
        text.drawOn(page);

        y_pos += 24;
        buf = new StringBuilder();
        for (int i = 0x390; i < 0x3EF; i++) {
            if (i % 16 == 0) {
                text.setText(buf.toString());
                text.setPosition(x_pos, y_pos += 24);
                text.drawOn(page);
                buf = new StringBuilder();
            }
            if (i == 0x3A2
                    || i == 0x3CF
                    || i == 0x3D0
                    || i == 0x3D3
                    || i == 0x3D4
                    || i == 0x3D5
                    || i == 0x3D7
                    || i == 0x3D8
                    || i == 0x3D9
                    || i == 0x3DA
                    || i == 0x3DB
                    || i == 0x3DC
                    || i == 0x3DD
                    || i == 0x3DE
                    || i == 0x3DF
                    || i == 0x3E0
                    || i == 0x3EA
                    || i == 0x3EB
                    || i == 0x3EC
                    || i == 0x3ED
                    || i == 0x3EF) {
                // Replace .notdef with space to generate PDF/A compliant PDF
                buf.append((char) 0x0020);
            }
            else {
                buf.append((char) i);
            }
        }
        
        y_pos += 24;
        buf = new StringBuilder();
        for (int i = 0x410; i < 0x46F; i++) {
            if (i % 16 == 0) {
                text.setText(buf.toString());
                text.setPosition(x_pos, y_pos += 24);
                text.drawOn(page);
                buf = new StringBuilder();
            }
            buf.append((char) i);
        }

        pdf.flush();
        fos.close();
    }

    public static void main(String[] args) throws Exception {
        new Example_07();
    }

}   // End of Example_07.java

© 2017 Innovatics Inc.