打印pdf itext 的多个pdf合并并删除旧的pdf文件

来源:互联网 发布:bt网络用语是什么意思 编辑:程序博客网 时间:2024/06/09 21:38

有时候我们打印pdf时需要生成多个pdf文件,最后合成一个新的pdf来打印,我们又嫌这么多pdf占内存所以合并后把之前的pdf删除掉。

/**
* 打印出库单(导出pdf文件)

* @throws IOException
* @method: printChuku() 
* @TODO: void
*/
public void printChuku() {
try {
//刊的状态 0常用,1不常用
Integer pub_state = getParaToInt("pub_state", 0);
String start = getPara("start");
String end = getPara("end");
//查询每期的每版的某个客户数量之和
String sql = "SELECT SUM(o.total_number) number,SUM(o.sub_number)  suber,"
+ "SUM(o.free_number) freenum, o.*,c.*,ou.*,u.user_xingming FROM ordertable o LEFT JOIN customers c ON o.cus_id=c.cus_id "
+ "LEFT JOIN USER u ON o.user_id=u.user_id left join outbound ou on o.out_id=ou.out_id "
+ " where o.ord_status=2 and o.out_id IS NOT NULL";
//查询业务员和客户
String sql1 = " SELECT o.*,c.*,ou.*,u.user_xingming FROM ordertable o LEFT JOIN customers c ON o.cus_id=c.cus_id "
+ "LEFT JOIN USER u ON o.user_id=u.user_id left join outbound ou on o.out_id=ou.out_id  where  o.ord_status=2  and o.out_id IS NOT NULL";
if (start != null && start != "" && end != null && end != "") {
String w = " and ou.out_date >='" + start + "' and ou.out_date<='" + end + "' ";
sql = sql + w;
sql1 = sql1 + w;
}
String osql = sql + " and o.pub_id in (select pub_id from publish where pub_state=" + pub_state + ")" + " GROUP BY ou.out_code";
sql = sql + " and o.pub_id in (select pub_id from publish where pub_state=" + pub_state + ")"
+ " GROUP BY ou.out_code,o.iss,o.edi_id,c.cus_name ORDER BY ou.out_id DESC";
sql1 = sql1 + " and o.pub_id in (select pub_id from publish where pub_state=" + pub_state + ") "
+ "GROUP BY ou.out_code,c.cus_name,o.iss ORDER BY ou.out_id DESC";
List<Ordertable> oList = Ordertable.dao.find(sql);
List<Ordertable> oList1 = Ordertable.dao.find(sql1);
List<Outbound> chuku = Outbound.dao.find(osql);
//获取刊等信息
List<Publish> plist = Publish.dao.find("select * from publish where pub_state=" + pub_state);
Font font = null;
Font cellFont = null;
PdfPTable table = null;
PdfPHeaderCell header = null;
PdfPCell cell = null;
String[] pdf = new String[chuku.size()];
for (int k = 0; k < chuku.size(); k++) {
pdf[k] = PathKit.getWebRootPath() + "/temp/OutboundOrder" + k + ".pdf";
//序号
Ordertable out = oList1.get(k);
//设置pdf每页的页眉和页脚
Date outDate = out.get("out_date");
//1.建立一个document实例对象
Rectangle rec = new Rectangle(240 * 2.5f, 140 * 2.5f);
Document document = new Document(rec, 10, 10, 50, 50);//纸张大小,marginleft,marginright,margintop,marginbottom
FileOutputStream outputStream = new FileOutputStream(pdf[k]);
//2.建立一个书写器(writer)与document对象关联,通过书写器可以将文档写入磁盘
PdfWriter writer = PdfWriter.getInstance(document, outputStream);
writer.setPageEvent(new HeadFootInfoPdfPageEvent(出库单", outDate.toString(), "出", out.getStr("out_code")));
//设置字体
BaseFont baseFont = BaseFont.createFont("C:/WINDOWS/Fonts/SIMSUN.TTC,1", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
font = new Font(baseFont, 10, Font.NORMAL);
cellFont = new Font(baseFont, 10);
//3.打开文档
document.open();
// 建立一个pdf表格
List<Edition> eList = Edition.dao.find("select * from edition where pub_id in ( select pub_id from publish where pub_state="
+ pub_state + ")");
//新建数组给table中的每个td设置宽度
float[] chang = new float[eList.size() * 2 + 6];
chang[0] = 15f;
chang[1] = 80f;
chang[2] = 30f;
chang[3] = 30f;
chang[4] = 35f;
chang[5] = 5f;
for (int i = 0; i < eList.size(); i++) {
chang[i + 5] = 20f;
}
for (int i = 0; i < eList.size(); i++) {
chang[eList.size() + i + 5] = 20f;
}
table = new PdfPTable(chang);
table.setSpacingBefore(20f);
// 设置表格宽度为100%  
table.setWidthPercentage(100);
table.setHeaderRows(2);//每页显示表头


//品名
cell = new PdfPCell(new Phrase("序号", cellFont));
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setRowspan(2);
cell.addHeader(header);
table.addCell(cell);
//客户名称(邮编)
cell = new PdfPCell(new Phrase("客户名称(邮编)", cellFont));
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setRowspan(2);
cell.addHeader(header);
table.addCell(cell);
//业务员
cell = new PdfPCell(new Phrase("业务员", cellFont));
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setRowspan(2);
cell.addHeader(header);
table.addCell(cell);
//收件人
cell = new PdfPCell(new Phrase("收件人", cellFont));
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setRowspan(2);
cell.addHeader(header);
table.addCell(cell);
//联系电话
cell = new PdfPCell(new Phrase("联系电话", cellFont));
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setRowspan(2);
cell.addHeader(header);
table.addCell(cell);
//期数
cell = new PdfPCell(new Phrase("期数", cellFont));
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setRowspan(2);
cell.addHeader(header);
table.addCell(cell);


//获取刊等信息
for (Publish p : plist) {
int n = Db.queryLong("select count(*) from edition  where pub_id=" + p.getInt("pub_id")).intValue();
String pName = p.getStr("pub_name");
//杂志
cell = new PdfPCell(new Phrase(pName, cellFont));
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setColspan(n * 2);
cell.addHeader(header);
table.addCell(cell);
}


//获取刊等信息
for (int j = 0; j < plist.size(); j++) {
Publish p = plist.get(j);
List<Edition> editList = Edition.dao.find("select edi_name from edition  where   pub_id=" + p.getInt("pub_id"));
for (Edition edition : editList) {
String eName = edition.getStr("edi_name");
cell = new PdfPCell(new Phrase(eName + "\r订数", cellFont));
cell.addHeader(header);
table.addCell(cell);
cell = new PdfPCell(new Phrase(eName + "\r赠数", cellFont));
cell.addHeader(header);
table.addCell(cell);
}
}
int hao = 0;
for (int s = 0; s < oList1.size(); s++) {
Ordertable ot1 = oList1.get(s);
if (out.getInt("out_id") == ot1.getInt("out_id")) {
++hao;
//序号
cell = new PdfPCell(new Phrase("" + hao, font));
table.addCell(cell);
//客户名称
cell = new PdfPCell(new Phrase(ot1.getStr("cus_name") + "(" + ot1.getStr("cus_code") + ")", font));
//cell.setColspan(8);
table.addCell(cell);
//业务员
cell = new PdfPCell(new Phrase(ot1.getStr("user_xingming"), font));
table.addCell(cell);
//收件人
cell = new PdfPCell(new Phrase(ot1.getStr("cus_linkman"), font));
table.addCell(cell);
//联系电话
cell = new PdfPCell(new Phrase(ot1.getStr("cus_phone"), font));
table.addCell(cell);
//期数
cell = new PdfPCell(new Phrase(ot1.getStr("iss"), font));
table.addCell(cell);
for (int n = 0; n < plist.size(); n++) {
Publish p = plist.get(n);
List<Edition> editList = Edition.dao.find("select edi_id from edition  where pub_id=" + p.getInt("pub_id"));
for (int j = 0; j < editList.size(); j++) {
Edition edition = editList.get(j);
PdfPCell cell1 = new PdfPCell(new Phrase("", cellFont));
PdfPCell cell2 = new PdfPCell(new Phrase("", cellFont));
int flag = 0;
for (int i = 0; i < oList.size(); i++) {
Ordertable ot = oList.get(i);
if (ot1.getInt("cus_id") == ot.getInt("cus_id") && ot1.getInt("user_id") == ot.getInt("user_id")
&& ot1.getStr("ord_year").equals(ot.getStr("ord_year")) && ot1.getStr("iss").equals(ot.getStr("iss"))
&& ot.getInt("edi_id") == edition.getInt("edi_id") && ot.getInt("pub_id") == p.getInt("pub_id")) {
String d = ot.getBigDecimal("suber").toString();
//订数和
cell1 = new PdfPCell(new Phrase(d.equals("0") ? "" : d, cellFont));
cell1.setPadding(0);
table.addCell(cell1);
String z = ot.getBigDecimal("freenum").toString();
//赠数和
cell2 = new PdfPCell(new Phrase(z.equals("0") ? "" : z, cellFont));
cell2.setPadding(0);
table.addCell(cell2);
flag = 1;
}


}
if (flag == 0) {
//订数和
cell1 = new PdfPCell(new Phrase("", cellFont));
cell1.setPadding(0);
table.addCell(cell1);


//赠数和
cell2 = new PdfPCell(new Phrase("", cellFont));
cell2.setPadding(0);
table.addCell(cell2);
}
}
}
}
}
//5.关闭文档
document.add(table);
document.close();
writer.close();
outputStream.close();
}
mergePdfFiles(pdf, PathKit.getWebRootPath() + "/temp/OutboundOrder.pdf");
redirect(getRequest().getScheme() + "://" + getRequest().getServerName() + ":" + getRequest().getServerPort()
+ getRequest().getContextPath() + "/temp/OutboundOrder.pdf");


} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

我们在写文件或者读文件的时候切记关闭流。

/**
* 合并多个pdf

* @param files
*            pdf数组
* @param savepath保存新的pdf的路径
*            LMM
*/
private void mergePdfFiles(String[] files, String savepath) {
try {

//合并的pdf的纸张大小
Rectangle rec = new Rectangle(240 * 2.5f, 140 * 2.5f);
Document document = new Document(rec);
FileOutputStream out = new FileOutputStream(savepath);
PdfCopy copy = new PdfCopy(document, out);
document.open();
for (int i = 0; i < files.length; i++) {
PdfReader reader = new PdfReader(files[i]);
int n = reader.getNumberOfPages();


for (int j = 1; j <= n; j++) {
document.newPage();
PdfImportedPage page = copy.getImportedPage(reader, j);
copy.addPage(page);
}
reader.close();
}
document.close();
copy.close();
out.close();

//把这些流关闭后删除之前的pdf
for (int i = 0; i < files.length; i++) {
File file = new File(files[i]);
file.delete();
}


} catch (IOException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
}
}

0 0
原创粉丝点击