iText7高级教程之构建基础块——1.引入字体&实践

    本章的例子,请参考我翻译的博文:iText7高级教程之构建基础块——1.引入字体,里面有详细的解释,有什么不懂得也可以评论或者私信我!

例子1:创建不嵌入的三种字体的文档

    三种不同的字体来创建带有标题和作者的PDF文档,三种字体是:Helvetica、Times-Bold和Times-Roman。代码如下

import com.itextpdf.io.font.FontConstants;
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;

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(FontConstants.TIMES_ROMAN);
        PdfFont bold = PdfFontFactory.createFont(FontConstants.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();
    }
}

例子2:创建嵌入字体的三种字体文档

    使用ttf文件,创建Cardo-Regular,Cardo-Bold和Cardo-Italic字体,代码如下:

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.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;


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, true);
        PdfFont bold = PdfFontFactory.createFont(BOLD, true);
        PdfFont italic = PdfFontFactory.createFont(ITALIC, true);
        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();
    }
}

    一个PdfFont对象只能被用于一个PdfDocument,所以我们创建三个PdfFont来创建三个文档,代码如下:

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.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;

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, true);
            app.bold = PdfFontFactory.createFont(boldProgram, PdfEncodings.WINANSI, true);
            app.italic = PdfFontFactory.createFont(italicProgram, PdfEncodings.WINANSI, true);
            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();
    }
}

例子3:不嵌入字体

    如果我们不把字体嵌入到文档中,那结果就会显示不正确,代码如下:

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;

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

例子4:错误地创建捷克文、俄文和韩文

    我们尝试创建带有捷克文、俄文和韩文的文档,但是没有字体程序,就会显示错误,代码如下:

import com.itextpdf.io.font.FontConstants;
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;

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

例子5:正确地创建捷克文、俄文和韩文

    我们尝试创建带有捷克文、俄文和韩文的文档,同时使用兼容的字体程序,就会正确了,编码使用的是代码页,代码如下:

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;


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, true);
        document.add(new Paragraph().setFont(font1250)
                .add(CZECH).add(" by Robert Louis Stevenson"));
        PdfFont font1251 = PdfFontFactory.createFont(FONT, "Cp1251", true);
        document.add(new Paragraph().setFont(font1251)
                .add(RUSSIAN).add(" by Robert Louis Stevenson"));
        PdfFont fontUnicode = PdfFontFactory.createFont(HCRBATANG, PdfEncodings.IDENTITY_H, true);
        document.add(new Paragraph().setFont(fontUnicode)
                .add(KOREAN).add(" by Robert Louis Stevenson"));

        //Close document
        document.close();
    }
}

例子6:使用Unicode创建捷克文、俄文和韩文的文档

    我们尝试创建带有捷克文、俄文和韩文的文档,同时使用兼容的字体程序,就会正确了,编码使用的是Unicode,代码如下:

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;


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, true);
        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, true);
        document.add(new Paragraph().setFont(fontUnicode)
                .add(KOREAN).add(" by Robert Louis Stevenson"));

        //Close document
        document.close();
    }
}

例子7:改变字体大小

    我们改变创建的字体的大小,代码如下:

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;


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

例子8:模仿粗体和斜体

    当我们没有字体相应的粗体和斜体的字体程序时,我们可以使用iText7里面的函数来模仿粗体和斜体,代码如下:

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;


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

例子9:改变字体颜色

    我们改变创建的字体的颜色,代码如下:

import com.itextpdf.kernel.color.Color;
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;


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(Color.BLUE);
        Text title2 = new Text("Dr. Jekyll")
                .setStrokeColor(Color.GREEN)
                .setTextRenderingMode(PdfCanvasConstants.TextRenderingMode.FILL_STROKE);
        Text title3 = new Text(" and ");
        Text title4 = new Text("Mr. Hyde")
                .setStrokeColor(Color.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();
    }
}

例子10:重用样式

    我们可以定义一个样式用于多个对象,代码如下:

import com.itextpdf.io.font.FontConstants;
import com.itextpdf.kernel.color.Color;
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;


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(FontConstants.TIMES_ROMAN);
        normal.setFont(font).setFontSize(14);
        Style code = new Style();
        PdfFont monospace = PdfFontFactory.createFont(FontConstants.COURIER);
        code.setFont(monospace).setFontColor(Color.RED)
                .setBackgroundColor(Color.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();
    }
}

Example代码下载

    本章代码可在如下地址下载(IDEA工程):iText7高级教程之构建基础块——第一章源代码工程

你可能感兴趣的:(java,iText,iText7)