java: create pdf using itextpdf 2.1.7 or 7.1.10 Library

java: create pdf using itextpdf 2.1.7 or 7.1.10  Library

 

/**
 * 版权所有 2022 涂聚文有限公司
 * 许可信息查看:
 * 描述:
 * IDE:IntelliJ IDEA 2021.2.3
 * 数据库:MSSQL Server 2019
 * OS:windows 10 x64
 * 历史版本: JDK 14.02
 * 2022-1-12 创建者 geovindu
 * 2022-1-15 添加 Lambda
 * 2022-1-15 修改:date
 * 接口类 mssql-jdbc-9.4.1.jre16.jar.
 *
 * 2022-1-15 修改者:Geovin Du
 * 生成API帮助文档的指令:
 *javadoc - -encoding Utf-8 -d apidoc iTextHelper.java
 https://mvnrepository.com/artifact/com.itextpdf
 https://mvnrepository.com/artifact/com.lowagie/itext/2.1.7
 https://mvnrepository.com/artifact/com.lowagie/itext/4.2.1

 https://sourceforge.net/projects/itext/
 https://github.com/itext

 java write stringbuilder to file
 http://guava-libraries.googlecode.com/
 https://github.com/google/guava
 Files.write(stringBuilder, file, Charsets.UTF_8)

 http://commons.apache.org/io/
 You could use the Apache Commons IO library, which gives you FileUtils:

 FileUtils.writeStringToFile(file, stringBuilder.toString(), Charset.forName("UTF-8"))


 https://github.com/weiyeh/iText-4.2.0
 https://github.com/ymasory/iText-4.2.0
 https://mvnrepository.com/artifact/com.itextpdf/html2pdf
 http://www.java2s.com/Code/Jar/i/Downloaditextpdf541jar.htm
 http://www.java2s.com/Code/Jar/i/Downloaditextrtf215jar.htm
 https://mvnrepository.com/artifact/com.lowagie/itext-rtf/2.1.7
 http://www.java2s.com/Code/Jar/i/Downloaditextasian217jar.htm

 https://mvnrepository.com/artifact/com.itextpdf/itext-asian/5.2.0
 https://mvnrepository.com/artifact/com.itextpdf
 https://mvnrepository.com/artifact/com.itextpdf.tool
 http://www.java2s.com/Code/Jar/i/itext.htm
 
 * */


package Geovin.Common;

import java.awt.Color;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/*
5.4.1
import com.itextpdf.io.*;
import com.itextpdf.pdfa.*;
import com.itextpdf.test.*;
import com.itextpdf.commons.*;
import com.itextpdf.pdfa.PdfADocument;
import com.itextpdf.barcodes.*;
import com.itextpdf.svg.*;
import com.itextpdf.forms.*;
import com.itextpdf.kernel.*;
import com.itextpdf.layout.*;
import com.itextpdf.layout.font.*;
import com.itextpdf.styledxmlparser.*;
import com.itextpdf.signatures.*;
import com.itextpdf.text.*;

import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.Image;

import com.itextpdf.text.Phrase;
import com.itextpdf.text.Header;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.TextField;
import com.itextpdf.text.*;
*/


import Geovin.Model.Person;
import com.lowagie.text.BadElementException;
import com.lowagie.text.Cell;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.tools.*;
import com.lowagie.text.pdf.fonts.*;

/**
 *itext-rtf 2.1.7
 * itextasian 2.1.7
 *itextpdf 2.1.7
 * @author geovindu
 * @version 1.0
 * */

public class iTextHelper {



    /**
     *
     *
     *
     * */
    public  void Create() throws DocumentException, IOException
    {
        // 创建Document对象(页面的大小为A4,左、右、上、下的页边距为10)
        Document document = new Document(PageSize.A4, 10, 10, 10, 10);
        // 建立书写器
        PdfWriter.getInstance(document, new FileOutputStream("src/geovindu.PDF"));
        // 设置相关的参数
        setParameters(document, "开发者测试", "涂聚文测试", "测试 开发者 调试", "geovindu", "geovindu");
        // 打开文档
        document.open();
        // 使用iTextAsian.jar中的字体
        BaseFont baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
        Font font = new Font(baseFont);

        List<Person> personList = new ArrayList<Person>();
        // 循环添加对象
        for (int i = 0; i < 5; i++) {
            Person user = new Person();
            user.setLastName("geovindu:"+i);
            user.setFirstName("开发者测试"+i);
            user.setSex("测试"+i);
            personList.add(user);
        }

        Table table = setTable(personList);
        document.add(new Paragraph("用户信息如下:",setFont()));
        document.add(table);

        // 关闭文档
        document.close();
    }


