Using fonts

Tags: fontsfont filesiText 7
Files: 
/*
 
    This file is part of the iText (R) project.
    Copyright (c) 1998-2016 iText Group NV
 
*/
 
/**
 * Example written by Bruno Lowagie, showing that not all fonts contain
 * all glyphs for all languages.
 */
package com.itextpdf.samples.sandbox.fonts;
 
import com.itextpdf.io.font.FontProgramFactory;
import com.itextpdf.io.font.PdfEncodings;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.samples.GenericTest;
import com.itextpdf.test.annotations.type.SampleTest;
import org.junit.experimental.categories.Category;
 
import java.io.File;
import java.util.HashSet;
import java.util.Set;
import java.util.TreeSet;
 
@Category(SampleTest.class)
public class FontTest extends GenericTest {
    public static final String DEST = "./target/test/resources/sandbox/fonts/font_test.pdf";
    public static final String FONTDIR = "./src/test/resources/book/part3/chapter11/font";
    public static final String TEXT = "Quick brown fox jumps over the lazy dog; 0123456789";
    public static final String CP1250 = "Nikogar\u0161nja zemlja";
    public static final String CP1251 = "\u042f \u043b\u044e\u0431\u043b\u044e \u0442\u0435\u0431\u044f";
    public static final String CP1252 = "Un long dimanche de fian\u00e7ailles";
    public static final String CP1253 = "\u039d\u03cd\u03c6\u03b5\u03c2";
    public static final String CHINESE = "\u5341\u950a\u57cb\u4f0f";
    public static final String JAPANESE = "\u8ab0\u3082\u77e5\u3089\u306a\u3044";
    public static final String KOREAN = "\ube48\uc9d1";
 
    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new FontTest().manipulatePdf(DEST);
    }
 
    @Override
    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(DEST));
        Document doc = new Document(pdfDoc);
        PdfFontFactory.registerDirectory(FONTDIR);
        Set<String> fonts = new HashSet<String>(FontProgramFactory.getRegisteredFonts());
        for (String fontname : fonts) {
            showFontInfo(doc, fontname);
        }
        doc.close();
    }
 
    protected void showFontInfo(Document doc, String fontname) {
        System.out.println(fontname);
        PdfFont font;
        try {
            font = PdfFontFactory.createRegisteredFont(fontname, PdfEncodings.IDENTITY_H);
        } catch (Exception e) {
            doc.add(new Paragraph(
                    String.format("The font %s doesn't have unicode support: %s", fontname, e.getMessage())));
            return;
        }
        doc.add(new Paragraph(
                String.format("Postscript name for %s: %s", fontname, font.getFontProgram().getFontNames().getFontName())));
        doc.add(new Paragraph(TEXT).setFont(font));
        doc.add(new Paragraph(String.format("CP1250: %s", CP1250)).setFont(font));
        doc.add(new Paragraph(String.format("CP1251: %s", CP1251)).setFont(font));
        doc.add(new Paragraph(String.format("CP1252: %s", CP1252)).setFont(font));
        doc.add(new Paragraph(String.format("CP1253: %s", CP1253)).setFont(font));
        doc.add(new Paragraph(String.format("CHINESE: %s", CHINESE)).setFont(font));
        doc.add(new Paragraph(String.format("JAPANESE: %s", JAPANESE)).setFont(font));
        doc.add(new Paragraph(String.format("KOREAN: %s", KOREAN)).setFont(font));
    }
}
/*
 
    This file is part of the iText (R) project.
    Copyright (c) 1998-2016 iText Group NV
 
*/
 
/**
 * This example was written by Bruno Lowagie in answer to:
 * http://stackoverflow.com/questions/34396579
 */
package com.itextpdf.samples.sandbox.fonts;
 
import com.itextpdf.io.font.PdfEncodings;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.samples.GenericTest;
import com.itextpdf.test.annotations.type.SampleTest;
import org.junit.experimental.categories.Category;
 
import java.io.File;
 
@Category(SampleTest.class)
public class FreeSansBold extends GenericTest {
    public static final String DEST
            = "./target/test/resources/sandbox/fonts/free_sans_bold.pdf";
    public static final String FONT
            = "./src/test/resources/font/FreeSans.ttf";
    public static final String FONTBOLD
            = "./src/test/resources/font/FreeSansBold.ttf";
 
    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new FreeSansBold().manipulatePdf(DEST);
    }
 
    @Override
    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
        Document doc = new Document(pdfDoc);
 
        PdfFont font = PdfFontFactory.createFont(FONT, PdfEncodings.IDENTITY_H);
        Paragraph p = new Paragraph("FreeSans regular: \u0110").setFont(font);
        doc.add(p);
 
        PdfFont bold = PdfFontFactory.createFont(FONTBOLD, PdfEncodings.IDENTITY_H);
        p = new Paragraph("FreeSans bold: \u0110").setFont(bold);
        doc.add(p);
 
        doc.close();
    }
}
/*
 
    This file is part of the iText (R) project.
    Copyright (c) 1998-2016 iText Group NV
 
*/
 
/**
 * Example written by Bruno Lowagie in answer to:
 * http://stackoverflow.com/questions/24220668/fontfactory-lowagie-java-getting-java-io-eofexception-when-trying-to-use-gre
 */
package com.itextpdf.samples.sandbox.fonts;
 
import com.itextpdf.io.font.PdfEncodings;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.samples.GenericTest;
import com.itextpdf.test.annotations.type.SampleTest;
import org.junit.experimental.categories.Category;
 
