代码如下,jar包为itext.jar,itextAsia.jar,最好都是最新的 ;2张图片也在最后贴出,把图片放到D盘可以直接生成制定格式的pdf。
最后生成的pdf如下:

代码如下:

  1. package com.itext.test;
  2.  
  3. import java.io.FileOutputStream;
  4.  
  5. import com.itextpdf.text.BaseColor;
  6. import com.itextpdf.text.Document;
  7. import com.itextpdf.text.Element;
  8. import com.itextpdf.text.Image;
  9. import com.itextpdf.text.PageSize;
  10. import com.itextpdf.text.Paragraph;
  11. import com.itextpdf.text.pdf.BaseFont;
  12. import com.itextpdf.text.pdf.PdfPCell;
  13. import com.itextpdf.text.pdf.PdfPTable;
  14. import com.itextpdf.text.pdf.PdfWriter;
  15.  
  16. public class BaoXiaoDan {
  17. public static void main(String[] args) {
  18.  
  19. try
  20. {
  21. Document document = new Document(PageSize.A4.rotate());
  22. PdfWriter.getInstance(document, new FileOutputStream("D:\\Helloworld.PDF"));
  23.  
  24. //设置字体
  25. BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
  26. com.itextpdf.text.Font FontChinese24 = new com.itextpdf.text.Font(bfChinese, 24, com.itextpdf.text.Font.BOLD);
  27. com.itextpdf.text.Font FontChinese18 = new com.itextpdf.text.Font(bfChinese, 18, com.itextpdf.text.Font.BOLD);
  28. com.itextpdf.text.Font FontChinese16 = new com.itextpdf.text.Font(bfChinese, 16, com.itextpdf.text.Font.BOLD);
  29. com.itextpdf.text.Font FontChinese12 = new com.itextpdf.text.Font(bfChinese, 12, com.itextpdf.text.Font.NORMAL);
  30. com.itextpdf.text.Font FontChinese11Bold = new com.itextpdf.text.Font(bfChinese, 11, com.itextpdf.text.Font.BOLD);
  31. com.itextpdf.text.Font FontChinese11 = new com.itextpdf.text.Font(bfChinese, 11, com.itextpdf.text.Font.ITALIC);
  32. com.itextpdf.text.Font FontChinese11Normal = new com.itextpdf.text.Font(bfChinese, 11, com.itextpdf.text.Font.NORMAL);
  33.  
  34. document.open();
  35. //table1
  36. PdfPTable table1 = new PdfPTable(3);
  37. PdfPCell cell11 = new PdfPCell(new Paragraph("费用报销",FontChinese24));
  38. cell11.setVerticalAlignment(Element.ALIGN_MIDDLE);
  39. cell11.setHorizontalAlignment(Element.ALIGN_CENTER);
  40. cell11.setBorder(0);
  41. String imagePath = "D:/alibaba.jpg";
  42. Image image1 = Image.getInstance(imagePath);
  43.  
  44. Image image2 = Image.getInstance(imagePath);
  45. //设置每列宽度比例
  46. int width11[] = {35,40,25};
  47. table1.setWidths(width11);
  48. table1.getDefaultCell().setBorder(0);
  49. table1.addCell(image1);
  50. table1.addCell(cell11);
  51. table1.addCell(image2);
  52. document.add(table1);
  53. //加入空行
  54. Paragraph blankRow1 = new Paragraph(18f, " ", FontChinese18);
  55. document.add(blankRow1);
  56.  
  57. //table2
  58. PdfPTable table2 = new PdfPTable(2);
  59. //设置每列宽度比例
  60. int width21[] = {2,98};
  61. table2.setWidths(width21);
  62. table2.getDefaultCell().setBorder(0);
  63. PdfPCell cell21 = new PdfPCell(new Paragraph("报销概要",FontChinese16));
  64. String imagePath2 = "D:/boder.jpg";
  65. Image image21 = Image.getInstance(imagePath2);
  66. cell21.setBorder(0);
  67. table2.addCell(image21);
  68. table2.addCell(cell21);
  69. document.add(table2);
  70. //加入空行
  71. Paragraph blankRow2 = new Paragraph(18f, " ", FontChinese18);
  72. document.add(blankRow2);
  73.  
  74. //table3
  75. PdfPTable table3 = new PdfPTable(3);
  76. int width3[] = {40,35,25};
  77. table3.setWidths(width3);
  78. PdfPCell cell31 = new PdfPCell(new Paragraph("申请人:"+"XXX",FontChinese11Normal));
  79. PdfPCell cell32 = new PdfPCell(new Paragraph("日期:"+"2011-11-11",FontChinese11Normal));
  80. PdfPCell cell33 = new PdfPCell(new Paragraph("报销单号:"+"123456789",FontChinese11Normal));
  81. cell31.setBorder(0);
  82. cell32.setBorder(0);
  83. cell33.setBorder(0);
  84. table3.addCell(cell31);
  85. table3.addCell(cell32);
  86. table3.addCell(cell33);
  87. document.add(table3);
  88. //加入空行
  89. Paragraph blankRow31 = new Paragraph(18f, " ", FontChinese11);
  90. document.add(blankRow31);
  91.  
  92. //table4
  93. PdfPTable table4 = new PdfPTable(2);
  94. int width4[] = {40,60};
  95. table4.setWidths(width4);
  96. PdfPCell cell41 = new PdfPCell(new Paragraph("公司:"+"XXX",FontChinese11Normal));
  97. PdfPCell cell42 = new PdfPCell(new Paragraph("部门:"+"XXX",FontChinese11Normal));
  98. cell41.setBorder(0);
  99. cell42.setBorder(0);
  100. table4.addCell(cell41);
  101. table4.addCell(cell42);
  102. document.add(table4);
  103. //加入空行
  104. Paragraph blankRow41 = new Paragraph(18f, " ", FontChinese11);
  105. document.add(blankRow41);
  106.  
  107. //table5
  108. PdfPTable table5 = new PdfPTable(1);
  109. PdfPCell cell51 = new PdfPCell(new Paragraph("报销说明:"+"XXX",FontChinese11));
  110. cell51.setBorder(0);
  111. table5.addCell(cell51);
  112. document.add(table5);
  113. //加入空行
  114. Paragraph blankRow51 = new Paragraph(18f, " ", FontChinese18);
  115. document.add(blankRow51);
  116.  
  117. //table6
  118. PdfPTable table6 = new PdfPTable(2);
  119. table6.getDefaultCell().setBorder(0);
  120. table6.setWidths(width21);
  121. PdfPCell cell61 = new PdfPCell(new Paragraph("报销明细",FontChinese16));
  122. cell61.setBorder(0);
  123. table6.addCell(image21);
  124. table6.addCell(cell61);
  125. document.add(table6);
  126. //加入空行
  127. Paragraph blankRow4 = new Paragraph(18f, " ", FontChinese16);
  128. document.add(blankRow4);
  129.  
  130. //table7
  131. PdfPTable table7 = new PdfPTable(6);
  132. BaseColor lightGrey = new BaseColor(0xCC,0xCC,0xCC);
  133. int width7[] = {20,18,13,20,14,15};
  134. table7.setWidths(width7);
  135. PdfPCell cell71 = new PdfPCell(new Paragraph("费用类型",FontChinese11Bold));
  136. PdfPCell cell72 = new PdfPCell(new Paragraph("费用发生时间",FontChinese11Bold));
  137. PdfPCell cell73 = new PdfPCell(new Paragraph("详细信息",FontChinese11Bold));
  138. PdfPCell cell74 = new PdfPCell(new Paragraph("消费金币/币种",FontChinese11Bold));
  139. PdfPCell cell75 = new PdfPCell(new Paragraph("报销汇率",FontChinese11Bold));
  140. PdfPCell cell76 = new PdfPCell(new Paragraph("报销金额",FontChinese11Bold));
  141. //表格高度
  142. cell71.setFixedHeight(25);
  143. cell72.setFixedHeight(25);
  144. cell73.setFixedHeight(25);
  145. cell74.setFixedHeight(25);
  146. cell75.setFixedHeight(25);
  147. cell76.setFixedHeight(25);
  148. //水平居中
  149. cell71.setHorizontalAlignment(Element.ALIGN_CENTER);
  150. cell72.setHorizontalAlignment(Element.ALIGN_CENTER);
  151. cell73.setHorizontalAlignment(Element.ALIGN_CENTER);
  152. cell74.setHorizontalAlignment(Element.ALIGN_CENTER);
  153. cell75.setHorizontalAlignment(Element.ALIGN_CENTER);
  154. cell76.setHorizontalAlignment(Element.ALIGN_CENTER);
  155. //垂直居中
  156. cell71.setVerticalAlignment(Element.ALIGN_MIDDLE);
  157. cell72.setVerticalAlignment(Element.ALIGN_MIDDLE);
  158. cell73.setVerticalAlignment(Element.ALIGN_MIDDLE);
  159. cell74.setVerticalAlignment(Element.ALIGN_MIDDLE);
  160. cell75.setVerticalAlignment(Element.ALIGN_MIDDLE);
  161. cell76.setVerticalAlignment(Element.ALIGN_MIDDLE);
  162. //边框颜色
  163. cell71.setBorderColor(lightGrey);
  164. cell72.setBorderColor(lightGrey);
  165. cell73.setBorderColor(lightGrey);
  166. cell74.setBorderColor(lightGrey);
  167. cell75.setBorderColor(lightGrey);
  168. cell76.setBorderColor(lightGrey);
  169. //去掉左右边框
  170. cell71.disableBorderSide(8);
  171. cell72.disableBorderSide(4);
  172. cell72.disableBorderSide(8);
  173. cell73.disableBorderSide(4);
  174. cell73.disableBorderSide(8);
  175. cell74.disableBorderSide(4);
  176. cell74.disableBorderSide(8);
  177. cell75.disableBorderSide(4);
  178. cell75.disableBorderSide(8);
  179. cell76.disableBorderSide(4);
  180. table7.addCell(cell71);
  181. table7.addCell(cell72);
  182. table7.addCell(cell73);
  183. table7.addCell(cell74);
  184. table7.addCell(cell75);
  185. table7.addCell(cell76);
  186. document.add(table7);
  187.  
  188. //table8
  189. PdfPTable table8 = new PdfPTable(6);
  190. int width8[] = {20,18,13,20,14,15};
  191. table8.setWidths(width8);
  192. PdfPCell cell81 = new PdfPCell(new Paragraph("差旅报销",FontChinese12));
  193. PdfPCell cell82 = new PdfPCell(new Paragraph("2011-11-11",FontChinese12));
  194. PdfPCell cell83 = new PdfPCell(new Paragraph("XXX",FontChinese12));
  195. PdfPCell cell84 = new PdfPCell(new Paragraph("XXX",FontChinese12));
  196. PdfPCell cell85 = new PdfPCell(new Paragraph("XXX",FontChinese12));
  197. PdfPCell cell86 = new PdfPCell(new Paragraph("XXX",FontChinese12));
  198. //表格高度
  199. cell81.setFixedHeight(25);
  200. cell82.setFixedHeight(25);
  201. cell83.setFixedHeight(25);
  202. cell84.setFixedHeight(25);
  203. cell85.setFixedHeight(25);
  204. cell86.setFixedHeight(25);
  205. //水平居中
  206. cell81.setHorizontalAlignment(Element.ALIGN_CENTER);
  207. cell82.setHorizontalAlignment(Element.ALIGN_CENTER);
  208. cell83.setHorizontalAlignment(Element.ALIGN_CENTER);
  209. cell84.setHorizontalAlignment(Element.ALIGN_CENTER);
  210. cell85.setHorizontalAlignment(Element.ALIGN_CENTER);
  211. cell86.setHorizontalAlignment(Element.ALIGN_CENTER);
  212. //垂直居中
  213. cell81.setVerticalAlignment(Element.ALIGN_MIDDLE);
  214. cell82.setVerticalAlignment(Element.ALIGN_MIDDLE);
  215. cell83.setVerticalAlignment(Element.ALIGN_MIDDLE);
  216. cell84.setVerticalAlignment(Element.ALIGN_MIDDLE);
  217. cell85.setVerticalAlignment(Element.ALIGN_MIDDLE);
  218. cell86.setVerticalAlignment(Element.ALIGN_MIDDLE);
  219. //边框颜色
  220. cell81.setBorderColor(lightGrey);
  221. cell82.setBorderColor(lightGrey);
  222. cell83.setBorderColor(lightGrey);
  223. cell84.setBorderColor(lightGrey);
  224. cell85.setBorderColor(lightGrey);
  225. cell86.setBorderColor(lightGrey);
  226. //去掉左右边框
  227. cell81.disableBorderSide(8);
  228. cell82.disableBorderSide(4);
  229. cell82.disableBorderSide(8);
  230. cell83.disableBorderSide(4);
  231. cell83.disableBorderSide(8);
  232. cell84.disableBorderSide(4);
  233. cell84.disableBorderSide(8);
  234. cell85.disableBorderSide(4);
  235. cell85.disableBorderSide(8);
  236. cell86.disableBorderSide(4);
  237. table8.addCell(cell81);
  238. table8.addCell(cell82);
  239. table8.addCell(cell83);
  240. table8.addCell(cell84);
  241. table8.addCell(cell85);
  242. table8.addCell(cell86);
  243. document.add(table8);
  244. //加入空行
  245. Paragraph blankRow5 = new Paragraph(18f, " ", FontChinese18);
  246. document.add(blankRow5);
  247.  
  248. //table9
  249. PdfPTable table9 = new PdfPTable(3);
  250. int width9[] = {30,50,20};
  251. table9.setWidths(width9);
  252. PdfPCell cell91 = new PdfPCell(new Paragraph("",FontChinese12));
  253. PdfPCell cell92 = new PdfPCell(new Paragraph("收到的报销金额",FontChinese12));
  254. PdfPCell cell93 = new PdfPCell(new Paragraph("1000",FontChinese24));
  255. cell92.setHorizontalAlignment(Element.ALIGN_RIGHT);
  256. cell92.setVerticalAlignment(Element.ALIGN_MIDDLE);
  257. cell93.setHorizontalAlignment(Element.ALIGN_LEFT);
  258. cell93.setVerticalAlignment(Element.ALIGN_MIDDLE);
  259. cell91.setBorder(0);
  260. cell92.setBorder(0);
  261. cell93.setBorder(0);
  262. table9.addCell(cell91);
  263. table9.addCell(cell92);
  264. table9.addCell(cell93);
  265. document.add(table9);
  266.  
  267. document.close();
  268.  
  269. } catch (Exception ex)
  270. {
  271. ex.printStackTrace();
  272. }
  273. }
  274. }

