【问题标题】:Header and footer with content, itextpdf带有内容的页眉和页脚,itextpdf
【发布时间】:2017-03-12 13:41:55
【问题描述】:

我的问题是当我尝试在 pdf 中添加内容时,我的表格从头开始出现而不是在标题下方,我的内容表格高度不正确

代码有点长,我不能上传图片,但我有一个带分页的页眉和页脚

看起来它只是在我的表格中添加一个带有内容的空间但我不知道该怎么做,我认为我的问题出在摘要方法中

private void addHeader(PdfWriter writer) {
    PdfPTable tableHeader = new PdfPTable(2);
    try {
        // set default
        tableHeader.setWidths(new int[] { 2, 24 });
        tableHeader.setTotalWidth(527);
        tableHeader.setLockedWidth(true);
        tableHeader.getDefaultCell().setFixedHeight(40);
        tableHeader.getDefaultCell().setBorder(Rectangle.BOTTOM);
        tableHeader.getDefaultCell().setBorderColor(BaseColor.LIGHT_GRAY);
        // addImage
        Image img = Image.getInstance(getClass().getClassLoader().getResource("imgPDF/logo.png"));
        tableHeader.addCell(img);
        // addText
        PdfPCell text = new PdfPCell();
        text.setPaddingBottom(15);
        text.setPaddingLeft(10);
        text.setBorder(Rectangle.BOTTOM);
        text.setBorderColor(BaseColor.LIGHT_GRAY);
        text.addElement(new Phrase(cve.getId(), new Font(Font.FontFamily.HELVETICA, 13)));
        text.addElement(new Phrase("https://myapp.com", new Font(Font.FontFamily.HELVETICA, 10)));
        tableHeader.addCell(text);
        // write content
        tableHeader.writeSelectedRows(0, -1, 34, 803, writer.getDirectContent());

    } catch (DocumentException e) {
        throw new ExceptionConverter(e);
    } catch (MalformedURLException e) {
        throw new ExceptionConverter(e);
    } catch (IOException e) {
        throw new ExceptionConverter(e);
    }

private void summary() throws IOException, DocumentException {
    PdfPTable table = new PdfPTable(2);
    table.setHeaderRows(0);
    table.setWidthPercentage(100);
    table.setSpacingBefore(15);
    table.setTotalWidth(100);

    // Add headers
    table.addCell(createHeaderCellWithColor("Summary"));
    table.addCell(createHeaderCellWithColor("ACCESS"));
    table.addCell(createCell("row 1"));
    table.addCell(createCell("row 2"));
    table.addCell(createCell("row 3"));

    PdfPTable table3 = new PdfPTable(3);
    PdfPCell cell = new PdfPCell(new Phrase("aaa"));
    cell.setColspan(3);
    table3.addCell(cell);

    table.addCell(createHeaderCellWithColor("IMPACT"));

    document.add(table);

有了这个,我基本上解决了,

document = new Document (PageSize.A4, 36, 36, 90, 36);

虽然我想下载更多的内容,而且离标题不太近

【问题讨论】:

    标签: itext


    【解决方案1】:

    之前有这个方法,参数等于15,使得header下面的内容,这个更加独立,,,简单的等于填充

    已解决

    text.setPaddingBottom(8);
    

    【讨论】:

      猜你喜欢
      • 2015-04-01
      • 1970-01-01
      • 2013-09-20
      • 1970-01-01
      • 2017-06-14
      • 2022-01-15
      • 2019-03-14
      • 1970-01-01
      • 2010-09-27
      相关资源
      最近更新 更多