使用itext生成pdf的,各种布局
代码如下,jar包为itext.jar,itextAsia.jar,最好都是最新的 ;2张图片也在最后贴出,把图片放到D盘可以直接生成制定格式的pdf。
最后生成的pdf如下:
代码如下:
package com.itext.test; import java.io.FileOutputStream; import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter; public class BaoXiaoDan {
public static void main(String[] args) { try
{
Document document = new Document(PageSize.A4.rotate());
PdfWriter.getInstance(document, new FileOutputStream("D:\\Helloworld.PDF")); //设置字体
BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
com.itextpdf.text.Font FontChinese24 = new com.itextpdf.text.Font(bfChinese, 24, com.itextpdf.text.Font.BOLD);
com.itextpdf.text.Font FontChinese18 = new com.itextpdf.text.Font(bfChinese, 18, com.itextpdf.text.Font.BOLD);
com.itextpdf.text.Font FontChinese16 = new com.itextpdf.text.Font(bfChinese, 16, com.itextpdf.text.Font.BOLD);
com.itextpdf.text.Font FontChinese12 = new com.itextpdf.text.Font(bfChinese, 12, com.itextpdf.text.Font.NORMAL);
com.itextpdf.text.Font FontChinese11Bold = new com.itextpdf.text.Font(bfChinese, 11, com.itextpdf.text.Font.BOLD);
com.itextpdf.text.Font FontChinese11 = new com.itextpdf.text.Font(bfChinese, 11, com.itextpdf.text.Font.ITALIC);
com.itextpdf.text.Font FontChinese11Normal = new com.itextpdf.text.Font(bfChinese, 11, com.itextpdf.text.Font.NORMAL); document.open();
//table1
PdfPTable table1 = new PdfPTable(3);
PdfPCell cell11 = new PdfPCell(new Paragraph("费用报销",FontChinese24));
cell11.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell11.setHorizontalAlignment(Element.ALIGN_CENTER);
cell11.setBorder(0);
String imagePath = "D:/alibaba.jpg";
Image image1 = Image.getInstance(imagePath); Image image2 = Image.getInstance(imagePath);
//设置每列宽度比例
int width11[] = {35,40,25};
table1.setWidths(width11);
table1.getDefaultCell().setBorder(0);
table1.addCell(image1);
table1.addCell(cell11);
table1.addCell(image2);
document.add(table1);
//加入空行
Paragraph blankRow1 = new Paragraph(18f, " ", FontChinese18);
document.add(blankRow1); //table2
PdfPTable table2 = new PdfPTable(2);
//设置每列宽度比例
int width21[] = {2,98};
table2.setWidths(width21);
table2.getDefaultCell().setBorder(0);
PdfPCell cell21 = new PdfPCell(new Paragraph("报销概要",FontChinese16));
String imagePath2 = "D:/boder.jpg";
Image image21 = Image.getInstance(imagePath2);
cell21.setBorder(0);
table2.addCell(image21);
table2.addCell(cell21);
document.add(table2);
//加入空行
Paragraph blankRow2 = new Paragraph(18f, " ", FontChinese18);
document.add(blankRow2); //table3
PdfPTable table3 = new PdfPTable(3);
int width3[] = {40,35,25};
table3.setWidths(width3);
PdfPCell cell31 = new PdfPCell(new Paragraph("申请人:"+"XXX",FontChinese11Normal));
PdfPCell cell32 = new PdfPCell(new Paragraph("日期:"+"2011-11-11",FontChinese11Normal));
PdfPCell cell33 = new PdfPCell(new Paragraph("报销单号:"+"123456789",FontChinese11Normal));
cell31.setBorder(0);
cell32.setBorder(0);
cell33.setBorder(0);
table3.addCell(cell31);
table3.addCell(cell32);
table3.addCell(cell33);
document.add(table3);
//加入空行
Paragraph blankRow31 = new Paragraph(18f, " ", FontChinese11);
document.add(blankRow31); //table4
PdfPTable table4 = new PdfPTable(2);
int width4[] = {40,60};
table4.setWidths(width4);
PdfPCell cell41 = new PdfPCell(new Paragraph("公司:"+"XXX",FontChinese11Normal));
PdfPCell cell42 = new PdfPCell(new Paragraph("部门:"+"XXX",FontChinese11Normal));
cell41.setBorder(0);
cell42.setBorder(0);
table4.addCell(cell41);
table4.addCell(cell42);
document.add(table4);
//加入空行
Paragraph blankRow41 = new Paragraph(18f, " ", FontChinese11);
document.add(blankRow41); //table5
PdfPTable table5 = new PdfPTable(1);
PdfPCell cell51 = new PdfPCell(new Paragraph("报销说明:"+"XXX",FontChinese11));
cell51.setBorder(0);
table5.addCell(cell51);
document.add(table5);
//加入空行
Paragraph blankRow51 = new Paragraph(18f, " ", FontChinese18);
document.add(blankRow51); //table6
PdfPTable table6 = new PdfPTable(2);
table6.getDefaultCell().setBorder(0);
table6.setWidths(width21);
PdfPCell cell61 = new PdfPCell(new Paragraph("报销明细",FontChinese16));
cell61.setBorder(0);
table6.addCell(image21);
table6.addCell(cell61);
document.add(table6);
//加入空行
Paragraph blankRow4 = new Paragraph(18f, " ", FontChinese16);
document.add(blankRow4); //table7
PdfPTable table7 = new PdfPTable(6);
BaseColor lightGrey = new BaseColor(0xCC,0xCC,0xCC);
int width7[] = {20,18,13,20,14,15};
table7.setWidths(width7);
PdfPCell cell71 = new PdfPCell(new Paragraph("费用类型",FontChinese11Bold));
PdfPCell cell72 = new PdfPCell(new Paragraph("费用发生时间",FontChinese11Bold));
PdfPCell cell73 = new PdfPCell(new Paragraph("详细信息",FontChinese11Bold));
PdfPCell cell74 = new PdfPCell(new Paragraph("消费金币/币种",FontChinese11Bold));
PdfPCell cell75 = new PdfPCell(new Paragraph("报销汇率",FontChinese11Bold));
PdfPCell cell76 = new PdfPCell(new Paragraph("报销金额",FontChinese11Bold));
//表格高度
cell71.setFixedHeight(25);
cell72.setFixedHeight(25);
cell73.setFixedHeight(25);
cell74.setFixedHeight(25);
cell75.setFixedHeight(25);
cell76.setFixedHeight(25);
//水平居中
cell71.setHorizontalAlignment(Element.ALIGN_CENTER);
cell72.setHorizontalAlignment(Element.ALIGN_CENTER);
cell73.setHorizontalAlignment(Element.ALIGN_CENTER);
cell74.setHorizontalAlignment(Element.ALIGN_CENTER);
cell75.setHorizontalAlignment(Element.ALIGN_CENTER);
cell76.setHorizontalAlignment(Element.ALIGN_CENTER);
//垂直居中
cell71.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell72.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell73.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell74.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell75.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell76.setVerticalAlignment(Element.ALIGN_MIDDLE);
//边框颜色
cell71.setBorderColor(lightGrey);
cell72.setBorderColor(lightGrey);
cell73.setBorderColor(lightGrey);
cell74.setBorderColor(lightGrey);
cell75.setBorderColor(lightGrey);
cell76.setBorderColor(lightGrey);
//去掉左右边框
cell71.disableBorderSide(8);
cell72.disableBorderSide(4);
cell72.disableBorderSide(8);
cell73.disableBorderSide(4);
cell73.disableBorderSide(8);
cell74.disableBorderSide(4);
cell74.disableBorderSide(8);
cell75.disableBorderSide(4);
cell75.disableBorderSide(8);
cell76.disableBorderSide(4);
table7.addCell(cell71);
table7.addCell(cell72);
table7.addCell(cell73);
table7.addCell(cell74);
table7.addCell(cell75);
table7.addCell(cell76);
document.add(table7); //table8
PdfPTable table8 = new PdfPTable(6);
int width8[] = {20,18,13,20,14,15};
table8.setWidths(width8);
PdfPCell cell81 = new PdfPCell(new Paragraph("差旅报销",FontChinese12));
PdfPCell cell82 = new PdfPCell(new Paragraph("2011-11-11",FontChinese12));
PdfPCell cell83 = new PdfPCell(new Paragraph("XXX",FontChinese12));
PdfPCell cell84 = new PdfPCell(new Paragraph("XXX",FontChinese12));
PdfPCell cell85 = new PdfPCell(new Paragraph("XXX",FontChinese12));
PdfPCell cell86 = new PdfPCell(new Paragraph("XXX",FontChinese12));
//表格高度
cell81.setFixedHeight(25);
cell82.setFixedHeight(25);
cell83.setFixedHeight(25);
cell84.setFixedHeight(25);
cell85.setFixedHeight(25);
cell86.setFixedHeight(25);
//水平居中
cell81.setHorizontalAlignment(Element.ALIGN_CENTER);
cell82.setHorizontalAlignment(Element.ALIGN_CENTER);
cell83.setHorizontalAlignment(Element.ALIGN_CENTER);
cell84.setHorizontalAlignment(Element.ALIGN_CENTER);
cell85.setHorizontalAlignment(Element.ALIGN_CENTER);
cell86.setHorizontalAlignment(Element.ALIGN_CENTER);
//垂直居中
cell81.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell82.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell83.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell84.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell85.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell86.setVerticalAlignment(Element.ALIGN_MIDDLE);
//边框颜色
cell81.setBorderColor(lightGrey);
cell82.setBorderColor(lightGrey);
cell83.setBorderColor(lightGrey);
cell84.setBorderColor(lightGrey);
cell85.setBorderColor(lightGrey);
cell86.setBorderColor(lightGrey);
//去掉左右边框
cell81.disableBorderSide(8);
cell82.disableBorderSide(4);
cell82.disableBorderSide(8);
cell83.disableBorderSide(4);
cell83.disableBorderSide(8);
cell84.disableBorderSide(4);
cell84.disableBorderSide(8);
cell85.disableBorderSide(4);
cell85.disableBorderSide(8);
cell86.disableBorderSide(4);
table8.addCell(cell81);
table8.addCell(cell82);
table8.addCell(cell83);
table8.addCell(cell84);
table8.addCell(cell85);
table8.addCell(cell86);
document.add(table8);
//加入空行
Paragraph blankRow5 = new Paragraph(18f, " ", FontChinese18);
document.add(blankRow5); //table9
PdfPTable table9 = new PdfPTable(3);
int width9[] = {30,50,20};
table9.setWidths(width9);
PdfPCell cell91 = new PdfPCell(new Paragraph("",FontChinese12));
PdfPCell cell92 = new PdfPCell(new Paragraph("收到的报销金额",FontChinese12));
PdfPCell cell93 = new PdfPCell(new Paragraph("1000",FontChinese24));
cell92.setHorizontalAlignment(Element.ALIGN_RIGHT);
cell92.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell93.setHorizontalAlignment(Element.ALIGN_LEFT);
cell93.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell91.setBorder(0);
cell92.setBorder(0);
cell93.setBorder(0);
table9.addCell(cell91);
table9.addCell(cell92);
table9.addCell(cell93);
document.add(table9); document.close(); } catch (Exception ex)
{
ex.printStackTrace();
}
}
}
使用itext生成pdf的,各种布局的更多相关文章
- 在linux环境下使用itext生成pdf
转载请注明出处 https://www.cnblogs.com/majianming/p/9537173.html 项目中需要对订单生成pdf文件,在不断的尝试之后,终于生成了比较满意的pdf文档. ...
- Java Itext 生成PDF文件
利用Java Itext生成PDF文件并导出,实现效果如下: PDFUtil.java package com.jeeplus.modules.order.util; import java.io.O ...
- 用itext生成PDF报错:Font 'STSong-Light1' with 'UniGB-UCS2-H' is not recognized.
用itext生成PDF报错,加上try catch捕获到异常是 BaseFont bFont = BaseFont.createFont("STSong-Light1", &quo ...
- 【PDF】java使用Itext生成pdf文档--详解
[API接口] 一.Itext简介 API地址:javadoc/index.html:如 D:/MyJAR/原JAR包/PDF/itext-5.5.3/itextpdf-5.5.3-javadoc/ ...
- Itext生成pdf文件
来源:https://my.oschina.net/lujianing/blog/894365 1.背景 在某些业务场景中,需要提供相关的电子凭证,比如网银/支付宝中转账的电子回单,签约的电子合同等. ...
- 【Java】使用iText生成PDF文件
iText介绍 iText是著名的开放源码的站点sourceforge一个项目,是用于生成PDF文档的一个java类库.通过iText不仅可以生成PDF或rtf的文档,而且可以将XML.Html文件转 ...
- java使用iText生成pdf表格
转载地址:http://www.open-open.com/code/view/1424011530749 首先需要你自己下载itext相关的jar包并添加引用,或者在maven中添加如下引用配置: ...
- 利用itext生成pdf的简单例子
一.itext简介 iText是著名的开放源码的站点sourceforge一个项目,是用于生成PDF文档的一个java类库.通过iText不仅可以生成PDF或rtf的文档,而且可以将XML.Html文 ...
- itext 生成pdf文件添加页眉页脚
原文来自:https://www.cnblogs.com/joann/p/5511905.html 我只是记录所有jar版本,由于版本冲突及不兼容很让人头疼的,一共需要5个jar, 其中itextpd ...
- iText生成PDF 格式报表
1.导包 <dependency> <groupId>com.lowagie</groupId> <artifactId>itext</artif ...
随机推荐
- 【转】java面试题
http://blog.csdn.net/jackfrued/article/details/44921941 Java面试题转
- rabbitmq (四) 路由
上文讲的是广播类型fanout 本章讲 direct和topic. 当使用广播类型fanout的时候: routingKey字段不起作用. direct:精确匹配 routingKey:匹配一个单词, ...
- 什么是BOM?,什么是DOM? BOM跟DOM之间的关系
什么是BOM? BOM是browser object model的缩写,简称浏览器对象模型.是用来获取或设置浏览器的属性.行为,例如:新建窗口.获取屏幕分辨率.浏览器版本号等. 比如 alert(); ...
- 权限管理demo-Http请求前后监听工具
工具作用: 1. 输出每次请求的参数 2. 接口的请求时间 package com.mmall.common; import com.mmall.util.JsonMapper; import lom ...
- python3:实现字符串的全排列(有重复字符)
抛出问题 求任意一个字符串的全排列组合,例如a='123',输出 123,132,213,231,312,321. 解决方案 #字符串任意两个位置字符交换 def str_replace(str, x ...
- iOS开发中didSelectRowAtIndexPath tap事件响应延迟
为UITableViewCell添加tapped事件,代码如下: class VideoViewController: UIViewController , UITableViewDataSource ...
- JDK1.7 ConcurrentHashMap--解决高并发下的HashMap使用问题
高并发下也可以使用HashTable .Collections.synchronizedMap因为他们是线程安全的,但是却牺牲了性能,无论是读操作.写操作都是给整个集合加锁,导致同一时间内其他操作均为 ...
- js跳转新窗口
语法:window.open(url,[target]); eg: window.open("index.html"); window.open("index.html& ...
- PAT甲级
https://www.cnblogs.com/jlyg/p/7525244.html 哇咔咔,瞧人家都刷完甲级130道题了,哼,有什么了不起的,考前刷完,再刷的比你多,哼,照样吊打. 沙耶加油! 一 ...
- Javascript中 toFixed
javascript中toFixed使用的是银行家舍入规则. 银行家舍入:所谓银行家舍入法,其实质是一种四舍六入五取偶(又称四舍六入五留双)法. 简单来说就是:四舍六入五考虑,五后非零就进一,五后为零 ...