Using fonts
fonttest
JAVA
/**
* Example written by Bruno Lowagie, showing that not all fonts contain
* all glyphs for all languages.
*/
package sandbox.fonts;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Set;
import java.util.TreeSet;
public class FontTest {
public static final String DEST = "results/fonts/overview.pdf";
public static final String FONTDIR = "resources/fonts";
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 IOException, DocumentException {
File file = new File(DEST);
file.getParentFile().mkdirs();
new FontTest().createPdf(DEST);
}
public void createPdf(String dest) throws IOException, DocumentException {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(dest));
document.open();
FontFactory.registerDirectory(FONTDIR);
Set<String> fonts = new TreeSet<String>(FontFactory.getRegisteredFonts());
for (String fontname : fonts) {
showFontInfo(document, fontname);
}
document.close();
}
protected void showFontInfo(Document document, String fontname) throws DocumentException {
System.out.println(fontname);
Font font = null;
try {
font = FontFactory.getFont(fontname, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
}
catch(Exception e) {
document.add(new Paragraph(String.format("The font %s doesn't have unicode support: %s", fontname, e.getMessage())));
return;
}
document.add(new Paragraph(
String.format("Postscript name for %s: %s", fontname, font.getBaseFont().getPostscriptFontName())));
document.add(new Paragraph(TEXT, font));
document.add(new Paragraph(String.format("CP1250: %s", CP1250), font));
document.add(new Paragraph(String.format("CP1251: %s", CP1251), font));
document.add(new Paragraph(String.format("CP1252: %s", CP1252), font));
document.add(new Paragraph(String.format("CP1253: %s", CP1253), font));
document.add(new Paragraph(String.format("CHINESE: %s", CHINESE), font));
document.add(new Paragraph(String.format("JAPANESE: %s", JAPANESE), font));
document.add(new Paragraph(String.format("KOREAN: %s", KOREAN), font));
}
}
liberationsans
JAVA
/**
* 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 sandbox.fonts;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;
@WrapToTest
public class LiberationSans {
public static final String DEST = "results/fonts/LiberationSans.pdf";
public static final String FONT = "resources/fonts/LiberationSans-Regular.ttf";
public static void main(String[] args) throws IOException, DocumentException {
File file = new File(DEST);
file.getParentFile().mkdirs();
new LiberationSans().createPdf(DEST);
}
public void createPdf(String dest) throws IOException, DocumentException {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(dest));
document.open();
FontFactory.register(FONT,"Greek-Regular");
Font f = FontFactory.getFont("Greek-Regular", "Cp1253", true);
Paragraph p = new Paragraph("\u039d\u03cd\u03c6\u03b5\u03c2", f);
document.add(p);
document.close();
}
}
notoexample
JAVA
/**
* 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 sandbox.fonts;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;
@WrapToTest
public class NotoExample {
public static final String DEST = "results/fonts/chinese.pdf";
public static final String FONT = "resources/fonts/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 IOException, DocumentException {
File file = new File(DEST);
file.getParentFile().mkdirs();
new NotoExample().createPdf(DEST);
}
public void createPdf(String dest) throws IOException, DocumentException {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(DEST));
document.open();
Font font = FontFactory.getFont(FONT, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
Paragraph p = new Paragraph(TEXT, font);
document.add(p);
document.add(new Paragraph(CHINESE, font));
document.add(new Paragraph(JAPANESE, font));
document.add(new Paragraph(KOREAN, font));
document.close();
}
}
smallcapsexample
JAVA
/**
* 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 sandbox.fonts;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;
@WrapToTest
public class SmallCapsExample {
public static final String DEST = "results/fonts/small_caps.pdf";
public static final String FONT = "resources/fonts/Delicious-SmallCaps.otf";
public static void main(String[] args) throws IOException, DocumentException {
File file = new File(DEST);
file.getParentFile().mkdirs();
new SmallCapsExample().createPdf(DEST);
}
public void createPdf(String dest) throws IOException, DocumentException {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(DEST));
document.open();
BaseFont bf = BaseFont.createFont(FONT, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
Font f = new Font(bf, 12);
Paragraph p = new Paragraph("This is some text displayed using a Small Caps font.", f);
document.add(p);
document.close();
}
}
tengwarquenya1
JAVA
/*
* Example written by Bruno Lowagie in answer to:
* http://stackoverflow.com/questions/27842872/finding-trouble-to-change-installed-font-using-itext-library
*/
package sandbox.fonts;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;
@WrapToTest
public class TengwarQuenya1 {
public static final String DEST = "results/fonts/tengwarquenya1.pdf";
public static final String FONT = "resources/fonts/QUENCAP1.TTF";
public static void main(String[] args) throws IOException, DocumentException {
File file = new File(DEST);
file.getParentFile().mkdirs();
new TengwarQuenya1().createPdf(DEST);
}
public void createPdf(String dest) throws IOException, DocumentException {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(DEST));
document.open();
Font f1 = FontFactory.getFont(FONT, BaseFont.WINANSI, BaseFont.EMBEDDED, 12);
document.add(new Paragraph("A Hello World PDF document.", f1));
document.close();
}
}
freesansbold
JAVA
/*
* This example was written in answer to:
* http://stackoverflow.com/questions/34396579
*/
package sandbox.fonts;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;import sandbox.WrapToTest;
;
/**
* @author iText
*/
@WrapToTest
public class FreeSansBold {
public static final String DEST = "results/fonts/free_sans_bold.pdf";
public static final String FONT = "resources/fonts/FreeSans.ttf";
public static final String FONTBOLD = "resources/fonts/FreeSansBold.ttf";
public static void main(String[] args) throws IOException, DocumentException {
File file = new File(DEST);
file.getParentFile().mkdirs();
new FreeSansBold().createPdf(DEST);
}
public void createPdf(String dest) throws IOException, DocumentException {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(dest));
document.open();
BaseFont bf = BaseFont.createFont(FONT, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
Font font = new Font(bf, 12);
Paragraph p = new Paragraph("FreeSans regular: \u0110", font);
document.add(p);
BaseFont bfbold = BaseFont.createFont(FONTBOLD, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
Font bold = new Font(bfbold, 12);
p = new Paragraph("FreeSans bold: \u0110", bold);
document.add(p);
document.close();
}
}
Resources
Delicious-SmallCaps.otf
LiberationSans-Regular.ttf
NotoSansCJKsc-Regular.otf
QUENCAP1.TTF
FreeSans.ttf
FreeSansBold.ttf
LiberationSans-Regular.ttf
NotoSansCJKsc-Regular.otf
QUENCAP1.TTF
FreeSans.ttf
FreeSansBold.ttf