/** * 版权所有 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.1import 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 {/****** */publicvoid 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** */publicFont 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*/publicCell 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*/publicDocument 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;}}
https://api.itextpdf.com/iText5/java/5.5.9/
5.2.0
/** * 版权所有 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 5.2.0 * @author geovindu * @version 1.0 * * * * */public class DuiTextPdfHelper {private static String FILE = "src/geovinduPdf.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 -> Propertiesprivate 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 lineaddEmptyLine(preface, 1);// Lets write a big headerpreface.add(new Paragraph("Title of the document", catFont));addEmptyLine(preface, 1);// Will create: Report generated by: _name, _datepreface.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 pagedocument.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 chapterChapter 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 listcreateList(subCatPart);Paragraph paragraph = new Paragraph();addEmptyLine(paragraph, 5);subCatPart.add(paragraph);// add a tablecreateTable(subCatPart);// now add all this to the documentdocument.add(catPart);// Next sectionanchor = new Anchor("Second Chapter", catFont);anchor.setName("Second Chapter");// Second parameter is the number of the chaptercatPart = 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 documentdocument.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(" "));}}}
https://riptutorial.com/Download/itext.pdf
https://www.netjstech.com/2018/10/creating-pdf-in-java-using-itext.html
https://github.com/itext/i7js-examples/blob/develop/src/main/java/com/itextpdf/samples/sandbox/layout/ParagraphTextWithStyle.java
https://kb.itextsupport.com/home/it7kb/ebooks/itext-7-jump-start-tutorial-for-java/chapter-7-creating-pdf-ua-and-pdf-a-documents
https://www.tutorialspoint.com/itext/itext_adding_table.htm
https://api.itextpdf.com/iText7/java/
7.2.7
/** * 版权所有 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 DuiTextPdfHelper.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-82、设置idea file编码 。在菜单栏找到”File->settings->搜索File Encodeing , 然后在IDE Encoding , Project Encoding和Default encoding for properties files都设置为utf-83、设置idea server编码 。在菜单栏找到”run->editconfigration” 找到”server”选项卡 设置 vm option为 -Dfile.encoding=utf-84、HELP->Edit Custom VM OPtions中加 -Dfile.encoding=utf-8 重启idea * */import java.io.FileOutputStream;import java.io.IOException;import java.io.*;import java.util.ArrayList;//import java.util.List;import com.itextpdf.*;import com.itextpdf.io.font.*;import com.itextpdf.pdfa.*;import com.itextpdf.kernel.*;import com.itextpdf.kernel.pdf.*;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.element.Paragraph;import com.itextpdf.layout.element.Text;import com.itextpdf.kernel.geom.PageSize;import com.itextpdf.layout.element.Cell;import com.itextpdf.layout.element.Table;import com.itextpdf.layout.element.Image;import com.itextpdf.layout.*;import com.itextpdf.layout.element.List;import com.itextpdf.layout.element.ListItem;//import com.itextpdf.layout.property.ListNumberingType;import com.itextpdf.layout.properties.*;/** * itext 7.2.1 * @author geovindu * @version 1.0 *** */public class DuiTextPdfHelper {private static String FILE = "src/geovindu.pdf";//PdfFont catFont =PdfFontFactory.createFont(StandardFonts.HELVETICA_BOLD);// new Font(Font.FontFamily.TIMES_ROMAN, 18,Font.BOLD);//PdfFont redFont =PdfFontFactory.createFont(StandardFonts.HELVETICA_BOLD);// new Font(Font.FontFamily.TIMES_ROMAN, 12,Font.NORMAL, BaseColor.RED);//PdfFont subFont = PdfFontFactory.createFont(StandardFonts.COURIER);//new Font(Font.FontFamily.TIMES_ROMAN, 16,Font.BOLD);//PdfFont smallBold =PdfFontFactory.createFont(StandardFonts.COURIER);// new Font(Font.FontFamily.TIMES_ROMAN, 12,Font.BOLD);// PdfFont font = PdfFontFactory.createFont(FontConstants.TIMES_ROMAN);public static void CreatePdf(){try {PdfFont catFont =PdfFontFactory.createFont(StandardFonts.HELVETICA_BOLD);// new Font(Font.FontFamily.TIMES_ROMAN, 18,Font.BOLD);PdfFont redFont =PdfFontFactory.createFont(StandardFonts.HELVETICA_BOLD);// new Font(Font.FontFamily.TIMES_ROMAN, 12,Font.NORMAL, BaseColor.RED);PdfFont subFont = PdfFontFactory.createFont(StandardFonts.COURIER);//new Font(Font.FontFamily.TIMES_ROMAN, 16,Font.BOLD);PdfFont smallBold =PdfFontFactory.createFont(StandardFonts.COURIER);// new Font(Font.FontFamily.TIMES_ROMAN, 12,Font.BOLD);//PdfDocument pdf = new PdfDocument(new PdfWriter(dest),new WriterProperties().addXmpMetadata()));PdfWriter writer = newPdfWriter(FILE);PdfDocument pdf = newPdfDocument(writer);Document document = new Document(pdf);// PdfWriter.getInstance(document, new FileOutputStream(FILE));// document.open();PdfFont russian = PdfFontFactory.createFont("src/geovindu/resources/fonts/FreeSans.ttf", "CP1251", pdf);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 -> Propertiesprivate static void addMetaData(Document document) {addCustomMetadadata(document,"Title","My first PDF");addCustomMetadadata(document,"Subject","My first PDF");addCustomMetadadata(document,"Keywords","My first PDF");addCustomMetadadata(document,"Author","geovindu");addCustomMetadadata(document,"Creator","geovindu");// 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 Exception {Paragraph preface = new Paragraph();PdfFont catFont =PdfFontFactory.createFont(StandardFonts.HELVETICA_BOLD);PdfFont redFont =PdfFontFactory.createFont(StandardFonts.HELVETICA_BOLD);// new Font(Font.FontFamily.TIMES_ROMAN, 12,Font.NORMAL, BaseColor.RED);PdfFont subFont = PdfFontFactory.createFont(StandardFonts.COURIER);//new Font(Font.FontFamily.TIMES_ROMAN, 16,Font.BOLD);PdfFont smallBold =PdfFontFactory.createFont(StandardFonts.COURIER);// new Font(Font.FontFamily.TIMES_ROMAN, 12,Font.BOLD);// We add one empty lineaddEmptyLine(preface, 1);// Lets write a big headerpreface.add(new Paragraph("Title of the document").setFont(catFont));addEmptyLine(preface, 1);// Will create: Report generated by: _name, _datepreface.add(new Paragraph("Report generated by: " + System.getProperty("user.name") + ", " + new Date() //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$).setFont(smallBold));addEmptyLine(preface, 3);preface.add(new Paragraph("This document describes something which is very important ").setFont(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 ;-).").setFont(redFont));document.add(preface);// Start a new page//document.();}private static void addContent(Document document) throws Exception {PdfFont subFont = PdfFontFactory.createFont(StandardFonts.COURIER);//new Font(Font.FontFamily.TIMES_ROMAN, 16,Font.BOLD);PdfFont catFont =PdfFontFactory.createFont(StandardFonts.HELVETICA_BOLD);// new Font(Font.FontFamily.TIMES_ROMAN, 18,Font.BOLD);Anchor anchor = new Anchor("First Chapter", catFont);anchor.setName("First Chapter");// Second parameter is the number of the chapterChapter catPart = new Chapter(new Paragraph(anchor), 1);Paragraph subPara = new Paragraph("Subcategory 1").setFont(subFont);Section subCatPart = catPart.addSection(subPara);subCatPart.add(new Paragraph("Hello"));subPara = new Paragraph("Subcategory 2").setFont(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 listcreateList(subCatPart);Paragraph paragraph = new Paragraph();addEmptyLine(paragraph, 5);subCatPart.add(paragraph);// add a tablecreateTable(subCatPart);// now add all this to the documentdocument.add(catPart);// Next sectionanchor = new Anchor("Second Chapter", subFont);anchor.setName("Second Chapter");// Second parameter is the number of the chaptercatPart = new Chapter(new Paragraph(anchor), 1);subPara = new Paragraph("Subcategory").setFont(catFont);subCatPart = catPart.addSection(subPara);subCatPart.add(new Paragraph("This is a very important message"));// now add all this to the documentdocument.add(catPart);}public static void addMetadata(Document document,String title, String subject, String author, String creator) {PdfDocumentInfo documentInfo = document.getPdfDocument().getDocumentInfo();if (title!="") {documentInfo.setTitle(title);}if (subject!="") {documentInfo.setSubject(subject);}if (author!="") {documentInfo.setAuthor(author);}if (creator!="") {documentInfo.setCreator(creator);}}public static void addCustomMetadadata(Document document, String key,String value) {PdfDocumentInfo documentInfo = document.getPdfDocument().getDocumentInfo();documentInfo.setMoreInfo(key, value);}private static void createTable(Document subCatPart)throws Exception {PdfFont headerFont = PdfFontFactory.createFont(StandardFonts.HELVETICA_BOLD);PdfFont cellFont = PdfFontFactory.createFont(StandardFonts.COURIER);//Table table = new Table(3);// t.setBorderColor(BaseColor.GRAY);// t.setPadding(4);// t.setSpacing(4);// t.setBorderWidth(1);Table table = new Table(new float[]{4, 4, 4});table.setWidth(UnitValue.createPercentValue(100));// adding headertable.addHeaderCell(new Cell().add(new Paragraph("First Name").setFont(headerFont)));table.addHeaderCell(new Cell().add(new Paragraph("Last Name").setFont(headerFont)));table.addHeaderCell(new Cell().add(new Paragraph("Email").setFont(headerFont)));/* Cell c1 = new Cell(new Phrase("Table Header 1"));c1.setHorizontalAlignment(Element.ALIGN_CENTER);table.addCell(c1);c1 = new Cell(new Phrase("Table Header 2"));c1.setHorizontalAlignment(Element.ALIGN_CENTER);table.addCell(c1);c1 = new Cell(new Phrase("Table Header 3"));c1.setHorizontalAlignment(Element.ALIGN_CENTER);table.addCell(c1);table.setHeaderRows(1);*/table.addCell("1.0 geovindu");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(Document subCatPart) {com.itextpdf.layout.element.List list = new com.itextpdf.layout.element.List().setSymbolIndent(14);//(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 void createTablePDF(String PDFPath){PdfWriter writer;try {writer = new PdfWriter(new FileOutputStream(PDFPath));PdfDocument pdf = new PdfDocument(writer);Document document = new Document(pdf, new PageSize(PageSize.A4));PdfFont headerFont = PdfFontFactory.createFont(StandardFonts.HELVETICA_BOLD);PdfFont cellFont = PdfFontFactory.createFont(StandardFonts.COURIER);// Create table with 3 columns of similar lengthTable table = new Table(new float[]{4, 4, 4});table.setWidth(UnitValue.createPercentValue(100));// adding headertable.addHeaderCell(new Cell().add(new Paragraph("First Name").setFont(headerFont)));table.addHeaderCell(new Cell().add(new Paragraph("Last Name").setFont(headerFont)));table.addHeaderCell(new Cell().add(new Paragraph("Email").setFont(headerFont)));java.util.List<User> users = getListOfUsers();// adding rowsfor(User user : users) {table.addCell(new Cell().add(new Paragraph(user.getFirstName()).setFont(cellFont)));table.addCell(new Cell().add(new Paragraph(user.getLastName()).setFont(cellFont)));table.addCell(new Cell().add(new Paragraph(user.getEmail()).setFont(cellFont)));}document.add(table);document.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}// Dummy method for adding List of Usersprivate java.util.List<User> getListOfUsers() {java.util.List<User> users = new ArrayList<User>();users.add(new User("Jack", "Reacher", "abc@xyz.com"));users.add(new User("Remington", "Steele", "rs@cbd.com"));users.add(new User("Jonathan", "Raven", "jr@sn.com"));return users;}protected void manipulatePdf(String dest) throws Exception {PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));Document doc = new Document(pdfDoc);Table table = new Table(3);float tableWidth = doc.getPdfDocument().getDefaultPageSize().getWidth()- (doc.getLeftMargin() + doc.getRightMargin());table.setWidth(tableWidth);Cell cell1 = new Cell();Paragraph p = new Paragraph("1");p.setProperty(Property.SPLIT_CHARACTERS, new DefaultSplitCharacters());cell1.add(p);table.addCell(cell1);Cell cell2 = new Cell();Paragraph p2 = new Paragraph("CamLane_Disp_Warn_Rq_Pr2_e0h2tjvjx5d9y5cbvxqsnhwa7");p2.setProperty(Property.SPLIT_CHARACTERS, new DefaultSplitCharacters());cell2.add(p2);table.addCell(cell2);Cell cell3 = new Cell();Paragraph p3 = new Paragraph("CamLane_Disp_Warn_Rq_AR2");p3.setProperty(Property.SPLIT_CHARACTERS, new DefaultSplitCharacters());cell3.add(p3);table.addCell(cell3);Cell cell4 = new Cell();Paragraph p4 = new Paragraph("SQC/CRC");p4.setProperty(Property.SPLIT_CHARACTERS, new DefaultSplitCharacters());cell4.add(p4);table.addCell(cell4);Cell cell5 = new Cell();Paragraph p5 = new Paragraph("SPV_EngRq1_VAN_Pr2_vx0c4n6d46wgrav5gmco6bvc");p5.setProperty(Property.SPLIT_CHARACTERS, new DefaultSplitCharacters());cell5.add(p5);table.addCell(cell5);Cell cell6 = new Cell();Paragraph p6 = new Paragraph("Bckl_Sw_Ft_Stat_Pr2_b14xqvpzjykdbhltdyma53upe");p6.setProperty(Property.SPLIT_CHARACTERS, new DefaultSplitCharacters());cell6.add(p6);table.addCell(cell6);doc.add(table);doc.close();}private static void addEmptyLine(Paragraph paragraph, int number) {for (int i = 0; i < number; i++) {paragraph.add(new Paragraph(" "));}}private void addImageToPDF(String PDFPath){PdfWriter writer;try {writer = new PdfWriter(new FileOutputStream(PDFPath));PdfDocument pdfDoc = new PdfDocument(writer);Document document = new Document(pdfDoc);PageSize pageSize = new PageSize(PageSize.A4).rotate();PdfCanvas canvas = new PdfCanvas(pdfDoc.addNewPage());// creating image data instance by passing the path to imageImageData img = ImageDataFactory.create("resources//netjs.png");canvas.saveState();// graphic statePdfExtGState state = new PdfExtGState();state.setFillOpacity(0.2f);canvas.setExtGState(state);canvas.addImage(img, 20, 650, pageSize.getWidth()/2, false);canvas.restoreState();document.add(new Paragraph("Adding image to PDF Example"));document.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}private void addImageToPDF(String PDFPath){PdfWriter writer;try {// creating image data instance by passing the path to imageString imFile="resources//netjs.png";ImageData data = https://tazarkount.com/read/ImageDataFactory.create(imFile);Image image = new image(data);writer = new PdfWriter(new FileOutputStream(PDFPath));PdfDocument pdfDoc = new PdfDocument(writer);Document document = new Document(pdfDoc);document.add(new Paragraph("Adding image to PDF Example"));document.add(image);document.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}private void addImageToPDF2(String PDFPath){PdfWriter writer;try {writer = new PdfWriter(new FileOutputStream(PDFPath));PdfDocument pdfDoc = new PdfDocument(writer);Document document = new Document(pdfDoc);document.add(new Paragraph("Choices Are (Using English Letters)"));// for offset (space from the left)com.itextpdf.layout.element.List list = new com.itextpdf.layout.element.List().setSymbolIndent(14).setListSymbol(ListNumberingType.ENGLISH_LOWER);// Add ListItem objectslist.add(new ListItem("Aerobic")).add(new ListItem("Anaerobic")).add(new ListItem("Flexibility Training"));// Add the listdocument.add(list);document.add(new Paragraph("Choices Are (Using Roman upper)"));list = new com.itextpdf.layout.element.List().setSymbolIndent(14).setListSymbol(ListNumberingType.ROMAN_UPPER);// Add ListItem objectslist.add(new ListItem("Aerobic")).add(new ListItem("Anaerobic")).add(new ListItem("Flexibility Training"));// Add the listdocument.add(list);document.add(new Paragraph("Choices Are (Using bullet symbol)"));list = new com.itextpdf.layout.element.List().setSymbolIndent(14).setListSymbol("\u2022"); // Passing unicode for bullet// Add ListItem objectslist.add(new ListItem("Aerobic")).add(new ListItem("Anaerobic")).add(new ListItem("Flexibility Training"));// Add the listdocument.add(list);document.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}} }

文章插图
https://github.com/arnosthavelka/itext-poc/blob/develop/src/main/java/com/github/aha/poc/itext/DocumentBuilder.java
https://api.itextpdf.com/iText5/java/
5.2.1
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.ttfprivate 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 -> Propertiesprivate 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 lineaddEmptyLine(preface, 1);// Lets write a big headerpreface.add(new Paragraph("Title of the document", catFont));addEmptyLine(preface, 1);// Will create: Report generated by: _name, _datepreface.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 pagedocument.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 chapterChapter 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 listcreateList(subCatPart);Paragraph paragraph = new Paragraph();addEmptyLine(paragraph, 5);subCatPart.add(paragraph);// add a tablecreateTable(subCatPart);// now add all this to the documentdocument.add(catPart);// Next sectionanchor = new Anchor("Second Chapter", catFont);anchor.setName("Second Chapter");// Second parameter is the number of the chaptercatPart = 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 documentdocument.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(" "));}}/*** 生成pdf文件*/public void createPdf(Font font) throws FileNotFoundException, DocumentException {String path = "src/"+System.currentTimeMillis()+".pdf";File file = new File(path);file.getParentFile().mkdirs();Document doc = new Document(PageSize.A4);PdfWriter.getInstance(doc, new FileOutputStream(file));doc.open();doc.add(new Paragraph("字体测试",font));doc.close();}/*** 使用windows系统下的字体,new Font方式*/public void DusetFont() throws DocumentException, IOException {String path = "C:/WINDOWS/Fonts/simhei.ttf";//windows里的字体资源路径BaseFont bf = BaseFont.createFont(path, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);Font font = new Font(bf, 10f, Font.NORMAL, BaseColor.BLACK);createPdf(font);}/*** 使用windows系统下的字体,FontFactory方式*/public void DusetFont2() throws DocumentException, IOException {String path = "C:/WINDOWS/Fonts/simhei.ttf";//windows里的字体资源路径Font font = FontFactory.getFont(path, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED,10f, Font.NORMAL, BaseColor.BLACK);createPdf(font);}/*** 使用自己查找的字体,FontFactory方式*/public void DusetFont3() throws DocumentException, IOException {String path = "src/main/resources/file/pdf/font/SIMYOU.TTF";//自己的字体资源路径Font font = FontFactory.getFont(path, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED,10f, Font.NORMAL, BaseColor.BLACK);createPdf(font);}/*** 使用iTextAsian.jar中的字体 , FontFactory方式*/public void DusetFont4() throws DocumentException, IOException {Font font = FontFactory.getFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED,10f, Font.NORMAL, BaseColor.BLACK);createPdf(font);}}
https://api.itextpdf.com/iText5/csharp/
https://api.itextpdf.com/DITO/java/2.2.5/
https://api.itextpdf.com/iText5/java/5.5.9/
https://api.itextpdf.com/iText7/java/7.2.1/
https://api.itextpdf.com/pdf2Data/java/2.1.2/
https://api.itextpdf.com/pdfCalligraph/java/2.0.5/
https://api.itextpdf.com/pdfHTML/java/4.0.1/
https://api.itextpdf.com/pdfSweep/java/3.0.0/
https://api.itextpdf.com/pdfXFA/java/3.0.0/
https://api.itextpdf.com/pdfRender/java/2.0.1/
https://api.itextpdf.com/pdfOCR/java/2.0.1/
https://api.itextpdf.com/licensekey/java/4.0.1/
https://api.itextpdf.com/pdfOptimizer/java/2.0.1/
https://api.itextpdf.com/iText7/dotnet/7.1.8/
https://api.itextpdf.com/pdfCalligraph/dotnet/2.0.5/
https://api.itextpdf.com/pdfHTML/dotnet/4.0.1/
https://api.itextpdf.com/pdfSweep/dotnet/3.0.0/
https://api.itextpdf.com/pdfXFA/dotnet/3.0.0/
https://api.itextpdf.com/pdfOCR/dotnet/2.0.1/
https://api.itextpdf.com/licensekey/dotnet/4.0.1/
https://api.itextpdf.com/pdfOptimizer/dotnet/2.0.1/
https://api.itextpdf.com/pdfOffice/java/2.0.1/

文章插图
java sdk 17.0.1
【java: create pdf using itextpdf 2.1.7 or 5.2.1 or 7.2.1Library】

文章插图
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)成功.---Geovin Du(涂聚文)
- 春季老年人吃什么养肝?土豆、米饭换着吃
- 三八妇女节节日祝福分享 三八妇女节节日语录
- 老人谨慎!选好你的“第三只脚”
- 校方进行了深刻的反思 青岛一大学生坠亡校方整改校规
- 脸皮厚的人长寿!有这特征的老人最长寿
- 长寿秘诀:记住这10大妙招 100%增寿
- 春季老年人心血管病高发 3条保命要诀
- 眼睛花不花要看四十八 老年人怎样延缓老花眼
- 香槟然能防治老年痴呆症? 一天三杯它人到90不痴呆
- 老人手抖的原因 为什么老人手会抖