使用itext生成pdf的,各种布局的更多相关文章

  1. 在linux环境下使用itext生成pdf

    转载请注明出处 https://www.cnblogs.com/majianming/p/9537173.html 项目中需要对订单生成pdf文件,在不断的尝试之后,终于生成了比较满意的pdf文档. ...

  2. Java Itext 生成PDF文件

    利用Java Itext生成PDF文件并导出,实现效果如下: PDFUtil.java package com.jeeplus.modules.order.util; import java.io.O ...

  3. 用itext生成PDF报错:Font 'STSong-Light1' with 'UniGB-UCS2-H' is not recognized.

    用itext生成PDF报错,加上try catch捕获到异常是 BaseFont bFont = BaseFont.createFont("STSong-Light1", &quo ...

  4. 【PDF】java使用Itext生成pdf文档--详解

    [API接口]  一.Itext简介 API地址:javadoc/index.html:如 D:/MyJAR/原JAR包/PDF/itext-5.5.3/itextpdf-5.5.3-javadoc/ ...

  5. Itext生成pdf文件

    来源:https://my.oschina.net/lujianing/blog/894365 1.背景 在某些业务场景中,需要提供相关的电子凭证,比如网银/支付宝中转账的电子回单,签约的电子合同等. ...

  6. 【Java】使用iText生成PDF文件

    iText介绍 iText是著名的开放源码的站点sourceforge一个项目,是用于生成PDF文档的一个java类库.通过iText不仅可以生成PDF或rtf的文档,而且可以将XML.Html文件转 ...

  7. java使用iText生成pdf表格

    转载地址:http://www.open-open.com/code/view/1424011530749 首先需要你自己下载itext相关的jar包并添加引用,或者在maven中添加如下引用配置: ...

  8. 利用itext生成pdf的简单例子

    一.itext简介 iText是著名的开放源码的站点sourceforge一个项目,是用于生成PDF文档的一个java类库.通过iText不仅可以生成PDF或rtf的文档,而且可以将XML.Html文 ...

  9. itext 生成pdf文件添加页眉页脚

    原文来自:https://www.cnblogs.com/joann/p/5511905.html 我只是记录所有jar版本,由于版本冲突及不兼容很让人头疼的,一共需要5个jar, 其中itextpd ...

  10. iText生成PDF 格式报表

    1.导包 <dependency> <groupId>com.lowagie</groupId> <artifactId>itext</artif ...

随机推荐

  1. [UE4]Spacer

    一.Spacer:留白占位控件 二.如下图所示,如果想要2个按钮都在容器右对齐: 三.可以放一个Spacer到最左边,设置成Fill,Spacer控件就是起到占位的作用.  

  2. 源码小结:Java 集合ArrayList,LinkedList 源码

    现在这篇主要讲List集合的三个子类: ArrayList 底层数据结构是数组.线程不安全 LinkedList 底层数据结构是链表.线程不安全 Vector 底层数据结构是数组.线程安全 Array ...

  3. nodejs 函数 :html2js

    var fs = require("fs"); var path = require("path"); function propStringToMap(ss1 ...

  4. Spring线程池的5个要素

    <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-// ...

  5. Digital Twin的8种解读!

    国际8大主流厂商对digital twin的理解,很有必要来一次汇总! 据IDC预测,2017年世界上将有40%的大型生产商都会应用虚拟仿真技术来为他们的生产过程进行建模,Digital Twin可以 ...

  6. nexus的安装和简介

    下载nexus Nexus 是Maven仓库管理器,通过nexus可以搭建maven仓库,同时nexus还提供强大的仓库管理功能,构件搜索功能等. 下载Nexus, 下载地址:http://www.s ...

  7. linux查看磁盘大小df命令

    df -h https://www.cnblogs.com/sparkdev/p/9273094.html

  8. Scrollview包裹布局问题。

    输入框获取焦点,键盘弹出,背景图片上移: https://blog.csdn.net/wljian1/article/details/79962802 android:scrollbarThumbVe ...

  9. servlet中的请求响应与重定向区别

    一.概念 请求响应(转发):将客户端请求转发另一个servlet或者jsp页面------------------------getRequestDispatcher()方法 重定向: 返回一个连接给 ...

  10. PHPStorm 2018 的安装 汉化 与使用

    下载地址 和安装方法 链接:https://pan.baidu.com/s/1FT8aZoQajw044qlNXkRPfg 提取码:z4sx 配置与使用方法 https://blog.csdn.net ...