import java.io.File;
 
@Category(SampleTest.class)
public class LiberationSans extends GenericTest {
    public static final String DEST = "./target/test/resources/sandbox/fonts/liberation_sans.pdf";
    public static final String FONT = "./src/test/resources/font/LiberationSans-Regular.ttf";
 
    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new LiberationSans().manipulatePdf(DEST);
    }
 
    @Override
    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(DEST));
        Document doc = new Document(pdfDoc);
 
        PdfFontFactory.register(FONT, "Greek-Regular");
        PdfFont f = PdfFontFactory.createRegisteredFont("Greek-Regular", PdfEncodings.CP1253, true);
 
        Paragraph p = new Paragraph("\u039d\u03cd\u03c6\u03b5\u03c2").setFont(f);
        doc.add(p);
 
        doc.close();
    }
}
/*
 
    This file is part of the iText (R) project.
    Copyright (c) 1998-2016 iText Group NV
 
*/
 
/**
 * Example written by Bruno Lowagie in answer to the following question:
 * http://stackoverflow.com/questions/29237980/itext-pdf-not-displaying-chinese-characters-when-using-noto-fonts
 */
package com.itextpdf.samples.sandbox.fonts;
 
import com.itextpdf.io.font.PdfEncodings;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.samples.GenericTest;
import com.itextpdf.test.annotations.type.SampleTest;
import org.junit.experimental.categories.Category;
 
import java.io.File;
 
@Category(SampleTest.class)
public class NotoExample extends GenericTest {
    public static final String DEST = "./target/test/resources/sandbox/fonts/chinese.pdf";
    public static final String FONT = "./src/test/resources/font/NotoSansCJKsc-Regular.otf";
 
    public static final String TEXT = "These are the protagonists in 'Hero', a movie by Zhang Yimou:\n"
            + "\u7121\u540d (Nameless), \u6b98\u528d (Broken Sword), "
            + "\u98db\u96ea (Flying Snow), \u5982\u6708 (Moon), "
            + "\u79e6\u738b (the King), and \u9577\u7a7a (Sky).";
    public static final String CHINESE = "\u5341\u950a\u57cb\u4f0f";
    public static final String JAPANESE = "\u8ab0\u3082\u77e5\u3089\u306a\u3044";
    public static final String KOREAN = "\ube48\uc9d1";
 
    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new NotoExample().manipulatePdf(DEST);
    }
 
    @Override
    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(DEST));
        Document doc = new Document(pdfDoc);
 
        PdfFont font = PdfFontFactory.createFont(FONT, PdfEncodings.IDENTITY_H);
 
        Paragraph p = new Paragraph(TEXT).setFont(font);
        doc.add(p);
        doc.add(new Paragraph(CHINESE).setFont(font));
        doc.add(new Paragraph(JAPANESE).setFont(font));
        doc.add(new Paragraph(KOREAN).setFont(font));
 
        doc.close();
    }
}
/*
 
    This file is part of the iText (R) project.
    Copyright (c) 1998-2016 iText Group NV
 
*/
 
/**
 * Example written by Bruno Lowagie in answer to:
 * http://thread.gmane.org/gmane.comp.java.lib.itext.general/65892
 *
 * Some text displayed using a Small Caps font.
 */
package com.itextpdf.samples.sandbox.fonts;
 
import com.itextpdf.io.font.PdfEncodings;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.samples.GenericTest;
import com.itextpdf.test.annotations.type.SampleTest;
import org.junit.experimental.categories.Category;
 
import java.io.File;
 
@Category(SampleTest.class)
public class SmallCapsExample extends GenericTest {
    public static final String DEST = "./target/test/resources/sandbox/fonts/small_caps_example.pdf";
    public static final String FONT = "./src/test/resources/font/Delicious-SmallCaps.otf";
 
    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new SmallCapsExample().manipulatePdf(DEST);
    }
 
    @Override
    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(DEST));
        Document doc = new Document(pdfDoc);
 
        PdfFont font = PdfFontFactory.createFont(FONT, PdfEncodings.IDENTITY_H);
 
        Paragraph p = new Paragraph("This is some text displayed using a Small Caps font.")
                .setFont(font);
        doc.add(p);
 
        doc.close();
    }
}
/*
 
    This file is part of the iText (R) project.
    Copyright (c) 1998-2016 iText Group NV
 
*/
 
/**
 * Example written by Bruno Lowagie in answer to:
 * http://stackoverflow.com/questions/27842872/finding-trouble-to-change-installed-font-using-itext-library
 */
package com.itextpdf.samples.sandbox.fonts;
 
import com.itextpdf.io.font.PdfEncodings;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.samples.GenericTest;
import com.itextpdf.test.annotations.type.SampleTest;
import org.junit.experimental.categories.Category;
 
import java.io.File;
 
@Category(SampleTest.class)
public class TengwarQuenya1 extends GenericTest {
    public static final String DEST = "./target/test/resources/sandbox/fonts/tengwar_quenya1.pdf";
    public static final String FONT = "./src/test/resources/font/QUENCAP1.TTF";
 
    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new TengwarQuenya1().manipulatePdf(DEST);
    }
 
    @Override
    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(DEST));
        Document doc = new Document(pdfDoc);
 
        PdfFont font = PdfFontFactory.createFont(FONT, PdfEncodings.WINANSI, true);
        Paragraph p = new Paragraph("\"A Hello World PDF document.").setFont(font);
        doc.add(p);
 
        doc.close();
    }
}
Resources: