Skip to main content
Skip table of contents

iText Building Blocks - Chapter 1: PdfFont examples

c01e01_text_paragraph

JAVA
JAVA
package com.itextpdf.highlevel.chapter01;

import com.itextpdf.io.font.constants.StandardFonts;
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.layout.element.Text;
import java.io.File;
import java.io.IOException;

/**
 * @author Bruno Lowagie (iText Software)
 */
public class C01E01_Text_Paragraph {
    
    public static final String DEST = "results/chapter01/text_paragraph.pdf";
    
    public static void main(String args[]) throws IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new C01E01_Text_Paragraph().createPdf(DEST);
    }
    
    public void createPdf(String dest) throws IOException {
        // Initialize PDF document
        PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
        
        // Initialize document
        Document document = new Document(pdf);
        
        // Add content
        PdfFont font = PdfFontFactory.createFont(StandardFonts.TIMES_ROMAN);
        PdfFont bold = PdfFontFactory.createFont(StandardFonts.TIMES_BOLD);
        Text title = new Text("The Strange Case of Dr. Jekyll and Mr. Hyde").setFont(bold);
        Text author = new Text("Robert Louis Stevenson").setFont(font);
        Paragraph p = new Paragraph().add(title).add(" by ").add(author);
        document.add(p);
        
        //Close document
        document.close();
    }
}
C#
C#
using System;
using System.IO;
using iText.IO.Font.Constants;
using iText.Kernel.Font;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;

namespace iText.Highlevel.Chapter01 {
    /// <author>Bruno Lowagie (iText Software)</author>
    public class C01E01_Text_Paragraph {
        public const String DEST = "../../../results/chapter01/text_paragraph.pdf";

        public static void Main(String[] args) {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();
            new C01E01_Text_Paragraph().CreatePdf(DEST);
        }

        public virtual void CreatePdf(String dest) {
            // Initialize PDF document
            PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
            // Initialize document
            Document document = new Document(pdf);
            // Add content
            PdfFont font = PdfFontFactory.CreateFont(StandardFonts.TIMES_ROMAN);
            PdfFont bold = PdfFontFactory.CreateFont(StandardFonts.TIMES_BOLD);
            Text title = new Text("The Strange Case of Dr. Jekyll and Mr. Hyde").SetFont(bold);
            Text author = new Text("Robert Louis Stevenson").SetFont(font);
            Paragraph p = new Paragraph().Add(title).Add(" by ").Add(author);
            document.Add(p);
            //Close document
            document.Close();
        }
    }
}

c01e02_text_paragraph_cardo

JAVA
JAVA
package com.itextpdf.highlevel.chapter01;

import com.itextpdf.io.font.FontProgram;
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.font.PdfFontFactory.EmbeddingStrategy;
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.layout.element.Text;
import java.io.File;
import java.io.IOException;

/**
 * @author Bruno Lowagie (iText Software)
 */
public class C01E02_Text_Paragraph_Cardo {
    
    public static final String DEST = "results/chapter01/text_paragraph_cardo.pdf";
    
    public static final String REGULAR = "src/main/resources/fonts/Cardo-Regular.ttf";
    public static final String BOLD = "src/main/resources/fonts/Cardo-Bold.ttf";
    public static final String ITALIC = "src/main/resources/fonts/Cardo-Italic.ttf";

    public static void main(String args[]) throws IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new C01E02_Text_Paragraph_Cardo().createPdf(DEST);
    }
    
    public void createPdf(String dest) throws IOException {
        // Initialize PDF document
        PdfDocument pdf = new PdfDocument(new PdfWriter(dest));

        // Initialize document
        Document document = new Document(pdf);
        
        // Add content
        FontProgram fontProgram = FontProgramFactory.createFont(REGULAR);
        PdfFont font = PdfFontFactory.createFont(fontProgram, PdfEncodings.WINANSI, EmbeddingStrategy.PREFER_EMBEDDED);
        PdfFont bold = PdfFontFactory.createFont(BOLD, PdfEncodings.WINANSI, EmbeddingStrategy.PREFER_EMBEDDED);
        PdfFont italic = PdfFontFactory.createFont(ITALIC, PdfEncodings.WINANSI, EmbeddingStrategy.PREFER_EMBEDDED);
        Text title = new Text("The Strange Case of Dr. Jekyll and Mr. Hyde").setFont(bold);
        Text author = new Text("Robert Louis Stevenson").setFont(font);
        Paragraph p = new Paragraph().setFont(italic).add(title).add(" by ").add(author);
        document.add(p);
        
        //Close document
        document.close();
    }
}
C#
C#
using System;
using System.IO;
using iText.Kernel.Font;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using iText.Licensing.Base;

namespace iText.Highlevel.Chapter01 {
    /// <author>Bruno Lowagie (iText Software)</author>
    public class C01E02_Text_Paragraph_Cardo {
        public static String KEY = "../../../resources/license/itextkey-typography.xml";

        public const String DEST = "../../../results/chapter01/text_paragraph_cardo.pdf";

        public const String REGULAR = "../../../resources/fonts/Cardo-Regular.ttf";

        public const String BOLD = "../../../resources/fonts/Cardo-Bold.ttf";

        public const String ITALIC = "../../../resources/fonts/Cardo-Italic.ttf";

        public static void Main(String[] args) {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();
            new C01E02_Text_Paragraph_Cardo().CreatePdf(DEST);
        }

        public virtual void CreatePdf(String dest) {
            LicenseKey.LoadLicenseFile(new FileStream(KEY, FileMode.Open, FileAccess.Read));
            // Initialize PDF document
            PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
            // Initialize document
            Document document = new Document(pdf);
            // Add content
            PdfFont font = PdfFontFactory.CreateFont(REGULAR, PdfFontFactory.EmbeddingStrategy.PREFER_EMBEDDED);
            PdfFont bold = PdfFontFactory.CreateFont(BOLD, PdfFontFactory.EmbeddingStrategy.PREFER_EMBEDDED);
            PdfFont italic = PdfFontFactory.CreateFont(ITALIC, PdfFontFactory.EmbeddingStrategy.PREFER_EMBEDDED);
            Text title = new Text("The Strange Case of Dr. Jekyll and Mr. Hyde").SetFont(bold);
            Text author = new Text("Robert Louis Stevenson").SetFont(font);
            Paragraph p = new Paragraph().SetFont(italic).Add(title).Add(" by ").Add(author);
            document.Add(p);
            //Close document
            document.Close();
        }
    }
}

c01e02_text_paragraph_cardo2

JAVA
JAVA
package com.itextpdf.highlevel.chapter01;

import com.itextpdf.io.font.FontProgram;
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.font.PdfFontFactory.EmbeddingStrategy;
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.layout.element.Text;

import java.io.File;
import java.io.IOException;

/**
 * @author Bruno Lowagie (iText Software)
 */
public class C01E02_Text_Paragraph_Cardo2 {
    
    public static final String DEST = "results/chapter01/text_paragraph_cardo%s.pdf";
    
    public static final String REGULAR = "src/main/resources/fonts/Cardo-Regular.ttf";
    public static final String BOLD = "src/main/resources/fonts/Cardo-Bold.ttf";
    public static final String ITALIC = "src/main/resources/fonts/Cardo-Italic.ttf";
    
    protected PdfFont font;
    protected PdfFont bold;
    protected PdfFont italic;
    
    public static void main(String args[]) throws IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        C01E02_Text_Paragraph_Cardo2 app = new C01E02_Text_Paragraph_Cardo2();
        FontProgram fontProgram = FontProgramFactory.createFont(REGULAR);
        FontProgram boldProgram = FontProgramFactory.createFont(BOLD);
        FontProgram italicProgram = FontProgramFactory.createFont(ITALIC);
        for (int i = 0; i < 3; ) {
            app.font = PdfFontFactory.createFont(fontProgram, PdfEncodings.WINANSI, EmbeddingStrategy.PREFER_EMBEDDED);
            app.bold = PdfFontFactory.createFont(boldProgram, PdfEncodings.WINANSI, EmbeddingStrategy.PREFER_EMBEDDED);
            app.italic = PdfFontFactory.createFont(italicProgram, PdfEncodings.WINANSI, EmbeddingStrategy.PREFER_EMBEDDED);
            app.createPdf(String.format(DEST, ++i));
        }
    }
    
    public void createPdf(String dest) throws IOException {
        // Initialize PDF document
        PdfDocument pdf = new PdfDocument(new PdfWriter(dest));

        // Initialize document
        Document document = new Document(pdf);
        
        // Add content
        Text title = new Text("The Strange Case of Dr. Jekyll and Mr. Hyde").setFont(bold);
        Text author = new Text("Robert Louis Stevenson").setFont(font);
        Paragraph p = new Paragraph().setFont(italic).add(title).add(" by ").add(author);
        document.add(p);
        
        //Close document
        document.close();
    }
}
C#
C#
using System;
using System.IO;
using iText.IO.Font;
using iText.Kernel.Font;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using iText.Licensing.Base;
using static iText.Kernel.Font.PdfFontFactory;


namespace iText.Highlevel.Chapter01 {
    /// <author>Bruno Lowagie (iText Software)</author>

    public class C01E02_Text_Paragraph_Cardo2 {
        
        public const String DEST = "../../../results/chapter01/text_paragraph_cardo{0}.pdf";
        public const String REGULAR = "../../../resources/fonts/Cardo-Regular.ttf";
        public const String BOLD = "../../../resources/fonts/Cardo-Bold.ttf";
        public const String ITALIC = "../../../resources/fonts/Cardo-Italic.ttf";

        protected PdfFont font;
        protected PdfFont bold;
        protected PdfFont italic;
        
        public static void Main(String[] args) {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();
            C01E02_Text_Paragraph_Cardo2 app = new C01E02_Text_Paragraph_Cardo2();
            FontProgram fontProgram = FontProgramFactory.CreateFont(REGULAR);
            FontProgram boldProgram = FontProgramFactory.CreateFont(BOLD);
            FontProgram italicProgram = FontProgramFactory.CreateFont(ITALIC);
            for (int i = 0; i < 3; ) {
                app.font = PdfFontFactory.CreateFont(fontProgram, PdfEncodings.WINANSI, EmbeddingStrategy.PREFER_EMBEDDED);
                app.bold = PdfFontFactory.CreateFont(boldProgram, PdfEncodings.WINANSI, EmbeddingStrategy.PREFER_EMBEDDED);
                app.italic = PdfFontFactory.CreateFont(italicProgram, PdfEncodings.WINANSI, EmbeddingStrategy.PREFER_EMBEDDED);
                app.CreatePdf(String.Format(DEST, ++i));
            }
        }

        public virtual void CreatePdf(String dest) {
            //Initialize PDF document
            PdfDocument pdf = new PdfDocument(new PdfWriter(dest));

            // Initialize document
            Document document = new Document(pdf);
            
            // Add content
            Text title = new Text("The Strange Case of Dr. Jekyll and Mr. Hyde").SetFont(bold);
            Text author = new Text("Robert Louis Stevenson").SetFont(font);
            Paragraph p = new Paragraph().SetFont(italic).Add(title).Add(" by ").Add(author);
            document.Add(p);
        
            //Close document
            document.Close();
        }
    }
}

c01e03_text_paragraph_nocardo

JAVA
JAVA
package com.itextpdf.highlevel.chapter01;

import com.itextpdf.io.font.PdfEncodings;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.font.PdfFontFactory.EmbeddingStrategy;
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.layout.element.Text;
import java.io.File;
import java.io.IOException;

/**
 * @author Bruno Lowagie (iText Software)
 */
public class C01E03_Text_Paragraph_NoCardo {
    
    public static final String DEST = "results/chapter01/text_paragraph_no_cardo.pdf";
    
    public static final String REGULAR = "src/main/resources/fonts/Cardo-Regular.ttf";
    public static final String BOLD = "src/main/resources/fonts/Cardo-Bold.ttf";
    public static final String ITALIC = "src/main/resources/fonts/Cardo-Italic.ttf";
    
    public static void main(String args[]) throws IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new C01E03_Text_Paragraph_NoCardo().createPdf(DEST);
    }
    
    public void createPdf(String dest) throws IOException {
        // Initialize PDF document
        PdfDocument pdf = new PdfDocument(new PdfWriter(dest));

        // Initialize document
        Document document = new Document(pdf);
        
        // Add content: the fonts aren't embedded! Don't do this!
        PdfFont font = PdfFontFactory.createFont(REGULAR, PdfEncodings.WINANSI, EmbeddingStrategy.PREFER_NOT_EMBEDDED);
        PdfFont bold = PdfFontFactory.createFont(BOLD, PdfEncodings.WINANSI, EmbeddingStrategy.PREFER_NOT_EMBEDDED);
        PdfFont italic = PdfFontFactory.createFont(ITALIC, PdfEncodings.WINANSI, EmbeddingStrategy.PREFER_NOT_EMBEDDED);
        Text title = new Text("The Strange Case of Dr. Jekyll and Mr. Hyde").setFont(bold);
        Text author = new Text("Robert Louis Stevenson").setFont(font);
        Paragraph p = new Paragraph().setFont(italic).add(title).add(" by ").add(author);
        document.add(p);
        
        //Close document
        document.close();
    }
}
C#
C#
using System;
using System.IO;
using iText.Kernel.Font;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using iText.Licensing.Base;

namespace iText.Highlevel.Chapter01 {
    /// <author>Bruno Lowagie (iText Software)</author>
    public class C01E03_Text_Paragraph_NoCardo {
        public static String KEY = "../../../resources/license/itextkey-typography.xml";

        public const String DEST = "../../../results/chapter01/text_paragraph_no_cardo.pdf";

        public const String REGULAR = "../../../resources/fonts/Cardo-Regular.ttf";

        public const String BOLD = "../../../resources/fonts/Cardo-Bold.ttf";

        public const String ITALIC = "../../../resources/fonts/Cardo-Italic.ttf";

        public static void Main(String[] args) {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();
            new C01E03_Text_Paragraph_NoCardo().CreatePdf(DEST);
        }

        public virtual void CreatePdf(String dest) {
            LicenseKey.LoadLicenseFile(new FileStream(KEY, FileMode.Open, FileAccess.Read));
            // Initialize PDF document
            PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
            // Initialize document
            Document document = new Document(pdf);
            // Add content: the fonts aren't embedded! Don't do this!
            PdfFont font = PdfFontFactory.CreateFont(REGULAR);
            PdfFont bold = PdfFontFactory.CreateFont(BOLD);
            PdfFont italic = PdfFontFactory.CreateFont(ITALIC);
            Text title = new Text("The Strange Case of Dr. Jekyll and Mr. Hyde").SetFont(bold);
            Text author = new Text("Robert Louis Stevenson").SetFont(font);
            Paragraph p = new Paragraph().SetFont(italic).Add(title).Add(" by ").Add(author);
            document.Add(p);
            //Close document
            document.Close();
        }
    }
}

