推荐用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;
}