在标题图像后在iText上插入图像

时间:2012-04-30 06:40:35

标签: java itext

我生成一个事件,用于向我的pdf文档中的每个页面添加页眉和页脚,问题是当我向页面添加新图像时,新图像显示在标题图像下。我试图找到解决方案,但我找不到它,我已尝试使用Alpha通道设置的png图像,但问题不会消失。

 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);
    }
}

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

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