2012-04-30 44 views
2

我生成一个事件,用于向我的PDF文档中的每个页面添加页眉和页脚,问题是当我向页面添加新图像时,新图像显示在页眉下图片。我试图找到解决方案,但我无法找到它,我尝试在Alpha通道设置的PNG图像,但问题不会消失。在头文件图像后在iText上插入图像

class PieCabecera extends PdfPageEventHelper{ 
    public int numeroPagina; 
    public Image imagen; 
    public PdfPTable tabla; 
    public PdfTemplate tpl; 
    public Phrase cabecera; 
    Font smallBold = new Font(Font.FontFamily.HELVETICA, 1, Font.BOLD); 

    /** 
    * 
    * @param writer 
    * @param documento 
    */ 
    @Override 
    public void onStartPage(PdfWriter writer, Document documento){ 
     numeroPagina++; 
     try{ 
      imagen = Image.getInstance("D:/Users/Operador/Documents/NetBeansProjects/ServiciosWeb-dev/web/img/logoPDF.jpg"); 
      imagen.setAbsolutePosition(50, 0); 
      PdfContentByte cbCabecera = writer.getDirectContent(); 
      tpl = cbCabecera.createTemplate(600, 250); 
      tpl.addImage(imagen); 
      cbCabecera.addTemplate(tpl, 0, 750); 
      cabecera = new Phrase(cbCabecera + ".", smallBold); 
      documento.add(cabecera); 
      Paragraph parrafo0 = new Paragraph(); 
      parrafo0.setSpacingBefore(12); 
      parrafo0.setSpacingAfter(14); 
      documento.add(parrafo0); 
      /*Línea de separación*/ 
      LineSeparator ls = new LineSeparator(); 
      documento.add(new Chunk(ls)); 
      Paragraph parrafo = new Paragraph(); 
      parrafo.setSpacingBefore(4); 
      documento.add(parrafo); 
     }catch(BadElementException e){ 
      LOGGER.log(Level.SEVERE, "Error: {0}", e.getStackTrace()); 
     }catch(IOException e){ 
      LOGGER.log(Level.SEVERE, "Error: {0}", e.getStackTrace()); 
     }catch(DocumentException e){ 
      LOGGER.log(Level.SEVERE, "Error: {0}", e.getStackTrace()); 
     } 
    } 

    /** 
    * 
    * @param writer 
    * @param documento 
    */ 
    @Override 
    public void onEndPage(PdfWriter writer, Document documento){ 
     Rectangle rect = writer.getBoxSize("art"); 
     //header 
     ColumnText.showTextAligned(writer.getDirectContent(),Element.ALIGN_CENTER, cabecera, rect.getRight(), rect.getTop(), 0); 
     //footer 
     ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_CENTER, new Phrase(String.format("Página %d", numeroPagina)), (rect.getLeft() + rect.getRight())/2, rect.getBottom() - 18, 0); 
    } 
} 

在此先感谢您的帮助。

回答

2

您是否设置了Document的边距,以便考虑页眉和页脚的高度?

+0

是的,在这一刻我会“玩”,谢谢你的帮助。 – lfergon

+0

这是错误的,由几个像素,谢谢。 – lfergon

+0

不客气! –