2012-11-21 32 views
-1

嗨! 下面是我的代碼導出數據庫到excel文件。現在根據我的要求,我想在頁面頂部添加公司的頁眉圖像.plz傢伙幫助我,並指導我完成task.thanks提前。下面是我的代碼...如何在使用itext從數據庫生成的pdf報告中添加標題圖像

 Document document = new Document(PageSize.A2); 

     PdfWriter.getInstance(document, new FileOutputStream("d:/".concat(datum1).concat(" ").concat("To").concat(" ").concat(datum2).concat(".pdf"))); 
     document.open(); 

     Image logo = Image.getInstance("d:/header.png"); 
     logo.setAlignment(Image.MIDDLE); 
     logo.scaleAbsoluteHeight(20); 
     logo.scaleAbsoluteWidth(20); 
     logo.scalePercent(100); 
     Chunk chunk = new Chunk(logo, 0, -45); 
     HeaderFooter header = new HeaderFooter(new Phrase(chunk), false); 
     header.setAlignment(Element.ALIGN_CENTER); 
     header.setBorder(Rectangle.NO_BORDER); 
     document.setHeader(header); 

     PdfPTable table = new PdfPTable(9); 
     table.setWidthPercentage(110); 
     table.addCell("calldate"); 
     table.addCell("src"); 
     table.addCell("dst"); 
     table.addCell("dstchannel"); 
     table.addCell("lastapp"); 
     table.addCell("duration"); 
     table.addCell("disposition"); 
     table.addCell("amaflags"); 
     table.addCell("cdrcost"); 


     String strQuery = ""; 
     ResultSet rs = null; 

     conexion conexiondb = new conexion(); 
     conexiondb.Conectar(); 

     strQuery = "SELECT * FROM cdrcost where date(calldate) between '" + datum1 + "' and '" + datum2 + "'"; 

     // strQuery = "SELECT * FROM cdrcost where date(calldate) between '2011-09-01' and '2012-01-01'"; 

     rs = conexiondb.Consulta(strQuery); 
     while (rs.next()) { 
      table.addCell(rs.getString("calldate")); 
      table.addCell(rs.getString("src")); 
      table.addCell(rs.getString("dst")); 
      table.addCell(rs.getString("dstchannel")); 
      table.addCell(rs.getString("lastapp")); 
      table.addCell(rs.getString("duration")); 
      table.addCell(rs.getString("disposition")); 
      table.addCell(rs.getString("amaflags")); 
      table.addCell(rs.getString("cdrcost")); 
     } 

     document.add(table); 
     document.close(); 
+0

重複的問題,昨天回答:http://stackoverflow.com/questions/13465657/itext-add-content-to-the-bottom-of-an-existing-page –

+0

@BrunoLowagie先生我經歷了鏈接,但沒有解決我的問題PLZ先生幫我 – Adarsh

+0

我會編輯我的答案。 –

回答

0
  1. 作爲add content to the bottom of an existing page解釋,setHeader()已經從iText的很久以前刪除的方法。請不要使用它!
  2. 您可以使用頁面事件輕鬆添加圖像作爲標題。頁面事件在Chapter 5 of iText in Action中解釋。您可以在SO

找到實例引用你似乎並不理解對SO前面給出的答案,讓我再說一遍:

您需要創建一個PdfPageEvent實現,例如通過延長PdfPageEventHelper類重寫onEndPage()方法。除非你指定一個

  • 不要使用onStartPage()添加內容,
  • 不要傳遞給頁面事件Document對象添加任何東西,
  • :當你這樣做的話,考慮到下面的注意事項不同的頁面大小,左下角的座標爲x = 0; y = 0。添加頁腳時需要考慮這一點。頁腳的y值低於標題的y值。

在你的代碼,你需要使用setPageEvent()方法PdfWriter對象之前打開的文檔。每當頁面完成時,您所寫的onEndPage()方法將被調用,所以這就是您需要使用PdfContentByte.addImage()添加圖像的位置。如果您希望圖像位於頁面的頂部,則需要詢問Document的尺寸,並相應地設置圖像對象的絕對位置。

如果您不明白這個詳細的解釋,請閱讀我的book或聘請iText開發人員爲您完成這項任務。

相關問題