itext實現pdf匯出
阿新 • • 發佈:2019-01-27
jar
iText-2.1.4.jar
iTextAsian.jar 中文必備,否則不顯示
程式設計師的一貫風格不多說,上程式碼:
public class OrderData2PDFServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private WebApplicationContext wac;
public OrderData2PDFServlet() {
super();
}
@Override
public void init() throws ServletException {
wac =WebApplicationContextUtils.getRequiredWebApplicationContext(this.getServletContext());
}
/*
* <p>生成批PDF document 並支援下載</p>
*
* @author Z.J
*
* @date 2016-8-2
*
* @param request
*
* @param response
*
* @throws ServletException
*
* @throws IOException
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// No.1 處理分頁查詢引數
Paginator page = new Paginator();
CommonUtils.setParametersForPage(page, request);
int columnCount=6;
float[] columnRatio=new float[] { 2, 2, 1, 1, 2, 2 };
//pdf表格匯出的title
String[] titleArr={"Paypal交易單號","客戶姓名","交易日期","總金額","國家","回訪次數"};
String header="訂單資訊";
OrderService orderService=(OrderService)wac.getBean("orderService");
//獲取訂單資訊
List<Order> resultList =orderService.getOrderListByPage(page);
//建立文件物件,並設定大小,本案例設為A4紙尺寸
Document document = new Document(PageSize.A4.rotate());
try{
// 設定響應輸出資料的字符集
response.setContentType("application/x-msdownload;charset=UTF-8");
// 啟用瀏覽器檔案下載框,提示使用者儲存
response.setHeader("Content-Disposition", "attachment;filename=order_info.pdf");
// open output stream
PdfWriter.getInstance(document, response.getOutputStream());
// open PDF document
document.open();
//字型處理,中文需要用到
FontSelector selector = new FontSelector();
//設定英文顯示字型TIMES_ROMAN及大小
selector.addFont(FontFactory.getFont(FontFactory.TIMES_ROMAN, 10));
//設定中文顯示字型STSong-Light
Font cf1 = FontFactory.getFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED, 10);
//設定標題顯示字型STSong-Light及大小
Font headerFont = FontFactory.getFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED, 18);
selector.addFont(cf1);
//建立pdf表格並填充表格內容
if (resultList != null && resultList.size() > 0) {
PdfPTable table = new PdfPTable(columnRatio);
PdfPCell topHeaderCell = new PdfPCell(new Phrase(header,headerFont));
topHeaderCell.setColspan(columnCount);
topHeaderCell.setMinimumHeight(35);
topHeaderCell.setBackgroundColor(new Color(189, 215, 238));
topHeaderCell.setHorizontalAlignment(Element.ALIGN_CENTER);
topHeaderCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
table.addCell(topHeaderCell);
PdfPCell defaultCell = table.getDefaultCell();
defaultCell.setHorizontalAlignment(Element.ALIGN_CENTER);
defaultCell.setMinimumHeight(30);
defaultCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
PdfPTableEvent event = new AlternatingBackground();
for (int i = 0; i < 2; i++) {
for(String s:titleArr){
table.addCell(selector.process(s));
}
}
table.setHeaderRows(3);
table.setFooterRows(1);
defaultCell.setMinimumHeight(25);
for (Order o : resultList) {
table.addCell(selector.process(CommonUtils.processNullAndEmpty(o.getPaypalNumber())));
CustomerModel cu=o.getCustomer();
if(cu!=null&&CommonUtils.isNotNullAndEmpty(cu.getCustomerName())){
table.addCell(selector.process(CommonUtils.processNullAndEmpty(cu.getCustomerName())));
}else{
table.addCell(selector.process(""));
}
table.addCell(selector.process(CommonUtils.processNullAndEmpty(o.getTradeDate())));
table.addCell(selector.process(CommonUtils.processNullAndEmpty(o.getOrderTotal())));
if(cu!=null&&CommonUtils.isNotNullAndEmpty(cu.getCustomerName())){
Country ct=cu.getCountry();
if(ct!=null&&CommonUtils.isNotNullAndEmpty(ct.getCountryName())){
table.addCell(selector.process(ct.getCountryName()));
}else{
table.addCell(selector.process(""));
}
}else{
table.addCell(selector.process(""));
}
table.addCell(selector.process(CommonUtils.processNullAndEmpty(o.getCallbackTimes())));
}
table.setTableEvent(event);
document.add(table);
}else{
Font f1 = FontFactory.getFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED, 28);
f1.setColor(new GrayColor(0.75f));
PdfPTable table = new PdfPTable(1);
table.setWidthPercentage(100);
PdfPCell cell = new PdfPCell(new Phrase("暫無資料",f1));
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setBorder(0);
cell.setMinimumHeight(30);
cell.setColspan(columnCount);
table.addCell(cell);
document.add(table);
}
}catch(Exception e){
e.printStackTrace();
}finally{
document.close();
}
}
/*
* <p></p>
*
* @author Z.J
*
* @date 2016-8-2
*
* @param request
*
* @param response
*
* @throws ServletException
*
* @throws IOException
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
圖簡單的朋友們,可以直接把本程式碼給一下就能用,要是想深究的話,請仔細閱讀itext的使用API。本案例使用FontSelector實現了中英文字型混合顯示,程式會自動根據當前要顯示的文字自行切換字型。
jar包和itext使用API的下載地址如下:http://download.csdn.net/detail/chrisjingu/9618083