     /**
      *
      *
      * */
     public Table setTable(List<Person> personList) throws BadElementException{
        //创建一个有3列的表格
        Table table = new Table(3);
        table.setBorderWidth(1);
        table.setBorderColor(new Color(0, 0, 255));
        table.setPadding(5);
        table.setSpacing(5);
        // 创建表头
        Cell cell1 = setTableHeader("姓");
        Cell cell2 = setTableHeader("名");
        Cell cell3 = setTableHeader("性别");
        table.addCell(cell1);
        table.addCell(cell2);
        table.addCell(cell3);
        // 添加此代码后每页都会显示表头
        table.endHeaders();


        for (int i = 0; i < personList.size(); i++) {
            Cell celli1 = setTableHeader(personList.get(i).getLastName());
            Cell celli2 = setTableHeader(personList.get(i).getFirstName());
            Cell celli3 = setTableHeader(personList.get(i).getSex());
            table.addCell(celli1);
            table.addCell(celli2);
            table.addCell(celli3);
        }

        return table;

    }

    /**
     * itextasian 2.1.7
     *
     * */
    public  Font setFont(){
        BaseFont baseFont = null;
        try {
            baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Font font = new Font(baseFont, 8, Font.NORMAL,Color.BLUE);
        return font;
    }
    /**
     * 设置cell
     * @param name
     * @return
     * @throws BadElementException
     */
    public  Cell setTableHeader(String name) throws BadElementException {

        Cell cell = new Cell(new Phrase(name, setFont()));
        //单元格水平对齐方式
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        //单元格垂直对齐方式
        cell.setVerticalAlignment(Element.ALIGN_CENTER);
       //        cell.setHeader(true);
        //cell.setBackgroundColor(Color.RED);
        return cell;

    }
    /**
     * 设置相关参数
     * @param document
     * @return
     */
    public  Document setParameters(Document document,String title,String subject,String keywords,String author,
                                         String creator){
        // 设置标题
        document.addTitle(title);
        // 设置主题
        document.addSubject(subject);
        // 设置作者
        document.addKeywords(keywords);
        // 设置作者
        document.addAuthor(author);
        // 设置创建者
        document.addCreator(creator);
        // 设置生产者
        document.addProducer();
        // 设置创建日期
        document.addCreationDate();

        return document;
    }




}

  

7.1.10

/**
 * 版权所有 2022 涂聚文有限公司
 * 许可信息查看:
 * 描述:
 * IDE:IntelliJ IDEA 2021.2.3
 * 数据库:MSSQL Server 2019
 * OS:windows 10 x64
 * 历史版本: JDK 14.02
 * 2022-1-12 创建者 geovindu
 * 2022-1-15 添加 Lambda
 * 2022-1-15 修改:date
 * 接口类 mssql-jdbc-9.4.1.jre16.jar.
 *
 * 2022-1-15 修改者:Geovin Du
 * 生成API帮助文档的指令:
 *javadoc - -encoding Utf-8 -d apidoc DuiTextPdfHelper.java
 *
 *
 https://mvnrepository.com/artifact/com.itextpdf
 https://mvnrepository.com/artifact/com.lowagie/itext/2.1.7
 https://mvnrepository.com/artifact/com.lowagie/itext/4.2.1

 https://sourceforge.net/projects/itext/
 https://github.com/itext

 java write stringbuilder to file
 http://guava-libraries.googlecode.com/
 https://github.com/google/guava
 Files.write(stringBuilder, file, Charsets.UTF_8)

 http://commons.apache.org/io/
 You could use the Apache Commons IO library, which gives you FileUtils:

 FileUtils.writeStringToFile(file, stringBuilder.toString(), Charset.forName("UTF-8"))


 https://github.com/weiyeh/iText-4.2.0
 https://github.com/ymasory/iText-4.2.0
 https://mvnrepository.com/artifact/com.itextpdf/html2pdf
 http://www.java2s.com/Code/Jar/i/Downloaditextpdf541jar.htm
 http://www.java2s.com/Code/Jar/i/Downloaditextrtf215jar.htm
 https://mvnrepository.com/artifact/com.lowagie/itext-rtf/2.1.7
 http://www.java2s.com/Code/Jar/i/Downloaditextasian217jar.htm
 https://mvnrepository.com/artifact/com.lowagie/itext
 https://mvnrepository.com/artifact/com.itextpdf/itext-asian/5.2.0
 https://mvnrepository.com/artifact/com.itextpdf
 https://mvnrepository.com/artifact/com.itextpdf.tool
 http://www.java2s.com/Code/Jar/i/itext.htm
 https://www.vogella.com/tutorials/JavaPDF/article.html
 * */


package Geovin.Common;

import java.awt.Color;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;

import com.itextpdf.io.*;
import com.itextpdf.pdfa.*;
import com.itextpdf.test.*;
import com.itextpdf.pdfa.PdfADocument;
import com.itextpdf.barcodes.*;
import com.itextpdf.svg.*;
import com.itextpdf.forms.*;
import com.itextpdf.kernel.*;
import com.itextpdf.layout.*;
import com.itextpdf.layout.font.*;
import com.itextpdf.styledxmlparser.*;
import com.itextpdf.signatures.*;
import com.itextpdf.text.*;

import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.Image;

import com.itextpdf.text.Phrase;
import com.itextpdf.text.Header;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.TextField;


import com.itextpdf.text.Anchor;
import com.itextpdf.text.BadElementException;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chapter;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.List;
import com.itextpdf.text.ListItem;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Section;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;


/**
 * iText 7.1.10
 * @author geovindu
 * @version 1.0
 *
 *
 *
 * */
public class DuiTextPdfHelper {