c01e04_czech_russian_korean_wrong

JAVA
JAVA
package com.itextpdf.highlevel.chapter01;

import com.itextpdf.io.font.constants.StandardFonts;
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 java.io.File;
import java.io.IOException;

/**
 * @author Bruno Lowagie (iText Software)
 */
public class C01E04_Czech_Russian_Korean_Wrong {
    
    public static final String DEST = "results/chapter01/czech_russian_korean_wrong.pdf";
    
    public static final String CZECH =
            "Podivn\u00fd p\u0159\u00edpad Dr. Jekylla a pana Hyda";
    public static final String RUSSIAN =
            "\u0421\u0442\u0440\u0430\u043d\u043d\u0430\u044f "
            + "\u0438\u0441\u0442\u043e\u0440\u0438\u044f "
            + "\u0434\u043e\u043a\u0442\u043e\u0440\u0430 "
            + "\u0414\u0436\u0435\u043a\u0438\u043b\u0430 \u0438 "
            + "\u043c\u0438\u0441\u0442\u0435\u0440\u0430 "
            + "\u0425\u0430\u0439\u0434\u0430";
    public static final String KOREAN =
            "\ud558\uc774\ub4dc, \uc9c0\ud0ac, \ub098";
    
    public static void main(String args[]) throws IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new C01E04_Czech_Russian_Korean_Wrong().createPdf(DEST);
    }
    
    public void createPdf(String dest) throws IOException {
        // Initialize PDF document
        PdfDocument pdf = new PdfDocument(new PdfWriter(dest));

        // Initialize document
        Document document = new Document(pdf);
        
        // Add content
        PdfFont font = PdfFontFactory.createFont(StandardFonts.TIMES_ROMAN);
        document.add(new Paragraph().setFont(font)
                .add(CZECH).add(" by Robert Louis Stevenson"));
        document.add(new Paragraph().setFont(font)
                .add(RUSSIAN).add(" by Robert Louis Stevenson"));
        document.add(new Paragraph().setFont(font)
                .add(KOREAN).add(" by Robert Louis Stevenson"));
        
        //Close document
        document.close();
    }
}
C#
C#
using System;
using System.IO;
using iText.IO.Font.Constants;
using iText.Kernel.Font;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;

namespace iText.Highlevel.Chapter01 {
    /// <author>Bruno Lowagie (iText Software)</author>
    public class C01E04_Czech_Russian_Korean_Wrong {
        public const String DEST = "../../../results/chapter01/czech_russian_korean_wrong.pdf";

        public const String CZECH = "Podivn\u00fd p\u0159\u00edpad Dr. Jekylla a pana Hyda";

        public const String RUSSIAN = "\u0421\u0442\u0440\u0430\u043d\u043d\u0430\u044f " + "\u0438\u0441\u0442\u043e\u0440\u0438\u044f "
             + "\u0434\u043e\u043a\u0442\u043e\u0440\u0430 " + "\u0414\u0436\u0435\u043a\u0438\u043b\u0430 \u0438 "
             + "\u043c\u0438\u0441\u0442\u0435\u0440\u0430 " + "\u0425\u0430\u0439\u0434\u0430";

        public const String KOREAN = "\ud558\uc774\ub4dc, \uc9c0\ud0ac, \ub098";

        public static void Main(String[] args) {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();
            new C01E04_Czech_Russian_Korean_Wrong().CreatePdf(DEST);
        }

        public virtual void CreatePdf(String dest) {
            // Initialize PDF document
            PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
            // Initialize document
            Document document = new Document(pdf);
            // Add content
            PdfFont font = PdfFontFactory.CreateFont(StandardFonts.TIMES_ROMAN);
            document.Add(new Paragraph().SetFont(font).Add(CZECH).Add(" by Robert Louis Stevenson"));
            document.Add(new Paragraph().SetFont(font).Add(RUSSIAN).Add(" by Robert Louis Stevenson"));
            document.Add(new Paragraph().SetFont(font).Add(KOREAN).Add(" by Robert Louis Stevenson"));
            //Close document
            document.Close();
        }
    }
}

c01e05_czech_russian_korean_right

JAVA
JAVA
package com.itextpdf.highlevel.chapter01;

import com.itextpdf.io.font.PdfEncodings;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.font.PdfFontFactory.EmbeddingStrategy;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;
import java.io.File;
import java.io.IOException;

/**
 * @author Bruno Lowagie (iText Software)
 */
public class C01E05_Czech_Russian_Korean_Right {

    public static final String DEST = "results/chapter01/czech_russian_korean.pdf";

    public static final String FONT = "src/main/resources/fonts/FreeSans.ttf";
    public static final String HCRBATANG = "src/main/resources/fonts/HANBatang.ttf";

