2

我生成了一个事件,用于在我的 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);
    }
}

在此先感谢您的帮助。

4

1 回答 1

2

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

于 2012-04-30T09:12:17.887 回答