java: itext 7.2.1 using jdk 17.0.1
/**
* iText 7.2.1
* bouncycastle 1.7.0
* slf4j-api-1.6.1
* slf4j-log4j12-1.6.1
* log4j-1.2.16
*
* */
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 | /** * * * * */ public static PdfFont getDefaultFont() { try { // return PdfFontFactory.createFont("C://windows//fonts//simsun.ttc,1", PdfEncodings.IDENTITY_H, false);// 引用系统字体 return PdfFontFactory.createFont( "STSong-Light" , "UniGB-UCS2-H" ); // 使用itext字体 } catch (IOException e) { // 记录日志 e.printStackTrace(); } return null ; } /** * * * * */ public static PdfFont getImportFont(String fontName) { // 获取resource下文件夹路径 String path = "src/resource/" ; String fontPath = path + fontName; try { // 处理中文乱码(支持Linux系统) //FontProgram fontProgram = FontProgramFactory.createFont(fontPath, false); PdfFont font = PdfFontFactory.createFont(path, PdfEncodings.IDENTITY_H); return font; } catch (IOException e) { // 记录日志 e.printStackTrace(); } return null ; } PdfFont hfFont; { try { hfFont =PdfFontFactory.createFont(path); // new Font(BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED), 8, Font.NORMAL); } catch (Exception e) { e.printStackTrace(); } } /** * 图片水印 * * */ public static void addImageToPDFDU(String PDFPath){ try { // PdfWriter writer = new PdfWriter(new File(PDFPath)); //new FileOutputStream(); // writer = new PdfWriter(new File(PDFPath)); // PdfDocument pdfDoc = new PdfDocument(writer); // PdfWriter pdfwriter = PdfWriter(new File(PDFPath); PdfDocument pdf = new PdfDocument( new PdfWriter(PDFPath)); PageSize pageSize = new PageSize(PageSize.A4).rotate(); //设置文档属性 pdf.getDocumentInfo().setAuthor( "涂聚文 Geovin Du" ); pdf.getDocumentInfo().setTitle( "iText7 java测试PDF" ); pdf.getDocumentInfo().setSubject( "涂聚文公司" ); pdf.getDocumentInfo().setMoreInfo( "Geovin Du" , "1201" ); pdf.getDocumentInfo().setCreator( "geovindu 涂聚文" ); pdf.getDocumentInfo().setKeywords( "iText,涂聚文 塗聚文 Geovin Du" ); Document document = new Document(pdf,PageSize.A4, true ); TextFooterEventHandler eventHandler = new TextFooterEventHandler(document); pdf.addEventHandler(PdfDocumentEvent.END_PAGE, eventHandler); for ( int i = 0 ; i < 12 ; i++) { document.add( new Paragraph( "Test " + i + " 涂聚文 Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet." ).setFont(getDefaultFont())); } // the currently last page is also the last page of the document, so inform the event listener eventHandler.lastPage = pdf.getLastPage(); PdfPage page1 = pdf.addNewPage(); PdfCanvas canvas = new PdfCanvas(page1); // creating image data instance by passing the path to image ImageData img = ImageDataFactory.create( "src/resource/gogo.png" ); canvas.saveState(); // graphic state PdfExtGState state = new PdfExtGState(); state.setFillOpacity( 0 .2f); canvas.setExtGState(state); canvas.addImageAt(img, 20 , pageSize.getWidth()/ 2 , false ); //650, canvas.restoreState(); document.add( new Paragraph( "Adding image to PDF Example,涂聚文" ).setFont(getDefaultFont())); pdf.addNewPage(); PdfFont headerFont = PdfFontFactory.createFont(path); PdfFont cellFont = PdfFontFactory.createFont(path); Table table = new Table( new float []{ 4 , 4 , 4 }); table.setWidth(UnitValue.createPercentValue( 100 )); // adding header table.addHeaderCell( new Cell().add( new Paragraph( "姓" ).setFont(headerFont))); table.addHeaderCell( new Cell().add( new Paragraph( "名" ).setFont(headerFont))); table.addHeaderCell( new Cell().add( new Paragraph( "区域名称" ).setFont(headerFont))); table.addCell( new Paragraph( "1.0 geovindu" ).setFont(cellFont)); table.addCell( new Paragraph( "1.1 涂聚文" ).setFont(cellFont)); table.addCell( new Paragraph( "1.2 上海市" ).setFont(cellFont)); table.addCell( new Paragraph( "2.1 北京市" ).setFont(cellFont)); table.addCell( new Paragraph( "2.2 重庆市" ).setFont(cellFont)); table.addCell( new Paragraph( "2.3 宜春市" ).setFont(cellFont)); document.add(table); document.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | import java.io.IOException; import com.itextpdf.kernel.pdf.EncryptionConstants; import com.itextpdf.kernel.pdf.PdfDocument; import com.itextpdf.kernel.pdf.PdfWriter; import com.itextpdf.kernel.pdf.WriterProperties; import com.itextpdf.layout.Document; import com.itextpdf.layout.element.Paragraph; /** *geovindu *Gevoin Du **/ public class ReadPDF { public static final String CREATED_PDF = "geovindu.pdf" ; final static String USER_PASSWORD = "geovindu" ; final static String OWNER_PASSWORD = "geovindu" ; public static void main(String[] args) { try { PdfWriter writer = new PdfWriter(CREATED_PDF, new WriterProperties() .setStandardEncryption(USER_PASSWORD.getBytes(), OWNER_PASSWORD.getBytes(), EncryptionConstants.ALLOW_PRINTING, EncryptionConstants.ENCRYPTION_AES_128 | EncryptionConstants.DO_NOT_ENCRYPT_METADATA)); PdfDocument pdf = new PdfDocument(writer); Document document = new Document(pdf); document.add( new Paragraph( "Password protected PDF where only content printing is permitted content can't be copied." )); document.close(); } catch (IOException e) { System.out.println( "Exception occurred " + e.getMessage()); } } } |
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!