java: itext8.05 create pdf
只能调用windows 已安装的字体,这样可以在系统中先预装中文字体,5.0 可以调用自配文件夹的字体文件。CSharp itext8.0 可以调用。
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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 | /** * encoding: utf-8 * 版权所有 2024 ©涂聚文有限公司 言語成了邀功盡責的功臣,還需要行爲每日來值班嗎 * 许可信息查看: https://github.com/itext/itext-publications-examples-java/blob/develop/src/main/java/com/itextpdf/samples/sandbox/fonts/FreeSansBold.java * 描述:https://mvnrepository.com/artifact/com.itextpdf/kernel * # Author : geovindu,Geovin Du 涂聚文. * # IDE : IntelliJ IDEA 2023.1 Java 17 * # Datetime : 2024 - 2024/11/21 - 20:12 * # User : geovindu * # Product : IntelliJ IDEA * # Project : EssentialAlgorithms itext8.05 * # File : SimpleItext.java * # explain : 学习 类 **/ package BLL; //import java.awt.*; import java.io.FileNotFoundException; import java.io.FileOutputStream; import com.itextpdf.io.font.PdfEncodings; import com.itextpdf.io.font.TrueTypeFont; import com.itextpdf.kernel.geom.PageSize; import com.itextpdf.kernel.colors.Color; import com.itextpdf.kernel.colors.*; import com.itextpdf.kernel.colors.gradients.*; import com.itextpdf.kernel.font.PdfFont; import com.itextpdf.kernel.font.PdfFontFactory; import com.itextpdf.kernel.font.*; import com.itextpdf.kernel.pdf.*; import com.itextpdf.kernel.pdf.canvas.draw.*; import com.itextpdf.io.image.ImageData; import com.itextpdf.io.image.ImageDataFactory; import com.itextpdf.io.font.FontProgramFactory; import com.itextpdf.layout.element.*; import com.itextpdf.layout.Document; import com.itextpdf.layout.element.Paragraph; import com.itextpdf.layout.borders.Border; import com.itextpdf.layout.element.Cell; import com.itextpdf.layout.element.Image; import com.itextpdf.layout.element.Paragraph; import com.itextpdf.layout.element.Table; import com.itextpdf.layout.properties.TextAlignment; import com.itextpdf.layout.properties.UnitValue; import com.itextpdf.layout.properties.VerticalAlignment; import com.itextpdf.layout.properties.HorizontalAlignment; import com.itextpdf.layout.element.Text; import com.itextpdf.layout.borders.*; import com.itextpdf.layout.borders.SolidBorder; import com.itextpdf.io.util.StreamUtil; import java.io.IOException; import java.util.List; import java.util.ArrayList; import java.io.File; import java.util.HashSet; import java.util.Set; import java.io.FileInputStream; import java.nio.file.Files; import java.nio.file.Paths; import Model.Orders; public class SimpleItext { private static String USER_PASSWORD = "geovindu" ; private static String OWNER_PASSWORD = "geovindu" ; public static final String FONTDIR = "font/" ; public void test() { try { String fontFile2 = "font/MHeiHK-Light.TTF" ; String path = "C:/WINDOWS/Fonts/simhei.ttf" ; //windows里的字体资源路径 String getfont= "" ; // Creating a PdfWriter object String dest = "coloredBorders.pdf" ; //PdfFont pfont=null; PdfWriter writer = new PdfWriter(dest); // Creating a PdfDocument object PdfDocument pdfDoc = new PdfDocument(writer); // Creating a Document object Document doc = new Document(pdfDoc); PdfFontFactory.registerDirectory(FONTDIR); byte [] bytes; FileInputStream myflie = new FileInputStream(fontFile2); //var is = getClass().getResourceAsStream(fontFile2); bytes = StreamUtil.inputStreamToArray(myflie); //byte[] tem = new byte[myflie.available()];//创建一个长度为myflie长的竹筒 //myflie.read(tem); //var pfont = FontProgramFactory.createFont(bytes); //PdfFont pfont = PdfFontFactory.createFont(bytes, PdfEncodings.UTF8); /* Set<String> fonts = new HashSet<String>(FontProgramFactory.getRegisteredFonts()); for (String fontname : fonts) { System.out.print(fontname); getfont=fontname; pfont = PdfFontFactory.createRegisteredFont(getfont, PdfEncodings.IDENTITY_H); doc.add(new Paragraph(String.format("Postscript name for %s: %s", getfont, pfont.getFontProgram().getFontNames().getFontName()))); } */ //PdfFont f2 = PdfFontFactory.createFont("STSong-Light", "UniGB-UCS2-H"); PdfFont pfont = PdfFontFactory.createFont(path, PdfEncodings.IDENTITY_H); //PdfFontFactory.EmbeddingStrategy.FORCE_EMBEDDED // Creating a table float [] pointColumnWidths = {200F, 200F}; Table table = new Table(pointColumnWidths); // Adding row 1 to the table Cell c1 = new Cell(); // Adding the contents of the cell c1.add( new Paragraph( "Name" ).setFont(pfont)); // Setting the back ground color of the cell c1.setBackgroundColor(ColorConstants.DARK_GRAY); // Instantiating the Border class Border b1 = new DashedBorder(ColorConstants.RED, 3 ); // Setting the border of the cell c1.setBorder(b1); // Setting the text alignment c1.setTextAlignment(TextAlignment.CENTER); // Adding the cell to the table table.addCell(c1); Cell c2 = new Cell(); c2.add( new Paragraph( "涂聚文" ).setFont(pfont)); c1.setBorder( new SolidBorder(ColorConstants.RED, 3 )); c2.setTextAlignment(TextAlignment.CENTER); table.addCell(c2); // Adding row 2 to the table Cell c3 = new Cell(); c3.add( new Paragraph( "Id" ).setFont(pfont)); c3.setBorder( new DottedBorder(ColorConstants.DARK_GRAY, 3 )); c3.setTextAlignment(TextAlignment.CENTER); table.addCell(c3); Cell c4 = new Cell(); c4.add( new Paragraph( "001" ).setFont(pfont)); c4.setBorder( new DoubleBorder(ColorConstants.DARK_GRAY, 3 )); c4.setTextAlignment(TextAlignment.CENTER); table.addCell(c4); // Adding row 3 to the table Cell c5 = new Cell(); c5.add( new Paragraph( "Designation" ).setFont(pfont)); c5.setBorder( new RoundDotsBorder(ColorConstants.RED, 3 )); c5.setTextAlignment(TextAlignment.CENTER); table.addCell(c5); Cell c6 = new Cell(); c6.add( new Paragraph( "Programmer" ).setFont(pfont)); c6.setBorder( new RoundDotsBorder(ColorConstants.RED, 3 )); c6.setTextAlignment(TextAlignment.CENTER); table.addCell(c6); // Adding Table to document doc.add(table); // Closing the document doc.close(); System.out.println( "Borders added successfully.." ); } catch (IOException oex) { oex.printStackTrace(); } catch (Exception ex) { ex.printStackTrace(); } } public void Createpdf() { try { String dem= "itextExample.pdf" ; PdfWriter writer = new PdfWriter(dem); //设置打开文档必码 // writer.setEncryption(USER_PASSWORD.getBytes(), //OWNER_PASSWORD.getBytes(), PdfWriter.ALLOW_PRINTING, //PdfWriter.ENCRYPTION_AES_128); String fontFile2 = "font\\MHeiHK-Light.TTF" ; // 这个报错: Type of font is not recognized String path = "C:/WINDOWS/Fonts/simhei.ttf" ; //windows里的字体资源路径 //PdfFont f2 = PdfFontFactory.createFont("STSong-Light", "UniGB-UCS2-H"); PdfFont pfont = PdfFontFactory.createFont(path, PdfEncodings.IDENTITY_H); //PdfFontFactory.EmbeddingStrategy.FORCE_EMBEDDED //PdfFont pfont = PdfFontFactory.createRegisteredFont(path, PdfEncodings.IDENTITY_H); PdfDocument pdfDoc = new PdfDocument(writer); Document document = new Document(pdfDoc); //document.setFont(pfont); //document.open(); document.add( new Paragraph( "Some content here" ).setFont(pfont).setFontColor(ColorConstants.RED)); //Set attributes here //document.("Geovin Du 涂聚文"); // document.addCreationDate(); //document.addCreator("dusystem.com"); //document.addTitle("Set Attribute Example"); //document.addSubject("An example to show how attributes can be added to pdf files."); Table table = new Table( 3 , true ); // 3 columns. //table.setHeight(100); //Width 100% //table.setHeight(10f); //Space before table //table.setSpacingAfter(10f); //Space after table //Set Column widths //float[] columnWidths = {1f, 1f, 1f}; //table.setHeight(columnWidths); Cell cell1 = new Cell().add( new Paragraph( "深圳市" ).setFont(pfont)); cell1.setBorder( new SolidBorder(ColorConstants.BLACK, 1 )); cell1.setPaddingLeft( 10 ); cell1.setHorizontalAlignment(HorizontalAlignment.CENTER); cell1.setVerticalAlignment(VerticalAlignment.MIDDLE); Cell cell2 = new Cell().add( new Paragraph( "南山區" ).setFont(pfont).setFontColor(ColorConstants.GREEN)); cell2.setBorder( new SolidBorder(ColorConstants.BLACK, 1 )); cell2.setPaddingLeft( 10 ); cell2.setHorizontalAlignment(HorizontalAlignment.LEFT); cell2.setVerticalAlignment(VerticalAlignment.MIDDLE); //.setVerticalAlignment(VerticalAlignment.MIDDLE) Cell cell3 = new Cell().add( new Paragraph( "科技園" ).setFont(pfont)); cell3.setBackgroundColor(ColorConstants.RED); cell3.setPaddingLeft( 10 ); cell3.setHorizontalAlignment(HorizontalAlignment.CENTER); cell3.setVerticalAlignment(VerticalAlignment.MIDDLE); table.addCell(cell1); table.addCell(cell2); table.addCell(cell3); // table.complete(); document.add(table); List<Orders> orders = new ArrayList<>(); Orders order = new Orders(); order.setProductId( 1 ); ; order.setProduct( "中國電器" ); order.setQty( 50 ); order.setPrice( 52.0 ); orders.add(order); order = new Orders(); order.setProductId( 2 ); order.setProduct ( "日本電器" ); order.setQty( 150 ); order.setPrice( 252.0 ); orders.add(order); //2 Table tableData = new Table( 5 , true ); tableData.setBorder( new SolidBorder(ColorConstants.BLACK, 1 )); tableData.setFont(pfont); Cell headerProductId = new Cell( 1 , 1 ).setTextAlignment(TextAlignment.LEFT).add( new Paragraph( "ID" )); Cell headerProduct = new Cell( 1 , 1 ).setTextAlignment(TextAlignment.LEFT).add( new Paragraph( "產品名稱" )); Cell headerProductPrice = new Cell( 1 , 1 ).setTextAlignment(TextAlignment.LEFT).add( new Paragraph( "價格" )); Cell headerProductQty = new Cell( 1 , 1 ).setTextAlignment(TextAlignment.LEFT).add( new Paragraph( "數量" )); Cell headerTotal = new Cell( 1 , 1 ).setTextAlignment(TextAlignment.LEFT).add( new Paragraph( "合計" )); tableData.addCell(headerProductId); tableData.addCell(headerProduct); tableData.addCell(headerProductPrice); tableData.addCell(headerProductQty); tableData.addCell(headerTotal); double grandTotalVal = 0 ; for (Orders c:orders) { Cell productid = new Cell( 1 , 1 ).setTextAlignment(TextAlignment.LEFT).add( new Paragraph(String.valueOf(c.getProductId()))); Cell product = new Cell( 1 , 1 ).setTextAlignment(TextAlignment.LEFT).add( new Paragraph(c.getProduct())); Cell price = new Cell( 1 , 1 ).setTextAlignment(TextAlignment.LEFT).add( new Paragraph(String.valueOf(c.getPrice()))); Cell qty = new Cell( 1 , 1 ).setTextAlignment(TextAlignment.LEFT).add( new Paragraph(String.valueOf(c.getQty()))); var value = c.getPrice() * c.getQty(); Cell total = new Cell( 1 , 1 ).setTextAlignment(TextAlignment.LEFT).add( new Paragraph(String.valueOf(value))); grandTotalVal += value; tableData.addCell(productid); tableData.addCell(product); tableData.addCell(price); tableData.addCell(qty); tableData.addCell(total); } Cell grandTotalHeader = new Cell( 1 , 4 ).setTextAlignment(TextAlignment.RIGHT).add( new Paragraph( "合計: " )).setBorderBottom( new SolidBorder(ColorConstants.BLACK, 1 )); Cell grandTotal = new Cell( 1 , 1 ).setTextAlignment(TextAlignment.LEFT).add( new Paragraph( " " + String.valueOf(grandTotalVal))).setBorderBottom( new SolidBorder(ColorConstants.BLACK, 1 )); grandTotal.setBorderBottom( new SolidBorder(ColorConstants.BLACK, 1 )); tableData.addCell(grandTotalHeader); tableData.addCell(grandTotal); //tableData.complete(); document.add(tableData); Table tablebottom = new Table( 4 , true ); //tablebottom.setBorder(new SolidBorder(ColorConstants.BLACK, 3)); tablebottom.setHorizontalAlignment(HorizontalAlignment.CENTER); tablebottom.addCell( new Cell().add( new Paragraph( "產品ID" ).setFont(pfont)).setBorderTop( new SolidBorder(ColorConstants.BLACK, 3 ))); tablebottom.addCell( new Cell().add( new Paragraph( "名稱" ).setFont(pfont)).setBorderTop( new SolidBorder(ColorConstants.BLACK, 3 ))); tablebottom.addCell( new Cell().add( new Paragraph( "書量" ).setFont(pfont)).setBorderTop( new SolidBorder(ColorConstants.BLACK, 3 ))); tablebottom.addCell( new Cell().add( new Paragraph( "價格" ).setFont(pfont)).setBorderTop( new SolidBorder(ColorConstants.BLACK, 3 ))); //table.setHeaderRows(1); //Cell[] cells = table.getCell(2,3); //for (int j=0;j<cells.length;j++){ //cells[j].setBackgroundColor(ColorConstants.BLACK); //} for (Orders s:orders){ Cell cel1= new Cell().add( new Paragraph(String.valueOf(s.getProductId()))).setBorder( new SolidBorder(ColorConstants.BLACK, 3 )); cel1.setTextAlignment(TextAlignment.CENTER); tablebottom.addCell(cel1); Cell cel2= new Cell().add( new Paragraph(s.getProduct()).setFont(pfont)).setBorder( new SolidBorder(ColorConstants.BLACK, 3 )); cel2.setTextAlignment(TextAlignment.CENTER); tablebottom.addCell(cel2); Cell cel3= new Cell().add( new Paragraph(String.valueOf(s.getQty()))).setBorder( new SolidBorder(ColorConstants.BLACK, 3 )); cel3.setTextAlignment(TextAlignment.CENTER); tablebottom.addCell(cel3); Cell cel4= new Cell().add( new Paragraph(String.valueOf(s.getPrice()))).setBorder( new SolidBorder(ColorConstants.BLACK, 3 )); cel4.setTextAlignment(TextAlignment.CENTER); tablebottom.addCell(cel4); } Cell cel1= new Cell().add( new Paragraph( "3" ).setBorderBottom( new SolidBorder(ColorConstants.BLACK, 3 ))); cel1.setTextAlignment(TextAlignment.CENTER); tablebottom.addCell(cel1); Cell cel2= new Cell().add( new Paragraph( "涂聚文" ).setFont(pfont).setBorderBottom( new SolidBorder(ColorConstants.BLACK, 3 )).setBorderLeft( new SolidBorder(ColorConstants.BLACK, 3 ))); cel2.setTextAlignment(TextAlignment.CENTER); tablebottom.addCell(cel2); Cell cel3= new Cell().add( new Paragraph( "333" ).setBorderBottom( new SolidBorder(ColorConstants.BLACK, 3 )).setBorderLeft( new SolidBorder(ColorConstants.BLACK, 3 ))); cel3.setTextAlignment(TextAlignment.CENTER); tablebottom.addCell(cel3); Cell cel4= new Cell().add( new Paragraph( "3333" ).setBorderBottom( new SolidBorder(ColorConstants.BLACK, 3 )).setBorderLeft( new SolidBorder(ColorConstants.BLACK, 3 ))); cel4.setTextAlignment(TextAlignment.CENTER); tablebottom.addCell(cel4); document.add(tablebottom); ImageData imageData = ImageDataFactory.create( "geovindu.jpg" ); Image image1 = new Image(imageData); //Fixed Positioning //image1.setAbsolutePosition(205f, 150f); //Scale to new height and new width of image image1.scaleAbsolute( 100 , 100 ); //Add to document document.add(image1); document.close(); writer.close(); } catch (Exception e) { e.printStackTrace(); } } } |
调用:
1 2 3 4 | // SimpleItext itextpdf= new SimpleItext(); //itextpdf.test(); itextpdf.Createpdf(); |
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
2022-11-21 Python: read csv file
2022-11-21 ffmpeg: 视频文件操作
2021-11-21 javascript: convert html web pages to pdf in javascript
2011-11-21 CSS DIV大图片右上角叠加小图片