    public static final String CZECH =
            "Podivn\u00fd p\u0159\u00edpad Dr. Jekylla a pana Hyda";
    public static final String RUSSIAN =
            "\u0421\u0442\u0440\u0430\u043d\u043d\u0430\u044f "
            + "\u0438\u0441\u0442\u043e\u0440\u0438\u044f "
            + "\u0434\u043e\u043a\u0442\u043e\u0440\u0430 "
            + "\u0414\u0436\u0435\u043a\u0438\u043b\u0430 \u0438 "
            + "\u043c\u0438\u0441\u0442\u0435\u0440\u0430 "
            + "\u0425\u0430\u0439\u0434\u0430";
    public static final String KOREAN =
            "\ud558\uc774\ub4dc, \uc9c0\ud0ac, \ub098";

    public static void main(String args[]) throws IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new C01E05_Czech_Russian_Korean_Right().createPdf(DEST);
    }

    public void createPdf(String dest) throws IOException {
        // Initialize PDF document
        PdfDocument pdf = new PdfDocument(new PdfWriter(dest));

        // Initialize document
        Document document = new Document(pdf);

        // Add content
        PdfFont font1250 = PdfFontFactory.createFont(FONT, PdfEncodings.CP1250, EmbeddingStrategy.PREFER_EMBEDDED);
        document.add(new Paragraph().setFont(font1250)
                .add(CZECH).add(" by Robert Louis Stevenson"));
        PdfFont font1251 = PdfFontFactory.createFont(FONT, "Cp1251", EmbeddingStrategy.PREFER_EMBEDDED);
        document.add(new Paragraph().setFont(font1251)
                .add(RUSSIAN).add(" by Robert Louis Stevenson"));
        PdfFont fontUnicode = PdfFontFactory.createFont(HCRBATANG, PdfEncodings.IDENTITY_H);
        document.add(new Paragraph().setFont(fontUnicode)
                .add(KOREAN).add(" by Robert Louis Stevenson"));

        //Close document
        document.close();
    }
}
C#
C#
using System;
using System.IO;
using iText.IO.Font;
using iText.Kernel.Font;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using iText.Licensing.Base;

namespace iText.Highlevel.Chapter01 {
    /// <author>Bruno Lowagie (iText Software)</author>
    public class C01E05_Czech_Russian_Korean_Right {
        public static String KEY = "../../../resources/license/itextkey-typography.xml";
        
        public const String DEST = "../../../results/chapter01/czech_russian_korean.pdf";

        public const String FONT = "../../../resources/fonts/FreeSans.ttf";

        public const String HCRBATANG = "../../../resources/fonts/HANBatang.ttf";

        public const String CZECH = "Podivn\u00fd p\u0159\u00edpad Dr. Jekylla a pana Hyda";

        public const String RUSSIAN = "\u0421\u0442\u0440\u0430\u043d\u043d\u0430\u044f " + "\u0438\u0441\u0442\u043e\u0440\u0438\u044f "
             + "\u0434\u043e\u043a\u0442\u043e\u0440\u0430 " + "\u0414\u0436\u0435\u043a\u0438\u043b\u0430 \u0438 "
             + "\u043c\u0438\u0441\u0442\u0435\u0440\u0430 " + "\u0425\u0430\u0439\u0434\u0430";

        public const String KOREAN = "\ud558\uc774\ub4dc, \uc9c0\ud0ac, \ub098";

        public static void Main(String[] args) {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();
            LicenseKey.LoadLicenseFile(new FileStream(KEY, FileMode.Open, FileAccess.Read));
            new C01E05_Czech_Russian_Korean_Right().CreatePdf(DEST);
        }

        public virtual void CreatePdf(String dest) {
            // Initialize PDF document
            PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
            // Initialize document
            Document document = new Document(pdf);
            // Add content
            PdfFont font1250 = PdfFontFactory.CreateFont(FONT, PdfEncodings.CP1250, 
                PdfFontFactory.EmbeddingStrategy.PREFER_EMBEDDED);
            document.Add(new Paragraph().SetFont(font1250).Add(CZECH).Add(" by Robert Louis Stevenson"));
            PdfFont font1251 = PdfFontFactory.CreateFont(FONT, "Cp1251", 
                PdfFontFactory.EmbeddingStrategy.PREFER_EMBEDDED);
            document.Add(new Paragraph().SetFont(font1251).Add(RUSSIAN).Add(" by Robert Louis Stevenson"));
            PdfFont fontUnicode = PdfFontFactory.CreateFont(HCRBATANG, PdfEncodings.IDENTITY_H);
            document.Add(new Paragraph().SetFont(fontUnicode).Add(KOREAN).Add(" by Robert Louis Stevenson"));
            //Close document
            document.Close();
        }
    }
}

c01e06_czech_russian_korean_unicode

JAVA
JAVA
package com.itextpdf.highlevel.chapter01;

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 java.io.File;
import java.io.IOException;

/**
 * @author Bruno Lowagie (iText Software)
 */
public class C01E06_Czech_Russian_Korean_Unicode {

    public static final String DEST = "results/chapter01/czech_russian_korean_unicode.pdf";

    public static final String FONT = "src/main/resources/fonts/FreeSans.ttf";
    public static final String HCRBATANG = "src/main/resources/fonts/HANBatang.ttf";

