java: spire.pdf.free 9.12.3 create pdf
可以用windows 系统中文字体,也可以从文件夹的字体文件
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 | /** * encoding: utf-8 * 版权所有 2024 ©涂聚文有限公司 * 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎 * 描述: * # Author : geovindu,Geovin Du 涂聚文. * # IDE : IntelliJ IDEA 2023.1 Java 17 * # Datetime : 2024 - 2024/1/11 - 20:32 * # User : geovindu * # Product : IntelliJ IDEA * # Project : EssentialAlgorithms * # File : simpleTable.java * # explain : 学习 类 **/ package BLL; import com.spire.pdf.*; import com.spire.pdf.graphics.*; import com.spire.pdf.tables.*; import java.awt.*; import java.awt.geom.Point2D; import com.spire.pdf.PdfPageBase; import com.spire.pdf.graphics.*; public class SimpleTable { /** * 生成表格示例 */ public void createtable() { try { String fontFile2 = "font/MHeiHK-Light.TTF" ; String path = "C:/WINDOWS/Fonts/simhei.ttf" ; //windows里的字体资源路径 PdfDocument doc = new PdfDocument(); //Set margin PdfUnitConvertor unitCvtr = new PdfUnitConvertor(); PdfMargins margin = new PdfMargins(); margin.setTop(unitCvtr.convertUnits( 2 .54f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point)); margin.setBottom(margin.getTop()); margin.setLeft(unitCvtr.convertUnits( 3 .17f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point)); margin.setRight(margin.getLeft()); // Create one page PdfPageBase page = doc.getPages().add(PdfPageSize.A4, margin); float y = 10 ; //Draw text using private font String fontFileName = "font/MHeiHK-Light.TTF" ; PdfTrueTypeFont trueTypeFont = new PdfTrueTypeFont(fontFileName, 14f); //Draw title PdfBrush brush1 = PdfBrushes.getBlack(); PdfTrueTypeFont font1 = new PdfTrueTypeFont(trueTypeFont,12f); PdfStringFormat format1 = new PdfStringFormat(PdfTextAlignment.Center); page.getCanvas().drawString( "国家列表" , trueTypeFont, brush1, page.getCanvas().getClientSize().getWidth() / 2 , y, format1); y = y + ( float ) font1.measureString( "国家列表" , format1).getHeight(); y = y + 5 ; String[] data = { "国家;首都;洲域;面积;人口" , "Argentina;Buenos Aires;South America;2777815;32300003" , "Bolivia;La Paz;South America;1098575;7300000" , "Brazil;Brasilia;South America;8511196;150400000" , "Canada;Ottawa;North America;9976147;26500000" , "Chile;Santiago;South America;756943;13200000" , "Colombia;Bagota;South America;1138907;33000000" , "Cuba;Havana;North America;114524;10600000" , "Ecuador;Quito;South America;455502;10600000" , "El Salvador;San Salvador;North America;20865;5300000" , "Guyana;Georgetown;South America;214969;800000" , "Jamaica;Kingston;North America;11424;2500000" , "Mexico;Mexico City;North America;1967180;88600000" , "Nicaragua;Managua;North America;139000;3900000" , "Paraguay;Asuncion;South America;406576;4660000" , "Peru;Lima;South America;1285215;21600000" , "United States of America;Washington;North America;9363130;249200000" , "Uruguay;Montevideo;South America;176140;3002000" , "Venezuela;Caracas;South America;912047;19700000" }; String[][] dataSource = new String[data.length][]; for ( int i = 0 ; i < data.length; i++) { dataSource[i] = data[i].split( "[;]" , - 1 ); } PdfTable table = new PdfTable(); //设置表头字体和样式 PdfTrueTypeFont font = new PdfTrueTypeFont( new Font( "宋体" ,Font.PLAIN, 12 )); table.getStyle().getHeaderStyle().setFont(font); table.getStyle().getHeaderStyle().setBackgroundBrush(PdfBrushes.getCadetBlue()); PdfTrueTypeFont fontBody = new PdfTrueTypeFont( new Font( "宋体" , Font.PLAIN, 10 )); //设置偶数行字体 table.getStyle().getDefaultStyle().setFont(fontBody); //设置奇数行字体 table.getStyle().getAlternateStyle().setFont(fontBody); table.getStyle().setCellPadding( 2 ); table.getStyle().setHeaderSource(PdfHeaderSource.Rows); 调用:<br><br>table.getStyle().setHeaderRowCount( 1 ); table.getStyle().setShowHeader( true ); table.setDataSource(dataSource); PdfLayoutResult result = table.draw(page, new Point2D.Float( 0 , y)); y = y + ( float ) result.getBounds().getHeight() + 5 ; PdfBrush brush2 = PdfBrushes.getGray(); PdfTrueTypeFont font2 = new PdfTrueTypeFont(trueTypeFont,12f); page.getCanvas().drawString(String.format( "国家* %1$s个:." , data.length - 1 ), font2, brush2, 5 , y); //Save pdf file. doc.saveToFile( "simpleTable.pdf" ); } catch (Exception exception) { System.out.println(exception.toString()); } } } |
调用:
1 2 3 | //生成PDF文檔 SimpleTable simpleTable= new SimpleTable(); simpleTable.createtable(); |
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
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大图片右上角叠加小图片