iText生成PDF报表文件
来源:互联网 发布:手机电子相册制作软件 编辑:程序博客网 时间:2024/04/30 06:56
推荐用iTextpdf包,lowagie已经被iTextpdf收购
下面链接中的table可能不能用了(有些下载jar包没有),用PdfPTable代替。
0、准备工作
//导入包并import
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.PdfPTable;
//建立PDF文档,做好文件写入和关闭工作
Document doc=new Document();
FileOutputStream fos;
try {
Order order = OrdersShowActivity.Orders.get((Integer) v.getTag());
fos = new FileOutputStream(new File(FilePath.ROOTDIRECTORY+customer.getCode() +"/"+order.getNumber()+".pdf"));//输出路径
PdfWriter.getInstance(doc, fos);
doc.open ();
//插入操作
doc.close();
fos.flush();
fos.close();
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
1、插入文本
String result = " 客户信息"+"\r\n\r\n 公司名称:";
doc.add(new Paragraph(result, setChineseFont()));
//中文
public static Font setChineseFont() {BaseFont bf = null;
Font fontChinese = null;
try {
bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
fontChinese = new Font(bf, 12, Font.NORMAL);
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return fontChinese;
}
2、插入图片
Image jpeg = Image.getInstance("file:zz.png");
jpeg.scaleAbsolute(80, 100);
doc.add(jpeg);
3、插入表格
Paragraph p =new Paragraph("\r\n客户信息", setChineseFont());
p.setAlignment(Element.ALIGN_CENTER);
doc.add(new Paragraph(p));
PdfPTable table = new PdfPTable(5);
int width[] = {20,20,20,20,20};//设置每列宽度比例
table.setWidths(width);
table.addCell(getTextCell("公司名称"));
table.addCell(getTextCell("客户名字"));
table.addCell(getTextCell("送货地址"));
table.addCell(getTextCell("联系电话"));
table.addCell(getTextCell("客户编号"));
table.addCell(getTextCell(customer.getCompany()));
table.addCell(getTextCell(customer.getName()));
table.addCell(getTextCell(customer.getAddress()));
table.addCell(getTextCell(customer.getPhone()));
table.addCell(getTextCell(customer.getCode()));
doc.add(table);
p =new Paragraph("\r\n\r\n订单信息", setChineseFont());
p.setAlignment(Element.ALIGN_CENTER);
doc.add(new Paragraph(p));
table = new PdfPTable(5);
table.setWidths(width);
table.addCell(getTextCell("参考图"));
table.addCell(getTextCell("位置"));
table.addCell(getTextCell("名字"));
table.addCell(getTextCell("数量"));
table.addCell(getTextCell("价格"));
for (int i = 0; i < OrdersShowActivity.item_num; i++) {
for (int j = 0; j < OrdersShowActivity.all_pic_list.get(i).size(); j++) {
Image jpeg = Image.getInstance("file:"+OrdersShowActivity.all_pic_list.get(i).get(j));
jpeg.scaleAbsolute(40,50);
table.addCell(new PdfPCell(jpeg));
table.addCell(getTextCell(OrdersShowActivity.all_position_list.get(i)));
table.addCell(getTextCell(OrdersShowActivity.all_name_list.get(i).get(j)));
table.addCell(getTextCell(OrdersShowActivity.all_num_list.get(i).get(j)+""));
table.addCell(getTextCell(OrdersShowActivity.all_price_list.get(i).get(j)+""));
}
}
doc.add(table);
p =new Paragraph("\r\n\r\n总计:"+o.getFirst_price()+" 加税:"+o.getTax()+" 优惠:"+o.getDiscount()+" 实付金额:"+o.getLast_price(), setChineseFont());
p.setAlignment(Element.ALIGN_RIGHT);
doc.add(new Paragraph(p));
//自己写的方法
private PdfPCell getTextCell(String str){
PdfPCell cell = new PdfPCell();
cell.setVerticalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
Paragraph p =new Paragraph(str, setChineseFont());
p.setAlignment(Element.ALIGN_CENTER);
cell.addElement(p);
return cell;
}
- iText生成PDF报表文件
- Struts+iText生成Pdf报表
- Struts+iText生成Pdf报表
- Struts+iText生成Pdf报表
- 利用iText生成PDF报表
- Struts+iText生成Pdf报表
- 利用iText生成pdf报表
- Itext生成PDF文件
- 使用ITEXT与JAVA生成PDF报表
- (例)Java生成PDF报表 iText
- Java使用itext生成PDF报表
- 用itext生成pdf报表上篇
- 用iText生成PDF文件
- Itext 生成 pdf文件使用方法:
- Freemarker+IText生成pdf文件
- 使用iText生成pdf文件
- 关于iText生成PDF文件
- 使用iText生成PDF文件
- sql 字符串替换
- C#如何操控FTP
- 第四章.使用DML语句更改数据.总结
- 递归函数 & 函数指针 ...
- C/C++内存泄漏及检测
- iText生成PDF报表文件
- scroll事件不触发?
- win10下为ionic搭载开发环境
- windows设置通过NFS连接到Linux共享数据
- 远程桌面设置
- OpenSSH相关漏洞解决
- 【c/c++】转换构造函数
- LOG4NET 使用方法
- ios app maximum memory budget