    public static final String CZECH =
            "Podivn\u00fd p\u0159\u00edpad Dr. Jekylla a pana Hyda";
    public static final String RUSSIAN =
            "\u0421\u0442\u0440\u0430\u043d\u043d\u0430\u044f "
            + "\u0438\u0441\u0442\u043e\u0440\u0438\u044f "
            + "\u0434\u043e\u043a\u0442\u043e\u0440\u0430 "
            + "\u0414\u0436\u0435\u043a\u0438\u043b\u0430 \u0438 "
            + "\u043c\u0438\u0441\u0442\u0435\u0440\u0430 "
            + "\u0425\u0430\u0439\u0434\u0430";
    public static final String KOREAN =
            "\ud558\uc774\ub4dc, \uc9c0\ud0ac, \ub098";

    public static void main(String args[]) throws IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new C01E06_Czech_Russian_Korean_Unicode().createPdf(DEST);
    }

    public void createPdf(String dest) throws IOException {
        // Initialize PDF document
        PdfDocument pdf = new PdfDocument(new PdfWriter(dest));

        // Initialize document
        Document document = new Document(pdf);

        // Add content
        PdfFont freeUnicode = PdfFontFactory.createFont(FONT, PdfEncodings.IDENTITY_H);
        document.add(new Paragraph().setFont(freeUnicode)
                .add(CZECH).add(" by Robert Louis Stevenson"));
        document.add(new Paragraph().setFont(freeUnicode)
                .add(RUSSIAN).add(" by Robert Louis Stevenson"));
        PdfFont fontUnicode = PdfFontFactory.createFont(HCRBATANG, PdfEncodings.IDENTITY_H);
        document.add(new Paragraph().setFont(fontUnicode)
                .add(KOREAN).add(" by Robert Louis Stevenson"));

        //Close document
        document.close();
    }
}
C#
C#
using System;
using System.IO;
using iText.IO.Font;
using iText.Kernel.Font;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using iText.Licensing.Base;

namespace iText.Highlevel.Chapter01 {
    /// <author>Bruno Lowagie (iText Software)</author>
    public class C01E06_Czech_Russian_Korean_Unicode {
        public static String KEY = "../../../resources/license/itextkey-typography.xml";

        public const String DEST = "../../../results/chapter01/czech_russian_korean_unicode.pdf";

        public const String FONT = "../../../resources/fonts/FreeSans.ttf";

        public const String HCRBATANG = "../../../resources/fonts/HANBatang.ttf";

        public const String CZECH = "Podivn\u00fd p\u0159\u00edpad Dr. Jekylla a pana Hyda";

        public const String RUSSIAN = "\u0421\u0442\u0440\u0430\u043d\u043d\u0430\u044f " + "\u0438\u0441\u0442\u043e\u0440\u0438\u044f "
             + "\u0434\u043e\u043a\u0442\u043e\u0440\u0430 " + "\u0414\u0436\u0435\u043a\u0438\u043b\u0430 \u0438 "
             + "\u043c\u0438\u0441\u0442\u0435\u0440\u0430 " + "\u0425\u0430\u0439\u0434\u0430";

        public const String KOREAN = "\ud558\uc774\ub4dc, \uc9c0\ud0ac, \ub098";

        public static void Main(String[] args) {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();
            LicenseKey.LoadLicenseFile(new FileStream(KEY, FileMode.Open, FileAccess.Read));
            new C01E06_Czech_Russian_Korean_Unicode().CreatePdf(DEST);
        }

        public virtual void CreatePdf(String dest) {
            // Initialize PDF document
            PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
            // Initialize document
            Document document = new Document(pdf);
            // Add content
            PdfFont freeUnicode = PdfFontFactory.CreateFont(FONT, PdfEncodings.IDENTITY_H);
            document.Add(new Paragraph().SetFont(freeUnicode).Add(CZECH).Add(" by Robert Louis Stevenson"));
            document.Add(new Paragraph().SetFont(freeUnicode).Add(RUSSIAN).Add(" by Robert Louis Stevenson"));
            PdfFont fontUnicode = PdfFontFactory.CreateFont(HCRBATANG, PdfEncodings.IDENTITY_H);
            document.Add(new Paragraph().SetFont(fontUnicode).Add(KOREAN).Add(" by Robert Louis Stevenson"));
            //Close document
            document.Close();
        }
    }
}

c01e07_fontsize

JAVA
JAVA
package com.itextpdf.highlevel.chapter01;

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.layout.element.Text;
import java.io.File;
import java.io.IOException;

/**
 * @author Bruno Lowagie (iText Software)
 */
public class C01E07_FontSize {
    
    public static final String DEST = "results/chapter01/font_size.pdf";
    
