Search in sources :

Example 1 with PdfFont

use of com.itextpdf.kernel.font.PdfFont in project spring-learning by moon-zhou.

the class FillTemplate method main.

public static void main(String[] args) {
    try {
        Map<String, String> params = new HashMap<>();
        params.put("applicant", "张三");
        params.put("name", "lisi");
        params.put("applicantId", "666");
        params.put("email", "1@1.com");
        String templatePath = "/Users/xxx/tmp/pdf/PDF 模板测试 1.pdf";
        String newPDFPath = "/Users/xxx/tmp/pdf/PDF 模板测试 2.pdf";
        String fontPath = "/Users/xxx/tmp/pdf/Alibaba-PuHuiTi-Regular.ttf";
        PdfDocument pdf = new PdfDocument(new PdfReader(templatePath), new PdfWriter(newPDFPath));
        /*PdfFont font = PdfFontFactory.createFont(this.getClass().getClassLoader().getResource("/").getPath()
                    + "font/Alibaba-PuHuiTi-Regular.ttf");*/
        PdfFont font = PdfFontFactory.createFont(fontPath);
        if (params != null && !params.isEmpty()) {
            // 有参数才替换
            PdfAcroForm form = PdfAcroForm.getAcroForm(pdf, true);
            Map<String, PdfFormField> fields = form.getFormFields();
            // 获取所有的表单域
            for (String param : params.keySet()) {
                PdfFormField formField = fields.get(param);
                // 获取某个表单域
                if (formField != null) {
                    // 替换值
                    formField.setFont(font).setValue(params.get(param));
                // 替换值
                // formField.setValue(params.get(param));
                }
            }
            // 锁定表单,不让修改
            form.flattenFields();
        }
        pdf.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : PdfAcroForm(com.itextpdf.forms.PdfAcroForm) PdfFormField(com.itextpdf.forms.fields.PdfFormField) PdfWriter(com.itextpdf.kernel.pdf.PdfWriter) HashMap(java.util.HashMap) PdfFont(com.itextpdf.kernel.font.PdfFont) PdfReader(com.itextpdf.kernel.pdf.PdfReader) PdfDocument(com.itextpdf.kernel.pdf.PdfDocument)

Example 2 with PdfFont

use of com.itextpdf.kernel.font.PdfFont in project i7js-highlevel by itext.

the class C04E05_ParagraphAndDiv2 method createPdf.

public void createPdf(String dest) throws IOException {
    // Initialize PDF document
    PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
    // Initialize document
    Document document = new Document(pdf);
    PdfFont font = PdfFontFactory.createFont(StandardFonts.TIMES_ROMAN);
    PdfFont bold = PdfFontFactory.createFont(StandardFonts.HELVETICA_BOLD);
    document.setTextAlignment(TextAlignment.JUSTIFIED).setHyphenation(new HyphenationConfig("en", "uk", 3, 3));
    BufferedReader br = new BufferedReader(new FileReader(SRC));
    String line;
    Div div = new Div();
    while ((line = br.readLine()) != null) {
        document.add(new Paragraph(line).setFont(bold).setFontSize(12).setMarginBottom(0).setKeepWithNext(true));
        div = new Div().setFont(font).setFontSize(11).setMarginBottom(18);
        while ((line = br.readLine()) != null) {
            div.add(new Paragraph(line).setMarginBottom(0).setFirstLineIndent(36));
            if (line.isEmpty()) {
                document.add(div);
                break;
            }
        }
    }
    document.add(div);
    // Close document
    document.close();
}
Also used : Div(com.itextpdf.layout.element.Div) PdfWriter(com.itextpdf.kernel.pdf.PdfWriter) PdfFont(com.itextpdf.kernel.font.PdfFont) BufferedReader(java.io.BufferedReader) HyphenationConfig(com.itextpdf.layout.hyphenation.HyphenationConfig) FileReader(java.io.FileReader) PdfDocument(com.itextpdf.kernel.pdf.PdfDocument) Document(com.itextpdf.layout.Document) PdfDocument(com.itextpdf.kernel.pdf.PdfDocument) Paragraph(com.itextpdf.layout.element.Paragraph)

Example 3 with PdfFont

use of com.itextpdf.kernel.font.PdfFont in project i7js-highlevel by itext.

the class C04E08_CustomListSymbols method createPdf.

public void createPdf(String dest) throws IOException {
    // Initialize PDF document
    PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
    // Initialize document
    PageSize pagesize = PageSize.A6.rotate();
    Document document = new Document(pdf, pagesize);
    // Set column parameters
    float offSet = 36;
    float gutter = 23;
    float columnWidth = (pagesize.getWidth() - offSet * 2) / 2 - gutter;
    float columnHeight = pagesize.getHeight() - offSet * 2;
    // Define column areas
    Rectangle[] columns = { new Rectangle(offSet, offSet, columnWidth, columnHeight), new Rectangle(offSet + columnWidth + gutter, offSet, columnWidth, columnHeight) };
    document.setRenderer(new ColumnDocumentRenderer(document, columns));
    List list = new List();
    list.setListSymbol("\u2022");
    list.add("Dr. Jekyll");
    list.add("Mr. Hyde");
    document.add(list);
    list = new List();
    PdfFont font = PdfFontFactory.createFont(StandardFonts.ZAPFDINGBATS);
    list.setListSymbol(new Text("*").setFont(font).setFontColor(ColorConstants.ORANGE));
    list.setSymbolIndent(10);
    list.add("Dr. Jekyll");
    list.add("Mr. Hyde");
    document.add(list);
    Image info = new Image(ImageDataFactory.create(INFO));
    info.scaleAbsolute(12, 12);
    list = new List().setSymbolIndent(3);
    list.setListSymbol(info);
    list.add("Dr. Jekyll");
    list.add("Mr. Hyde");
    document.add(list);
    list = new List();
    list.setListSymbol(ListNumberingType.ENGLISH_LOWER);
    list.setPostSymbolText("- ");
    list.add("Dr. Jekyll");
    list.add("Mr. Hyde");
    document.add(list);
    list = new List(ListNumberingType.DECIMAL);
    list.setPreSymbolText("Part ");
    list.setPostSymbolText(": ");
    list.add("Dr. Jekyll");
    list.add("Mr. Hyde");
    document.add(list);
    list = new List(ListNumberingType.DECIMAL);
    list.setItemStartIndex(5);
    list.add("Dr. Jekyll");
    list.add("Mr. Hyde");
    document.add(list);
    list = new List(ListNumberingType.ROMAN_LOWER);
    list.setListSymbolAlignment(ListSymbolAlignment.LEFT);
    for (int i = 0; i < 6; i++) {
        list.add("Dr. Jekyll");
        list.add("Mr. Hyde");
    }
    document.add(list);
    // Close document
    document.close();
}
Also used : PdfWriter(com.itextpdf.kernel.pdf.PdfWriter) PageSize(com.itextpdf.kernel.geom.PageSize) Rectangle(com.itextpdf.kernel.geom.Rectangle) Text(com.itextpdf.layout.element.Text) PdfDocument(com.itextpdf.kernel.pdf.PdfDocument) Document(com.itextpdf.layout.Document) Image(com.itextpdf.layout.element.Image) PdfDocument(com.itextpdf.kernel.pdf.PdfDocument) ColumnDocumentRenderer(com.itextpdf.layout.ColumnDocumentRenderer) PdfFont(com.itextpdf.kernel.font.PdfFont) List(com.itextpdf.layout.element.List)

Example 4 with PdfFont

use of com.itextpdf.kernel.font.PdfFont in project i7js-highlevel by itext.

the class C06E03_TOC_GoToPage method createPdf.

public void createPdf(String dest) throws IOException {
    // Initialize PDF document
    PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
    // Initialize document
    Document document = new Document(pdf);
    PdfFont font = PdfFontFactory.createFont(StandardFonts.TIMES_ROMAN);
    PdfFont bold = PdfFontFactory.createFont(StandardFonts.HELVETICA_BOLD);
    document.setTextAlignment(TextAlignment.JUSTIFIED).setHyphenation(new HyphenationConfig("en", "uk", 3, 3)).setFont(font).setFontSize(11);
    BufferedReader br = new BufferedReader(new FileReader(SRC));
    String name, line;
    Paragraph p;
    boolean title = true;
    int counter = 0;
    List<SimpleEntry<String, Integer>> toc = new ArrayList<>();
    while ((line = br.readLine()) != null) {
        p = new Paragraph(line);
        p.setKeepTogether(true);
        if (title) {
            name = String.format("title%02d", counter++);
            p.setFont(bold).setFontSize(12).setKeepWithNext(true).setDestination(name);
            title = false;
            document.add(p);
            // The following line is problematic when using setKeepWithNext
            toc.add(new SimpleEntry(line, pdf.getNumberOfPages()));
        } else {
            p.setFirstLineIndent(36);
            if (line.isEmpty()) {
                p.setMarginBottom(12);
                title = true;
            } else {
                p.setMarginBottom(0);
            }
            document.add(p);
        }
    }
    document.add(new AreaBreak(AreaBreakType.NEXT_PAGE));
    p = new Paragraph().setFont(bold).add("Table of Contents");
    document.add(p);
    toc.remove(0);
    List<TabStop> tabstops = new ArrayList();
    tabstops.add(new TabStop(580, TabAlignment.RIGHT, new DottedLine()));
    for (SimpleEntry<String, Integer> entry : toc) {
        p = new Paragraph().addTabStops(tabstops).add(entry.getKey()).add(new Tab()).add(String.valueOf(entry.getValue())).setAction(PdfAction.createGoTo(PdfExplicitDestination.createFit(pdf.getPage(entry.getValue()))));
        document.add(p);
    }
    // Close document
    document.close();
}
Also used : PdfWriter(com.itextpdf.kernel.pdf.PdfWriter) SimpleEntry(java.util.AbstractMap.SimpleEntry) ArrayList(java.util.ArrayList) HyphenationConfig(com.itextpdf.layout.hyphenation.HyphenationConfig) TabStop(com.itextpdf.layout.element.TabStop) AreaBreak(com.itextpdf.layout.element.AreaBreak) PdfDocument(com.itextpdf.kernel.pdf.PdfDocument) Document(com.itextpdf.layout.Document) PdfDocument(com.itextpdf.kernel.pdf.PdfDocument) Paragraph(com.itextpdf.layout.element.Paragraph) Tab(com.itextpdf.layout.element.Tab) DottedLine(com.itextpdf.kernel.pdf.canvas.draw.DottedLine) PdfFont(com.itextpdf.kernel.font.PdfFont) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader)

Example 5 with PdfFont

use of com.itextpdf.kernel.font.PdfFont in project i7js-highlevel by itext.

the class C06E11_TOC_OutlinesDestinations method createPdf.

public void createPdf(String dest) throws IOException {
    // Initialize PDF document
    PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
    pdf.getCatalog().setPageMode(PdfName.UseOutlines);
    // Initialize document
    Document document = new Document(pdf);
    PdfFont font = PdfFontFactory.createFont(StandardFonts.TIMES_ROMAN);
    PdfFont bold = PdfFontFactory.createFont(StandardFonts.HELVETICA_BOLD);
    document.setTextAlignment(TextAlignment.JUSTIFIED).setHyphenation(new HyphenationConfig("en", "uk", 3, 3)).setFont(font).setFontSize(11);
    BufferedReader br = new BufferedReader(new FileReader(SRC));
    String line;
    Paragraph p;
    boolean title = true;
    PdfOutline outline = null;
    while ((line = br.readLine()) != null) {
        p = new Paragraph(line);
        p.setKeepTogether(true);
        if (title) {
            outline = createOutline(outline, pdf, line, p);
            p.setFont(bold).setFontSize(12).setKeepWithNext(true);
            title = false;
            document.add(p);
        } else {
            p.setFirstLineIndent(36);
            if (line.isEmpty()) {
                p.setMarginBottom(12);
                title = true;
            } else {
                p.setMarginBottom(0);
            }
            document.add(p);
        }
    }
    // Close document
    document.close();
}
Also used : PdfWriter(com.itextpdf.kernel.pdf.PdfWriter) PdfFont(com.itextpdf.kernel.font.PdfFont) BufferedReader(java.io.BufferedReader) HyphenationConfig(com.itextpdf.layout.hyphenation.HyphenationConfig) FileReader(java.io.FileReader) PdfDocument(com.itextpdf.kernel.pdf.PdfDocument) Document(com.itextpdf.layout.Document) PdfDocument(com.itextpdf.kernel.pdf.PdfDocument) Paragraph(com.itextpdf.layout.element.Paragraph) PdfOutline(com.itextpdf.kernel.pdf.PdfOutline)

Aggregations

PdfFont (com.itextpdf.kernel.font.PdfFont)51 PdfDocument (com.itextpdf.kernel.pdf.PdfDocument)45 PdfWriter (com.itextpdf.kernel.pdf.PdfWriter)45 Paragraph (com.itextpdf.layout.element.Paragraph)45 Document (com.itextpdf.layout.Document)39 HyphenationConfig (com.itextpdf.layout.hyphenation.HyphenationConfig)29 BufferedReader (java.io.BufferedReader)24 FileReader (java.io.FileReader)24 AreaBreak (com.itextpdf.layout.element.AreaBreak)17 Rectangle (com.itextpdf.kernel.geom.Rectangle)12 Text (com.itextpdf.layout.element.Text)10 PdfPage (com.itextpdf.kernel.pdf.PdfPage)8 DottedLine (com.itextpdf.kernel.pdf.canvas.draw.DottedLine)8 Div (com.itextpdf.layout.element.Div)8 ArrayList (java.util.ArrayList)8 PdfCanvas (com.itextpdf.kernel.pdf.canvas.PdfCanvas)7 Tab (com.itextpdf.layout.element.Tab)7 TabStop (com.itextpdf.layout.element.TabStop)7 Canvas (com.itextpdf.layout.Canvas)6 ColumnDocumentRenderer (com.itextpdf.layout.ColumnDocumentRenderer)6