2012-11-21 30 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开发人员为您完成这项任务。

相关问题