2017-08-17 85 views
0

我使用iText5庫生成PDF文檔。
我想在pdf文件中創建一個表。
當錶行數大於3時,它工作正常。
但是,當表中的行數小於3,它產生沒有表(只有標題被打印出來。)僅表部分生成

這是我的代碼中的空白PDF文件:

import com.itextpdf.text.Chunk; 
import com.itextpdf.text.Document; 
import com.itextpdf.text.DocumentException; 
import com.itextpdf.text.Element; 
import com.itextpdf.text.Font; 
import com.itextpdf.text.Font.FontFamily; 
import com.itextpdf.text.PageSize; 
import com.itextpdf.text.Paragraph; 
import com.itextpdf.text.Phrase; 
import com.itextpdf.text.pdf.GrayColor; 
import com.itextpdf.text.pdf.PdfPCell; 
import com.itextpdf.text.pdf.PdfPTable; 
import com.itextpdf.text.pdf.PdfWriter; 

import java.io.File; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.sql.Connection; 
import java.sql.PreparedStatement; 
import java.sql.ResultSet; 
import javax.swing.JOptionPane; 
//import sandbox.WrapToTest; 

//@WrapToTest 
public class ColumnWidthExample { 
    static String f=""; 

    public static final String DEST = "C:\\Users\\Rahul\\Documents\\Challan_Reports\\"; 
// public static void main(String[] args) throws IOException, DocumentException { 
//  ColumnWidthExample.createPdf("2017-08-08","anuj"); 
// } 

    public static String createPdf(String dest,String gname) throws IOException, DocumentException { 
     try{ 

//  for (int counter = 1; counter < 101; counter++) { 
//   table.addCell(String.valueOf(counter)); 
//   table.addCell("key " + counter); 
//   table.addCell("value " + counter); 
//  } 

      Connection con=DB.getConnection(); 
      PreparedStatement ps=con.prepareStatement("select * from echallan where guarantor_name='"+gname+"' and date='"+dest+"' order by date desc"); 
      ResultSet rs=ps.executeQuery(); 
      if(rs.next()) 

      { JOptionPane.showMessageDialog(null, "exists"); 
       System.out.println(dest+gname+" cpdf"); 
     File file = new File(DEST+" "+dest+" "+gname+".pdf"); 
     file.getParentFile().mkdirs(); 

     Document document = new Document(PageSize.A4.rotate()); 
     PdfWriter.getInstance(document, new FileOutputStream(DEST+""+dest+"_"+gname+".pdf")); 
     f=DEST+" "+dest+" "+gname+".pdf"; 
     document.open(); 
     document.add(new Chunk("")); 
     document.add(new Paragraph(" Report of Guarantor "+gname+" for date "+dest)); 
     document.add(new Paragraph("")); 
     float[] columnWidths = {10, 10, 10,5,5,10,10,5,7,5,10,10,13,10,10}; 
     PdfPTable table = new PdfPTable(columnWidths); 
     table.setWidthPercentage(105); 
     table.getDefaultCell().setUseAscender(true); 
     table.getDefaultCell().setUseDescender(true); 
     Font f = new Font(FontFamily.HELVETICA, 10, Font.NORMAL, GrayColor.GRAYWHITE); 
     PdfPCell cell = new PdfPCell(new Phrase()); 

     table.getDefaultCell().setBackgroundColor(new GrayColor(0.75f)); 
     table.addCell("Merchant\ncode"); 
     table.addCell("Merchant\nname"); 
     table.addCell("Invioce"); 
     table.addCell("Bale\nfrom"); 
     table.addCell("Bale\nto"); 
     table.addCell("Total\nbale"); 
     table.addCell("Station"); 
     table.addCell("Sort"); 
     table.addCell("Chart"); 
     table.addCell("Ex\nmill"); 
     table.addCell("Rransport"); 
     table.addCell("Remarks"); 
     table.addCell("Date"); 
     table.addCell("Guarantor\nname"); 
     table.addCell("Guarantor\ncode"); 
     table.setHeaderRows(3); 
     table.setFooterRows(1); 
     table.getDefaultCell().setBackgroundColor(GrayColor.GRAYWHITE); 
     table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER); 
       cell = new PdfPCell(new Phrase()); 
       do{ 
        for(int i=1;i<=15;i++){ 
        table.addCell(""+rs.getString(i)); 
        JOptionPane.showMessageDialog(null, rs.getString(i));} 
        document.add(new Phrase()); 
       }while(rs.next()); 
       document.add(table); 
     document.close(); 

        return "Generated successfully.\n File Saved in \n "+DEST+dest+"_"+gname; 
      } 
      else 
      { 
       return "No data found"; 
      } 


     } 
     catch(Exception e) 
     { 
      System.out.print(e); 
      return "unsuccessfull"; 
     } 


    } 
} 
+0

您在do while語句和訪問列值時從1到15循環。你確定桌子有15列嗎?這看起來類似https://stackoverflow.com/questions/30050715/java-sql-sqlexception-column-index-out-of-range-8-6 – Vaibs

回答

0

我相當某些iText會生成表格,無論您輸入表格的行數是多少。

尋找問題的可能途徑: - iText不會(默認情況下)插入部分填充的行。如果您的表格有5列,而您只插入4個單元格,則只會看到標題。

此外,如果您可以自由選擇可以使用的iText版本,請使用iText7。很多錯誤修正,尤其是與表格有關的錯誤修正。

此外,在http://developers.itextpdf.com/content/itext-7-examples/itext-7-tables

3

檢查出的文檔的問題是頁眉/頁腳的行相關。

在接下來的行中,您設置了2個標題行和1個頁腳行(#setHeaderRows(int)負責設置標題行的數量,#setFooterRows(int)負責標識有多少標題行是實際頁腳行):

table.setHeaderRows(3); 
table.setFooterRows (1); 

所以如果你添加3行,你沒有實際的內容(你只有頁眉和頁腳行)。因此你的結果pdf是空的。

如果您運行的下一個片段

  float[] columnWidths = {10, 10, 10,5,5,10,10,5,7,5,10,10,13,10,10}; 
      PdfPTable table = new PdfPTable(columnWidths); 
      table.setHeaderRows(3); 
      table.setFooterRows(1); 

      int j = 55; 
      do{ 
       j--; 
       for(int i=1;i<=15;i++){ 
        table.addCell("string " + j); 
        } 
       document.add(new Phrase()); 
      } while(j >0); 
      document.add(table); 
      document.close(); 

你會看到以「串54」和行「串53」細胞重複每一頁(如頭)。對於「string 52」單元格的行(作爲頁腳)也是如此。

據我瞭解,您沒有保留頁眉和頁腳行的介紹。只要刪除上面提到的行,這將解決您的問題。

+0

老兄非常感謝它很好的工作。 –