java - 在 iText 上标题图像之后插入图像

标签 java itext

我生成一个事件,用于向 pdf 文档的每个页面添加页眉和页脚,问题是当我向页面添加新图像时,新图像出现在页眉图像下方。我试图找到解决方案,但我找不到它,我尝试过使用 Alpha channel 设置的 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);
    }
}

预先感谢您的帮助。

最佳答案

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

关于java - 在 iText 上标题图像之后插入图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10379506/

相关文章:

java - 在没有管理员权限的 Windows 10 上安装 Weblogic 12.2.1.3

java - 拦截查询字符串

java - 什么更好 : Interface or playing with public/protected?

java - 如何使用 Java 将 PDF 中表格的特定单元格内的文本设为粗体

java - CWE-73 : External Control of File Name or Path Veracode java solution

java - Java C/C++ 转换中的指针

java - 解析 PDF 时出现奇怪的空格

java - iText-PdfReader : Rebuild failed: Dictionary key endstream is not a name

pdf - iTextSharp PDF 使用 C# 读取突出显示的文本(突出显示注释)

上传加密 PDF 文件时出现 java.lang.ClassNotFoundException