/**
* 版权所有 2021 涂聚文有限公司
* 许可信息查看:
* 描述:
*
* 数据库:Ms SQL server 2019
* IDE: Eclipse IDE for Enterprise Java and Web Developers - 2021-09
* OS: Windows 10 x64
* IDE: Eclipse IDE for Enterprise Java and Web Developers - 2021-09
* 历史版本: JDK 14.0.2
* 2021-12-12 创建者 geovindu
* 2021-12-15 添加 Lambda
* 2021-12-15 修改:date
* 接口类 mssql-jdbc-9.4.1.jre16.jar.
* 数据库:MSSQL Server 2019
* 2021-12-15 修改者:Geovin Du
* 生成API帮助文档的指令:
*javadoc - -encoding Utf-8 -d apidoc iTextPdfHelper.java
*https://www.microsoft.com/en-us/software-download/windows10
*https://github.com/PaddlePaddle/PaddleOCR
*https://docs.microsoft.com/es-es/sql/connect/jdbc/download-microsoft-jdbc-driver-for-sql-server?view=sql-server-ver15
*https://github.com/microsoft/mssql-jdbc/blob/main/README.md
*oracle.jdbc.driver.OracleDriver
*
*1、打开idea安装目录的bin目录下的idea.exe.vmoption和idea64.exe.vmoption,在最后加上 -Dfile.encoding=utf-8
2、设置idea file编码。在菜单栏找到”File->settings->搜索File Encodeing,然后在IDE Encoding ,Project Encoding和Default encoding for properties files都设置为utf-8
3、设置idea server编码。在菜单栏找到”run->editconfigration” 找到”server”选项卡 设置 vm option为 -Dfile.encoding=utf-8
4、HELP->Edit Custom VM OPtions中加 -Dfile.encoding=utf-8 重启idea
* */
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 java.io.*;
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.Section;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.FontFactory;
/**
* itextpdf 5.2.1
* itext-asian-5.2.0
* itext-rtf-2.1.7
* @author geovindu
* @version 1.0
*
*
*
* */
public class iTextPdfHelper {
private static String FILE = "src/geovindu.pdf";
//中文字体
private static String path = "C:/WINDOWS/Fonts/STFANGSO.TTF";//windows里的字体资源路径simhei.ttf
private static Font dufont = FontFactory.getFont(path, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED,10f, Font.NORMAL, BaseColor.BLACK);
private static Font catFont =FontFactory.getFont(path, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED,18f, Font.NORMAL, BaseColor.BLACK);// new Font(Font.FontFamily.TIMES_ROMAN, 18,Font.BOLD);
private static Font redFont =FontFactory.getFont(path, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED,12f, Font.NORMAL, BaseColor.BLACK);// new Font(Font.FontFamily.TIMES_ROMAN, 12,Font.NORMAL, BaseColor.RED);
private static Font subFont =FontFactory.getFont(path, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED,16f, Font.NORMAL, BaseColor.BLACK);// new Font(Font.FontFamily.TIMES_ROMAN, 16,Font.BOLD);
private static Font smallBold =FontFactory.getFont(path, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED,12f, Font.NORMAL, BaseColor.BLACK);// 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("geovindu");
document.addCreator("geovindu");
}
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 涂聚文",subFont));
subCatPart.add(new Paragraph("Paragraph 2涂聚文涂聚文",subFont));
subCatPart.add(new Paragraph("Paragraph 3涂聚文",subFont));
// 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",subFont));
// 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);
Phrase pp=new Phrase("Table Header油料作物 1",subFont);
PdfPCell c1 = new PdfPCell(pp);
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("Table Header涂 2",subFont));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("Table Header 聚文3",subFont));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
table.setHeaderRows(1);
table.addCell(new Phrase("1.0涂聚文",subFont));
table.addCell(new Phrase("1.1涂聚文",subFont));
table.addCell(new Phrase("1.2涂聚文",subFont));
table.addCell(new Phrase("2.1塗聚文工團",subFont));
table.addCell(new Phrase("2.2涂聚文",subFont));
table.addCell(new Phrase("2.3",subFont));
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(" "));
}
}
}