    public static void main(String args[]) throws IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new C01E07_FontSize().createPdf(DEST);
    }
    
    public void createPdf(String dest) throws IOException {
        // Initialize PDF document
        PdfDocument pdf = new PdfDocument(new PdfWriter(dest));

        // Initialize document
        Document document = new Document(pdf);
        
        // Add content
        Text title1 = new Text("The Strange Case of ").setFontSize(12);
        Text title2 = new Text("Dr. Jekyll and Mr. Hyde").setFontSize(16);
        Text author = new Text("Robert Louis Stevenson");
        Paragraph p = new Paragraph().setFontSize(8)
                .add(title1).add(title2).add(" by ").add(author);
        document.add(p);
        
        //Close document
        document.close();
    }
}
C#
C#
using System;
using System.IO;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;

namespace iText.Highlevel.Chapter01 {
    /// <author>Bruno Lowagie (iText Software)</author>
    public class C01E07_FontSize {
        public const String DEST = "../../../results/chapter01/font_size.pdf";

        public static void Main(String[] args) {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();
            new C01E07_FontSize().CreatePdf(DEST);
        }

        public virtual void CreatePdf(String dest) {
            // Initialize PDF document
            PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
            // Initialize document
            Document document = new Document(pdf);
            // Add content
            Text title1 = new Text("The Strange Case of ").SetFontSize(12);
            Text title2 = new Text("Dr. Jekyll and Mr. Hyde").SetFontSize(16);
            Text author = new Text("Robert Louis Stevenson");
            Paragraph p = new Paragraph().SetFontSize(8).Add(title1).Add(title2).Add(" by ").Add(author);
            document.Add(p);
            //Close document
            document.Close();
        }
    }
}

c01e08_bolditalic

JAVA
JAVA
package com.itextpdf.highlevel.chapter01;

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.layout.element.Text;
import java.io.File;
import java.io.IOException;

/**
 * @author Bruno Lowagie (iText Software)
 */
public class C01E08_BoldItalic {
    
    public static final String DEST = "results/chapter01/bold_italic.pdf";
    
    public static void main(String args[]) throws IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new C01E08_BoldItalic().createPdf(DEST);
    }
    
    public void createPdf(String dest) throws IOException {
        // Initialize PDF document
        PdfDocument pdf = new PdfDocument(new PdfWriter(dest));

        // Initialize document
        Document document = new Document(pdf);
        
        // Add content
        Text title1 = new Text("The Strange Case of ").setItalic();
        Text title2 = new Text("Dr. Jekyll and Mr. Hyde").setBold();
        Text author = new Text("Robert Louis Stevenson").setItalic().setBold();
        Paragraph p = new Paragraph()
                .add(title1).add(title2).add(" by ").add(author);
        document.add(p);
        
        //Close document
        document.close();
    }
}
C#
C#
using System;
using System.IO;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;

namespace iText.Highlevel.Chapter01 {
    /// <author>Bruno Lowagie (iText Software)</author>
    public class C01E08_BoldItalic {
        public const String DEST = "../../../results/chapter01/bold_italic.pdf";

        public static void Main(String[] args) {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();
            new C01E08_BoldItalic().CreatePdf(DEST);
        }

        public virtual void CreatePdf(String dest) {
            // Initialize PDF document
            PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
            // Initialize document
            Document document = new Document(pdf);
            // Add content
            Text title1 = new Text("The Strange Case of ").SetItalic();
            Text title2 = new Text("Dr. Jekyll and Mr. Hyde").SetBold();
            Text author = new Text("Robert Louis Stevenson").SetItalic().SetBold();
            Paragraph p = new Paragraph().Add(title1).Add(title2).Add(" by ").Add(author);
            document.Add(p);
            //Close document
            document.Close();
        }
    }
}

c01e09_colorrendering

JAVA
JAVA
package com.itextpdf.highlevel.chapter01;

import com.itextpdf.kernel.colors.ColorConstants;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.canvas.PdfCanvasConstants;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Text;
import java.io.File;
import java.io.IOException;

/**
 * @author Bruno Lowagie (iText Software)
 */
public class C01E09_ColorRendering {

    public static final String DEST = "results/chapter01/color_rendermode.pdf";

    public static void main(String args[]) throws IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new C01E09_ColorRendering().createPdf(DEST);
    }

    public void createPdf(String dest) throws IOException {
        // Initialize PDF document
        PdfDocument pdf = new PdfDocument(new PdfWriter(dest));

        // Initialize document
        Document document = new Document(pdf);

        // Add content
        Text title1 = new Text("The Strange Case of ").setFontColor(ColorConstants.BLUE);
        Text title2 = new Text("Dr. Jekyll")
                .setStrokeColor(ColorConstants.GREEN)
                .setTextRenderingMode(PdfCanvasConstants.TextRenderingMode.FILL_STROKE);
        Text title3 = new Text(" and ");
        Text title4 = new Text("Mr. Hyde")
                .setStrokeColor(ColorConstants.RED).setStrokeWidth(0.5f)
                .setTextRenderingMode(PdfCanvasConstants.TextRenderingMode.STROKE);
        Paragraph p = new Paragraph().setFontSize(24)
                .add(title1).add(title2).add(title3).add(title4);
        document.add(p);

        //Close document
        document.close();
    }
}
C#
C#
using System;
using System.IO;
using iText.Kernel.Colors;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Canvas;
using iText.Layout;
using iText.Layout.Element;

