Java生成pdf

本文详细介绍了如何使用Java和Apache PDFBox库生成付款通知单的PDF文件,包括设置页面布局、添加公司信息、制作表格和输出内容。

public void onClick$previewaccount_btn(){

Rectangle rect = new Rectangle(800,600); 

Document document = new Document(rect);

//文件路径

String path = System.getProperty("cdncharge_pdf" , "D:/apache-tomcat-6.0.32/webapps/cdncharge/cdndomaingrouppdf/");

String name = "付款通知单.pdf";

FileOutputStream pdfFile = null; 

try {

pdfFile = new FileOutputStream(new File(path+name)); 

PdfWriter.getInstance(document, pdfFile);

BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);

//设置页脚标题

com.lowagie.text.Font FontChinese = new com.lowagie.text.Font(bfChinese, 8, com.lowagie.text.Font.NORMAL);

HeaderFooter footer = new HeaderFooter(new Paragraph("Title", FontChinese), false);

footer.setBorder(Rectangle.NO_BORDER);

   footer.setAlignment(Element.ALIGN_CENTER);

   //设置标题

            Font fonthead = new Font(bfChinese, 17, Font.BOLD);

HeaderFooter head = new HeaderFooter(new Paragraph("付款通知单", fonthead), false);

head.setAlignment(Element.ALIGN_CENTER);

head.setBorder(Rectangle.NO_BORDER);

   document.setHeader(head);

   document.setFooter( footer);

//此行代码的为止尤为重要

document.open();

//写入内容

getPayment(document , FontChinese);

document.close();

//Executions.getCurrent().sendRedirect("file:///"+path+name, "_blank");

}catch (Exception e) {

e.printStackTrace();

}finally{

if(pdfFile != null){

try {

pdfFile.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

public void getPayment(Document document,com.lowagie.text.Font FontChinese) throws Exception{

BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);

        Font fonthead = new Font(bfChinese, 14, Font.BOLD);

Paragraph customer = new Paragraph("上海XXX公司:",fonthead);

document.add(customer);

        Font font1 = new Font(bfChinese, 12, Font.NORMAL);

Paragraph text1 = new Paragraph("感谢贵公司对我公司的信任和支持!",font1);

text1.setIndentationLeft(20f);

document.add(text1);

Paragraph text2 = new Paragraph("根据贵公司..........",font1);

text2.setIndentationLeft(20f);

document.add(text2);

Chunk simple = new Chunk("");

document.add(simple);

        Font tablehead = new Font(bfChinese, 12, Font.BOLD);

//制作表格

PdfPTable table = new PdfPTable(4);

PdfPCell cellhead1 = new PdfPCell(new Paragraph("时间",tablehead));

PdfPCell cellhead2 = new PdfPCell(new Paragraph("项目",tablehead));

PdfPCell cellhead3 = new PdfPCell(new Paragraph("金额(元)",tablehead));

PdfPCell cellhead4 = new PdfPCell(new Paragraph("备注",tablehead));

table.addCell(cellhead1);

table.addCell(cellhead2);

table.addCell(cellhead3);

table.addCell(cellhead4);

PdfPCell cell1 = new PdfPCell(new Paragraph("2015.1.1-2015.12.31",font1));

PdfPCell cell2 = new PdfPCell(new Paragraph("555555",font1));

PdfPCell cell3 = new PdfPCell(new Paragraph("4000",font1));

PdfPCell cell4 = new PdfPCell(new Paragraph("55555",font1));

table.addCell(cell1);

table.addCell(cell2);

table.addCell(cell3);

table.addCell(cell4);

PdfPCell cell11 = new PdfPCell(new Paragraph("2015.1.1-2015.12.31",font1));

PdfPCell cell12 = new PdfPCell(new Paragraph("66666",font1));

PdfPCell cell13 = new PdfPCell(new Paragraph("6000",font1));

PdfPCell cell14 = new PdfPCell(new Paragraph("666666底",font1));

table.addCell(cell11);

table.addCell(cell12);

table.addCell(cell13);

table.addCell(cell14);

document.add(table);

Paragraph text3 = new Paragraph("合计:10000元 ",tablehead);

text3.setAlignment(Element.ALIGN_RIGHT);

text3.setIndentationRight(60f);

document.add(text3);

//添加块

Chunk chunk1 = new Chunk("根据以上明细,......................",font1); 

Chunk chunk2 = new Chunk("壹万元",tablehead); 

Chunk chunk3 = new Chunk("(10,000元),请贵公司将此笔费以支票或汇款方式最晚于本月25日前支付于我公司。相关汇款信息如下:",font1); 

//添加短语,将三个块拼接起来

Phrase phrase = new Phrase(); 

phrase.add(chunk1);

phrase.add(chunk2);

phrase.add(chunk3);

document.add(phrase);

Paragraph text4 = new Paragraph("上海.............. ",tablehead);

Paragraph text5 = new Paragraph("帐号............. ",tablehead);

Paragraph text6 = new Paragraph("开户行.................",tablehead);

text4.setIndentationLeft(100f);

text5.setIndentationLeft(100f);

text6.setIndentationLeft(100f);

document.add(text4);

document.add(text5);

document.add(text6);

Paragraph text7 = new Paragraph("如对此付款通知单有任何问题,请与........。",font1);

text7.setIndentationLeft(20f);

document.add(text7);

Paragraph text8 = new Paragraph("电话:021-61805555-1122 Email:yuanchunchun@dnion.com",font1);

text8.setIndentationLeft(20f);

document.add(text8);

Paragraph text9 = new Paragraph("若贵司在收到本付.............",font1);

text9.setIndentationLeft(20f);

document.add(text9);

Paragraph text10 = new Paragraph("敬请给予理解和合作!我公司将始终如一地竭诚为贵公司提供全面、高效的服务",font1);

text10.setIndentationLeft(20f);

document.add(text10);

Paragraph text11 = new Paragraph("顺祝",font1);

text11.setIndentationLeft(20f);

document.add(text11);

Paragraph text12 = new Paragraph("商祺!",font1);

text12.setIndentationLeft(60f);

document.add(text12);

Paragraph text13 = new Paragraph("上海..................",font1);

text13.setAlignment(Element.ALIGN_RIGHT);

document.add(text13);

Paragraph text14 = new Paragraph("业务管理中心",font1);

text14.setAlignment(Element.ALIGN_RIGHT);

document.add(text14);

Paragraph text15 = new Paragraph("XXXX年XX月XX日",font1);

text15.setAlignment(Element.ALIGN_RIGHT);

document.add(text15);

}

转载于:https://www.cnblogs.com/kisstear/p/4789116.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值