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!

using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Text;

using PDFjet.NET;


/**
 *  Example_07.cs
 *
 */
public class Example_07 {

    public Example_07() {

        FileStream fos = new FileStream("Example_07.pdf", FileMode.Create);
        BufferedStream bos = new BufferedStream(fos);

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

        FileStream fis = new FileStream(
                "fonts/DroidFonts/DroidSerif-Regular.otf", FileMode.Open, FileAccess.Read);
        Font f1 = new Font(pdf, fis, CodePage.UNICODE, Embed.YES);

        fis = new FileStream(
                "fonts/DroidFonts/DroidSerif-Italic.ttf", FileMode.Open, FileAccess.Read);
        Font f2 = new Font(pdf, fis, 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);
        }

        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);
        }

        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();
        bos.Close();
    }

    public static void Main(String[] args) {
        new Example_07();
    }

}   // End of Example_07.cs

© 2017 Innovatics Inc.