java: itext 7.2.1 using jdk 14.0.2
//import com.itextpdf.layout.properties.*; //7.2.0
//import com.itextpdf.layout.properties.VerticalAlignment;//7.2.0
import com.itextpdf.layout.property.*; //7.1.10
import com.itextpdf.layout.property.VerticalAlignment;//7.1.10
/** * 获取统一字体 * * **/ public static PdfFont getPdfFont(){ PdfFont pdfFont = null; try { pdfFont = PdfFontFactory.createFont(path, PdfEncodings.IDENTITY_H); } catch (IOException e) { e.printStackTrace(); } return pdfFont; } /** * 生成简单PDF * 涂聚文 Geovin Du * * */ public static void duPdf(){ //语法糖 try (PdfWriter writer = new PdfWriter(new File("src/geoivndu4.pdf")); PdfDocument pdf = new PdfDocument(writer); Document document = new Document(pdf, PageSize.A4.rotate()); ){ //设置文档属性 pdf.getDocumentInfo().setAuthor("涂聚文"); pdf.getDocumentInfo().setTitle("IText测试PDF"); pdf.getDocumentInfo().setSubject("涂聚文公司"); pdf.getDocumentInfo().setMoreInfo("涂聚文","111"); pdf.getDocumentInfo().setCreator("geovindu"); pdf.getDocumentInfo().setKeywords("IText涂聚文"); //注册事件监听 pdf.addEventHandler(PdfDocumentEvent.END_PAGE, new DuEventHandler()); //设置字体 document.setFont(getPdfFont()); //页边距 document.setMargins(20, 20, 20, 20); //简单文字 document.add(new Paragraph("简单文字")); document.add(new Paragraph("Hello Word!").add(new Tab()).add(new Text("你好!").addStyle(new Style().setFontSize(24)))); //简单图片 document.add(new Paragraph("简单图片")); document.add(new Image(ImageDataFactory.create("src/resource/geovindulogo.jpg"))); //简单表格 document.add(new Paragraph("简单表格")); Table table = new Table(new float[]{3, 3, 4}); PdfFont font = getPdfFont(); //标题、内容 process(table, "姓名;年龄;电话号码", font, true); for (int i = 0; i < 5; i++) { process(table, "涂聚文"+i+";"+(18+i)+";1500000000"+i, font, false); } document.add(table); //超链接 document.add(new Paragraph("超链接")); PdfLinkAnnotation annotation = new PdfLinkAnnotation(new Rectangle(0, 0)); annotation.setAction(PdfAction.createURI("https://itextpdf.com/")); Paragraph p = new Paragraph("更多精彩内容,猛戳:").add(new Link("这里", annotation)); document.add(p); pdf.addNewPage(); //换一页 document.add(new AreaBreak(AreaBreakType.NEXT_PAGE)); pdf.addNewPage(); document.close(); System.out.println("操作完成!"); } catch (IOException e) { e.printStackTrace(); System.err.println("操作异常..."); } }
//import com.itextpdf.html2pdf.ConverterProperties; //import com.itextpdf.html2pdf.HtmlConverter; /** * 版权所有 2022 涂聚文有限公司 * 许可信息查看: * 描述: * * 数据库: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 * 2022-1-12 创建者 geovindu * 2022-1-15 添加 Lambda * 2022-1-15 修改:date * 接口类 mssql-jdbc-9.4.1.jre16.jar. * 数据库:MSSQL Server 2019 * 2022-1-25 修改者: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 * */ import com.itextpdf.io.font.PdfEncodings; import com.itextpdf.io.image.ImageDataFactory; import com.itextpdf.kernel.colors.Color; import com.itextpdf.kernel.colors.DeviceRgb; import com.itextpdf.kernel.events.Event; import com.itextpdf.kernel.events.IEventHandler; import com.itextpdf.kernel.events.PdfDocumentEvent; import com.itextpdf.kernel.font.PdfFont; import com.itextpdf.kernel.font.PdfFontFactory; import com.itextpdf.kernel.geom.PageSize; import com.itextpdf.kernel.geom.Rectangle; import com.itextpdf.kernel.pdf.PdfDocument; import com.itextpdf.kernel.pdf.PdfPage; import com.itextpdf.kernel.pdf.PdfWriter; import com.itextpdf.kernel.pdf.action.PdfAction; import com.itextpdf.kernel.pdf.annot.PdfLinkAnnotation; import com.itextpdf.kernel.pdf.canvas.PdfCanvas; import com.itextpdf.layout.Canvas; import com.itextpdf.layout.Document; import com.itextpdf.layout.Style; import com.itextpdf.layout.element.*; import com.itextpdf.layout.font.FontProvider; import com.itextpdf.layout.properties.*; import com.itextpdf.layout.properties.VerticalAlignment; import com.itextpdf.kernel.pdf.canvas.*; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; /** * itext 7.2.1 * log4j 1.2.16 * slf4j api 1.7.33 * slf4j-log4j 12-1.7.33 * * */ public class DuEventHandler implements IEventHandler { private static final String FONT = "C:/Windows/Fonts/simhei.ttf"; private String markString="涂聚文 Geovin Du"; private String headString="涂聚文人事系统分析论文"; public DuEventHandler() {} /** * * * **/ public DuEventHandler(String markString,String headstring) { this.headString=headstring; this.markString=markString; } @Override public void handleEvent(Event event) { try { PdfDocumentEvent docEvent = (PdfDocumentEvent) event; PdfDocument pdfDoc = docEvent.getDocument(); PdfPage page = docEvent.getPage(); int pageNumber = pdfDoc.getPageNumber(page); int pageTotal=pdfDoc.getNumberOfPages(); Rectangle pageSize = page.getPageSize(); PdfCanvas pdfCanvas = new PdfCanvas(page.newContentStreamBefore(), page.getResources(), pdfDoc); //背景颜色 Color backgroundColor = new DeviceRgb(245, 245, 245);; pdfCanvas.saveState() .setFillColor(backgroundColor) .rectangle(pageSize.getLeft(), pageSize.getBottom(),pageSize.getWidth(), pageSize.getHeight()) .fill().restoreState(); //页脚页眉 PdfFont pdfFont = getPdfFont(); String header = headString; String footer = "第 "+pageNumber+" 页 共 "+pageTotal +"页"; pdfCanvas.beginText() .setFontAndSize(pdfFont, 9) .moveText((pageSize.getWidth() / 2) - (pdfFont.getWidth(header) / 200), pageSize.getTop() - 20) .showText(header) .moveText((pdfFont.getWidth(header) / 200) - (pdfFont.getWidth(footer) / 200), -pageSize.getTop() + 30) .showText(footer) .endText(); Rectangle rectangle = new Rectangle(36, 650, 100, 100); //文字水印 Canvas canvas = new Canvas(pdfCanvas,rectangle); canvas.setFontColor(new DeviceRgb(200, 200, 200)); canvas.setProperty(20, 20); canvas.setFont(pdfFont); for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { canvas.showTextAligned(new Paragraph(markString).setOpacity(0.8f),(150 + i * 300), (160 + j * 150), pdfDoc.getPageNumber(page), TextAlignment.CENTER, VerticalAlignment.MIDDLE, 45); } } pdfCanvas.release(); } catch(Exception ex) { ex.printStackTrace(); } } //获取统一字体 public static PdfFont getPdfFont(){ PdfFont pdfFont = null; try { pdfFont = PdfFontFactory.createFont(FONT, PdfEncodings.IDENTITY_H); } catch (IOException e) { e.printStackTrace(); } return pdfFont; } }
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)