【问题标题】:Splitting table cells diagonally itextsharp沿对角线分割表格单元格
【发布时间】:2016-03-27 21:50:01
【问题描述】:

我第一次尝试使用 iTextSharp(),但在 pdf 文档中创建表格时遇到问题。 确实,我想把第一个单元格对角分割写在第一行的标题和第一列的标题中。

图1是我能做的 图2是我想做的

谢谢。Picture 1Picture 2

【问题讨论】:

    标签: c# itextsharp pdfptable


    【解决方案1】:

    您需要使用 official documentation 中记录的 cell 事件 创建该特殊单元。

    我会给你一些伪代码,你可以将它们转换成 C# 并创建一个如下所示的表:

    这是单元格事件的伪代码:

    class Diagonal implements PdfPCellEvent {
        protected String columns;
        protected String rows;
    
        public Diagonal(String columns, String rows) {
            this.columns = columns;
            this.rows = rows;
        }
    
        public void cellLayout(PdfPCell cell, Rectangle position,
            PdfContentByte[] canvases) {
            PdfContentByte canvas = canvases[PdfPTable.TEXTCANVAS];
            ColumnText.showTextAligned(canvas, Element.ALIGN_RIGHT, 
                new Phrase(columns), position.getRight(2), position.getTop(12), 0);
            ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, 
                new Phrase(rows), position.getLeft(2), position.getBottom(2), 0);
            canvas = canvases[PdfPTable.LINECANVAS];
            canvas.moveTo(position.getLeft(), position.getTop());
            canvas.lineTo(position.getRight(), position.getBottom());
            canvas.stroke();
        }
    }
    

    这是向您展示如何使用单元格事件的伪代码:

    public void createPdf(String dest) throws IOException, DocumentException {
        Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream(dest));
        document.open();
        PdfPTable table = new PdfPTable(6);
        table.getDefaultCell().setMinimumHeight(30);
        PdfPCell cell = new PdfPCell();
        cell.setCellEvent(new Diagonal("Gravity", "Occ"));
        table.addCell(cell);
        table.addCell("1");
        table.addCell("2");
        table.addCell("3");
        table.addCell("4");
        table.addCell("5");
        for (int i = 0; i < 5; ) {
            table.addCell(String.valueOf(++i));
            table.addCell("");
            table.addCell("");
            table.addCell("");
            table.addCell("");
            table.addCell("");
        }
        document.add(table);
        document.close();
    }
    

    现在由您将这个伪代码(实际上是 Java 代码)转换为 C#。

    【讨论】:

    猜你喜欢
    • 2019-05-10
    • 2019-07-05
    • 1970-01-01
    • 2016-07-04
    • 1970-01-01
    • 2011-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多