对角拆分表格单元格 [英] Splitting table cells diagonally itextsharp

查看:173
本文介绍了对角拆分表格单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

谢谢.

这是单元事件的伪代码:

  class对角线实现PdfPCellEvent {受保护的String列;受保护的字符串行;公共Diagonal(字符串列,字符串行){this.columns =列;this.rows =行;}public void cellLayout(PdfPCell单元格,矩形位置,PdfContentByte [] canvas){PdfContentByte canvas = canvass [PdfPTable.TEXTCANVAS];ColumnText.showTextAligned(画布,Element.ALIGN_RIGHT,新词组(列),position.getRight(2),position.getTop(12),0);ColumnText.showTextAligned(画布,Element.ALIGN_LEFT,新的短语(行),position.getLeft(2),position.getBottom(2),0);canvas = canvass [PdfPTable.LINECANVAS];canvas.moveTo(position.getLeft(),position.getTop());canvas.lineTo(position.getRight(),position.getBottom());canvas.stroke();}} 

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

  public void createPdf(String dest)引发IOException,DocumentException {Document document = new Document();PdfWriter.getInstance(document,new FileOutputStream(dest));document.open();PdfPTable表=新的PdfPTable(6);table.getDefaultCell().setMinimumHeight(30);PdfPCell单元格=新的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");对于(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#.

I am trying to use iTextSharp() for the first time, and I'm having an issue creating a table in a pdf document. Indeed, I want to split the first cell diagonally to write in the title of the first row and the title of the first column.

Picture 1 is what I could do Picture 2 is what I want to do

Thank you.Picture 1 Picture 2

解决方案

You need to create that special cell using a cell event as documented in the official documentation.

I'll give you some pseudo code that you can convert to C# and that will create a table that looks like this:

This is the pseudo-code for the cell event:

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();
    }
}

This is the pseudo-code that shows you how to use the cell event:

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();
}

Now it's up to you to convert this pseudo code (that is actually working Java code) to C#.

这篇关于对角拆分表格单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