namespace iText.Highlevel.Chapter01 {
    /// <author>Bruno Lowagie (iText Software)</author>
    public class C01E09_ColorRendering {
        public const String DEST = "../../../results/chapter01/color_rendermode.pdf";

        public static void Main(String[] args) {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();
            new C01E09_ColorRendering().CreatePdf(DEST);
        }

        public virtual void CreatePdf(String dest) {
            // Initialize PDF document
            PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
            // Initialize document
            Document document = new Document(pdf);
            // Add content
            Text title1 = new Text("The Strange Case of ").SetFontColor(ColorConstants.BLUE);
            Text title2 = new Text("Dr. Jekyll").SetStrokeColor(ColorConstants.GREEN).SetTextRenderingMode(PdfCanvasConstants.TextRenderingMode
                .FILL_STROKE);
            Text title3 = new Text(" and ");
            Text title4 = new Text("Mr. Hyde").SetStrokeColor(ColorConstants.RED).SetStrokeWidth(0.5f).SetTextRenderingMode(PdfCanvasConstants.TextRenderingMode
                .STROKE);
            Paragraph p = new Paragraph().SetFontSize(24).Add(title1).Add(title2).Add(title3).Add(title4);
            document.Add(p);
            //Close document
            document.Close();
        }
    }
}

c01e10_reusingstyles

JAVA
JAVA
package com.itextpdf.highlevel.chapter01;

import com.itextpdf.io.font.constants.StandardFonts;
import com.itextpdf.kernel.colors.ColorConstants;
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.Style;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Text;
import java.io.File;
import java.io.IOException;

/**
 * @author Bruno Lowagie (iText Software)
 */
public class C01E10_ReusingStyles {

    public static final String DEST = "results/chapter01/style_example.pdf";

    public static void main(String args[]) throws IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new C01E10_ReusingStyles().createPdf(DEST);
    }
    public void createPdf(String dest) throws IOException {
        PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
        Document document = new Document(pdf);
        Style normal = new Style();
        PdfFont font = PdfFontFactory.createFont(StandardFonts.TIMES_ROMAN);
        normal.setFont(font).setFontSize(14);
        Style code = new Style();
        PdfFont monospace = PdfFontFactory.createFont(StandardFonts.COURIER);
        code.setFont(monospace).setFontColor(ColorConstants.RED)
                .setBackgroundColor(ColorConstants.LIGHT_GRAY);
        Paragraph p = new Paragraph();
        p.add(new Text("The Strange Case of ").addStyle(normal));
        p.add(new Text("Dr. Jekyll").addStyle(code));
        p.add(new Text(" and ").addStyle(normal));
        p.add(new Text("Mr. Hyde").addStyle(code));
        p.add(new Text(".").addStyle(normal));
        document.add(p);
        document.close();
    }
}
C#
C#
using System;
using System.IO;
using iText.IO.Font.Constants;
using iText.Kernel.Colors;
using iText.Kernel.Font;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;

namespace iText.Highlevel.Chapter01 {
    /// <author>Bruno Lowagie (iText Software)</author>
    public class C01E10_ReusingStyles {
        public const String DEST = "../../../results/chapter01/style_example.pdf";

        public static void Main(String[] args) {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();
            new C01E10_ReusingStyles().CreatePdf(DEST);
        }

        public virtual void CreatePdf(String dest) {
            PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
            Document document = new Document(pdf);
            Style normal = new Style();
            PdfFont font = PdfFontFactory.CreateFont(StandardFonts.TIMES_ROMAN);
            normal.SetFont(font).SetFontSize(14);
            Style code = new Style();
            PdfFont monospace = PdfFontFactory.CreateFont(StandardFonts.COURIER);
            code.SetFont(monospace).SetFontColor(ColorConstants.RED).SetBackgroundColor(ColorConstants.LIGHT_GRAY);
            Paragraph p = new Paragraph();
            p.Add(new Text("The Strange Case of ").AddStyle(normal));
            p.Add(new Text("Dr. Jekyll").AddStyle(code));
            p.Add(new Text(" and ").AddStyle(normal));
            p.Add(new Text("Mr. Hyde").AddStyle(code));
            p.Add(new Text(".").AddStyle(normal));
            document.Add(p);
            document.Close();
        }
    }
}

Resources

Cardo-Bold.ttf
Cardo-Italic.ttf
Cardo-Regular.ttf
SIL.txt
FreeSans.ttf
HANBatang.ttf
gpl-3.0.txt
hancom.txt

Results

cmp_text_paragraph.pdf

cmp_text_paragraph_cardo.pdf

cmp_text_paragraph_no_cardo.pdf

cmp_czech_russian_korean_wrong.pdf

cmp_czech_russian_korean.pdf

cmp_czech_russian_korean_unicode.pdf

cmp_font_size.pdf

cmp_bold_italic.pdf

cmp_color_rendermode.pdf

cmp_style_example.pdf

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.