【问题标题】:Write a title on itextPdf table border在 itextPdf 表格边框上写一个标题
【发布时间】:2016-03-02 12:01:56
【问题描述】:

我正在使用 iTextPdf 在 android 应用程序上创建一个表。

我需要将表格的标题放在其上边框,如下图所示。

我怎样才能做到这一点? 在此先感谢

【问题讨论】:

    标签: android itextpdf pdfptable


    【解决方案1】:

    请查看CellTitle 示例。它会创建一个 PDF,其中在单元格边框上添加了标题:cell_title.pdf

    这是通过使用cell event

    class Title implements PdfPCellEvent {
        protected String title;
    
        public Title(String title) {
            this.title = title;
        }
    
        public void cellLayout(PdfPCell cell, Rectangle position,
            PdfContentByte[] canvases) {
            Chunk c = new Chunk(title);
            c.setBackground(BaseColor.LIGHT_GRAY);
            PdfContentByte canvas = canvases[PdfPTable.TEXTCANVAS];
            ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, 
                new Phrase(c), position.getLeft(5), position.getTop(5), 0);
        }
    }
    

    可以使用setCellEvent() 方法将单元格事件添加到PdfPCell

    public PdfPCell getCell(String content, String title) {
        PdfPCell cell = new PdfPCell(new Phrase(content));
        cell.setCellEvent(new Title(title));
        cell.setPadding(5);
        return cell;
    }
    

    下一次,请在提问之前展示您尝试过的内容。还请查看official documentation,因为您自己回答问题所需的一切都可以在 iText 网站上找到。

    【讨论】:

      猜你喜欢
      • 2015-09-28
      • 2013-02-23
      • 2013-08-08
      • 2020-03-27
      • 1970-01-01
      • 1970-01-01
      • 2013-05-22
      • 2013-03-06
      • 2021-02-18
      相关资源
      最近更新 更多