    private static String FILE = "src/FirstPdf.pdf";
    private static Font catFont = new Font(Font.FontFamily.TIMES_ROMAN, 18,
            Font.BOLD);
    private static Font redFont = new Font(Font.FontFamily.TIMES_ROMAN, 12,
            Font.NORMAL, BaseColor.RED);
    private static Font subFont = new Font(Font.FontFamily.TIMES_ROMAN, 16,
            Font.BOLD);
    private static Font smallBold = new Font(Font.FontFamily.TIMES_ROMAN, 12,
            Font.BOLD);

    public static void CreatePdf()
    {
        try {
            Document document = new Document();
            PdfWriter.getInstance(document, new FileOutputStream(FILE));
            document.open();
            addMetaData(document);
            addTitlePage(document);
            addContent(document);
            document.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    // iText allows to add metadata to the PDF which can be viewed in your Adobe
    // Reader
    // under File -> Properties
    private static void addMetaData(Document document) {
        document.addTitle("My first PDF");
        document.addSubject("Using iText");
        document.addKeywords("Java, PDF, iText");
        document.addAuthor("Lars Vogel");
        document.addCreator("Lars Vogel");
    }

    private static void addTitlePage(Document document)
            throws DocumentException {
        Paragraph preface = new Paragraph();
        // We add one empty line
        addEmptyLine(preface, 1);
        // Lets write a big header
        preface.add(new Paragraph("Title of the document", catFont));

        addEmptyLine(preface, 1);
        // Will create: Report generated by: _name, _date
        preface.add(new Paragraph(
                "Report generated by: " + System.getProperty("user.name") + ", " + new Date(), //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
                smallBold));
        addEmptyLine(preface, 3);
        preface.add(new Paragraph(
                "This document describes something which is very important ",
                smallBold));

        addEmptyLine(preface, 8);

        preface.add(new Paragraph(
                "This document is a preliminary version and not subject to your license agreement or any other agreement with vogella.com ;-).",
                redFont));

        document.add(preface);
        // Start a new page
        document.newPage();
    }

    private static void addContent(Document document) throws DocumentException {
        Anchor anchor = new Anchor("First Chapter", catFont);
        anchor.setName("First Chapter");

        // Second parameter is the number of the chapter
        Chapter catPart = new Chapter(new Paragraph(anchor), 1);

        Paragraph subPara = new Paragraph("Subcategory 1", subFont);
        Section subCatPart = catPart.addSection(subPara);
        subCatPart.add(new Paragraph("Hello"));

        subPara = new Paragraph("Subcategory 2", subFont);
        subCatPart = catPart.addSection(subPara);
        subCatPart.add(new Paragraph("Paragraph 1"));
        subCatPart.add(new Paragraph("Paragraph 2"));
        subCatPart.add(new Paragraph("Paragraph 3"));

        // add a list
        createList(subCatPart);
        Paragraph paragraph = new Paragraph();
        addEmptyLine(paragraph, 5);
        subCatPart.add(paragraph);

        // add a table
        createTable(subCatPart);

        // now add all this to the document
        document.add(catPart);

        // Next section
        anchor = new Anchor("Second Chapter", catFont);
        anchor.setName("Second Chapter");

        // Second parameter is the number of the chapter
        catPart = new Chapter(new Paragraph(anchor), 1);

        subPara = new Paragraph("Subcategory", subFont);
        subCatPart = catPart.addSection(subPara);
        subCatPart.add(new Paragraph("This is a very important message"));

        // now add all this to the document
        document.add(catPart);

    }

    private static void createTable(Section subCatPart)
            throws BadElementException {
        PdfPTable table = new PdfPTable(3);

        // t.setBorderColor(BaseColor.GRAY);
        // t.setPadding(4);
        // t.setSpacing(4);
        // t.setBorderWidth(1);

        PdfPCell c1 = new PdfPCell(new Phrase("Table Header 1"));
        c1.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(c1);

        c1 = new PdfPCell(new Phrase("Table Header 2"));
        c1.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(c1);

        c1 = new PdfPCell(new Phrase("Table Header 3"));
        c1.setHorizontalAlignment(Element.ALIGN_CENTER);
        table.addCell(c1);
        table.setHeaderRows(1);

        table.addCell("1.0");
        table.addCell("1.1");
        table.addCell("1.2");
        table.addCell("2.1");
        table.addCell("2.2");
        table.addCell("2.3");

        subCatPart.add(table);

    }

    private static void createList(Section subCatPart) {
        List list = new List(true, false, 10);
        list.add(new ListItem("First point"));
        list.add(new ListItem("Second point"));
        list.add(new ListItem("Third point"));
        subCatPart.add(list);
    }

    private static void addEmptyLine(Paragraph paragraph, int number) {
        for (int i = 0; i < number; i++) {
            paragraph.add(new Paragraph(" "));
        }
    }


}

  

 

哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)成功.---Geovin Du(涂聚文)