springmvc使用itext生存pdf文件


最近做項目,需要生成PDF格式的日報,由於之前沒有做過,研究了好幾天,中途遇到了一些問題,現在把它記錄先來,方便以后查看.

先貼代碼:

Document document = new Document(PageSize.A4); 
PdfWriter.getInstance(document, new FileOutputStream(fileName));
BaseFont bfChinese = BaseFont.createFont("C:/WINDOWS/Fonts/SIMYOU.TTF", BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);
// 標題字體        
Font f30 = new Font(bfChinese, 30, Font.NORMAL, Color.BLACK);
// 正文字體        
Font f8 = new Font(bfChinese, 15, Font.NORMAL, Color.BLACK);//小標題字體                
Font f9 = new Font(bfChinese, 15, Font.NORMAL, Color.RED);//填充內容字體        
document.open();                
Paragraph title = new Paragraph("日報", f30);        
title.setAlignment(1); 
// 標題        
document.add(title);        
// 換行        
document.add(new Chunk("\n"));        
// 添加table實例,共有4列        
PdfPTable table = new PdfPTable(4);        
table.setWidthPercentage(100);        
table.setHorizontalAlignment(PdfPTable.ALIGN_CENTER);        
PdfPCell cell;
  // 表格標題和數據
     cell = new PdfPCell(new Paragraph("工程名稱", f8));
     cell.setMinimumHeight(25);
cell.setHorizontalAlignment(1);
     table.addCell(cell);
     cell = new PdfPCell(new Paragraph("工程一", f9)); 
     cell.setColspan(3);
     cell.setMinimumHeight(25);
cell.setHorizontalAlignment(1);
     table.addCell(cell);
     cell = new PdfPCell(new Paragraph("信息化監管單位", f8)); 
     cell.setMinimumHeight(25);
cell.setHorizontalAlignment(1);
     table.addCell(cell);
cell = new PdfPCell(new Paragraph("人民群眾", f9));
     cell.setMinimumHeight(25);
     cell.setHorizontalAlignment(1);
     table.addCell(cell);
     cell = new PdfPCell(new Paragraph("監管日期", f8));
     cell.setHorizontalAlignment(1);
     table.addCell(cell);
     cell = new PdfPCell(new Paragraph("2016-05-12", f9));
     cell.setMinimumHeight(25);
     cell.setHorizontalAlignment(1);

     table.addCell(cell);

  document.add(table);

生成的樣式如下,以上我想一般都不會有什么問題.

但是在設置子標題的時候,出現了合並單元格的問題.就是itex雖然有單元格合並的方法,但是行合並和列合並不能同時使用,否則生成的樣式就會錯位,但是有時候又必須這么做,

那么我自己的解決方式通過創建多個PdfPTable來實現.比如下圖

這里我共有6列,但是在其他地方其實不是6列,可能是8列,如果整個文章只用一個table的話那么就需要列合並,如果里面沒有行合並,那么完全可以,但是現在有行合並,

用同一個table就比較麻煩,當然也是可以通過隱藏表格邊框線來實現單元格合並,我也試過,但缺點也很明顯,那就是里面的內容不容易居中,調起來很麻煩,所以我就選擇了使用

多個PdfPTable的方式來實現.

當遇到同時需要行合並和列合並的時候,就創建一個PdfPTable,列樹就是明細列數,比如我上圖,列數應該是6而不是4,這樣我行合並和列合並就可以分開,一個單元格就不會出現同時需要行合並和列合並,代碼是:

 PdfPTable table2 = new PdfPTable(6);
        table2.setWidthPercentage(100);
        table2.setHorizontalAlignment(PdfPTable.ALIGN_CENTER);

 cell = new PdfPCell(new Paragraph("標段", f8));
        cell.setRowspan(2);
        cell.setHorizontalAlignment(1);
        cell.setVerticalAlignment(5);
        table2.addCell(cell);
        
        cell = new PdfPCell(new Paragraph("混合料", f8));
        cell.disableBorderSide(2);
        cell.setRowspan(2);
        cell.setVerticalAlignment(5);
        cell.setHorizontalAlignment(1);
        table2.addCell(cell);
        
        cell = new PdfPCell(new Paragraph("開始碾壓樁號", f8));
        cell.setColspan(2);
        cell.setHorizontalAlignment(1);
        cell.setMinimumHeight(25);
        table2.addCell(cell);
        
        cell = new PdfPCell(new Paragraph("最后碾壓樁號", f8));
        cell.setColspan(2);
        cell.setHorizontalAlignment(1);
        cell.setMinimumHeight(25);
        table2.addCell(cell);
        
        cell = new PdfPCell(new Paragraph("欠壓率(%)", f8));
        cell.setHorizontalAlignment(1);
        cell.setMinimumHeight(25);
        table2.addCell(cell);
        
        cell = new PdfPCell(new Paragraph("超壓率(%)", f8));
        cell.setHorizontalAlignment(1);
        cell.setMinimumHeight(25);
        table2.addCell(cell);
        
        cell = new PdfPCell(new Paragraph("欠壓率(%)", f8));
        cell.setHorizontalAlignment(1);
        cell.setMinimumHeight(25);
        table2.addCell(cell);
        
        cell = new PdfPCell(new Paragraph("超壓率(%)", f8));
        cell.setHorizontalAlignment(1);
        cell.setMinimumHeight(25);
        table2.addCell(cell);
        
        cell = new PdfPCell(new Paragraph("AP1", f9));
        cell.setHorizontalAlignment(1);
        cell.setVerticalAlignment(1);
        cell.setMinimumHeight(25);
        table2.addCell(cell);
        
        cell = new PdfPCell(new Paragraph("AC-20C", f9));
        cell.setHorizontalAlignment(1);
        cell.setMinimumHeight(25);
        table2.addCell(cell);
        
        cell = new PdfPCell(new Paragraph("2.2", f9));
        cell.setHorizontalAlignment(1);
        cell.setMinimumHeight(25);
        table2.addCell(cell);
        
        cell = new PdfPCell(new Paragraph("0", f9));
        cell.setHorizontalAlignment(1);
        cell.setMinimumHeight(25);
        table2.addCell(cell);
        
        cell = new PdfPCell(new Paragraph("2.2", f9));
        cell.setHorizontalAlignment(1);
        cell.setMinimumHeight(25);
        table2.addCell(cell);
        
        cell = new PdfPCell(new Paragraph("0", f9));
        cell.setHorizontalAlignment(1);
        cell.setMinimumHeight(25);
        table2.addCell(cell);
        
        cell = new PdfPCell(new Paragraph("AP1", f9));
        cell.setHorizontalAlignment(1);
        cell.setMinimumHeight(25);
        table2.addCell(cell);
        
        cell = new PdfPCell(new Paragraph("AC-25C", f9));
        cell.setHorizontalAlignment(1);
        cell.setMinimumHeight(25);
        table2.addCell(cell);
        
        cell = new PdfPCell(new Paragraph("2.2", f9));
        cell.setHorizontalAlignment(1);
        cell.setMinimumHeight(25);
        table2.addCell(cell);
        
        cell = new PdfPCell(new Paragraph("0", f9));
        cell.setHorizontalAlignment(1);
        cell.setMinimumHeight(25);
        table2.addCell(cell);
        
        cell = new PdfPCell(new Paragraph("2.2", f9));
        cell.setHorizontalAlignment(1);
        cell.setMinimumHeight(25);
        table2.addCell(cell);
        
        cell = new PdfPCell(new Paragraph("0", f9));
        cell.setHorizontalAlignment(1);
        cell.setMinimumHeight(25);
        table2.addCell(cell);
        
        document.add(table2);

我遇到的的第二個問題是往單元格里插入圖片.

一開始我是

 Image image2 = Image.getInstance (imagePath2);
 cell = new PdfPCell(image2 );

這樣添加進去的,但是樣式出現了問題

他會把表格的邊框覆蓋度掉.然后我嘗試通過 image.scaleAbsolute(520,150)來調整圖片大小,但是始終不行.無意中發現了PdfPCell 有一個setImage(Image) 方法,

嘗試了一下,完美達標,效果如下,

 

所以如果在表格里插入圖片,則調用cell的setImage方法.代碼如下

 Image image2 = Image.getInstance (imagePath2);
        cell = new PdfPCell();
        cell.setColspan(7);
        cell.setImage(image2);
        table3.addCell(cell);  

都不需要設置圖片大小,它會自動填充整個表格.

以上就是我在生成pdf文件過程中遇到的兩個問題,在此記錄一下,如果有遇到相似問題的同學可以參考一下


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM