首页 文章

针对C#的iText PDF版本7的问题

提问于
浏览
0

我正在使用iText PDF生成一个PDF文件,其中包含一个应按如下方式布置的表格:

------------------------------------------------
|                   HEADER                       |
 ------------------------------------------------
|  some data goes here |  more data here         |
 ------------------------------------------------
| Col 1  | Col 2 | Col 3 | Col 4 | Col 5 | Col 6 |
 ------------------------------------------------
|   1    | SDF      wer    qwerwq | weqr | WERQW |
 ------------------------------------------------ 
|        |                        |      |       |
|        |                        |      |       |
|        |                        |      |       |
|        |                        |      |       |
 ------------------------------------------------
|  footer information                            |
 ------------------------------------------------

但是表格如下:

------------------------------------------------
| HEADER                                         |
 ------------------------------------------------
|  some data goes here |  more data here         |
 ------------------------------------------------
| Col 1  | Col 2 | Col 3 | Col 4 | Col 5 | Col 6 |
 ------------------------------------------------
|   1    | SDF   |  wer  | qwerwq | weqr | WERQW |
 ------------------------------------------------ 
|        |                        |      |       |
 ------------------------------------------------  
|        |                        |      |       |
 ------------------------------------------------ 
|        |                        |      |       |
 ------------------------------------------------ 
|        |                        |      |       |
 ------------------------------------------------
|  footer information                            |
 ------------------------------------------------

我尝试过遵循这些示例,但它们是用Java编写的,而C#的Object模型似乎略有不同 . “Col 1”值为1的行下方的行分为第2,3和4列 .

注意事项:

  • 对于 Headers 单元格我通过调用cell.SetHorizontalAlignment(HorizontalAlignment.CENTER)设置水平对齐方式

  • 我需要将某些文本的颜色设置为红色

  • 我正在使用table.AddCell方法添加单元格

  • 我正在将表格的边框(根据文档,这是默认单元格)设置为Border.NO_BORDER .

  • 这适用于用C#编写的Web应用程序

  • 我已经下载了最新版的iText(版本7.0.1)

  • 我创建了一个自定义的CellRender,但似乎没有效果 .

  • 最初我使用的是iText 5,但我需要更好地控制表格的渲染,因为我需要知道我们到达页面的距离 .

  • 这是我用来创建单元格的代码:

PdfFont cellFont = font;

    if ((fontStyle & FONT_STYLE_BOLD) == FONT_STYLE_BOLD && (fontStyle & FONT_STYLE_ITALIC) == FONT_STYLE_ITALIC)
    {
        cellFont = fontBoldItalic;
    }
    else if ((fontStyle & FONT_STYLE_BOLD) == FONT_STYLE_BOLD)
    {
        cellFont = fontBold;
    }
    else if ((fontStyle & FONT_STYLE_ITALIC) == FONT_STYLE_ITALIC)
    {
        cellFont = fontItalic;
    }

    Color fontColor = Color.BLACK;
    if ((fontStyle & FONT_STYLE_RED) == FONT_STYLE_RED)
    {
        fontColor = Color.RED;
    }

    Text text = new Text(content);
    text.SetFont(cellFont);
    text.SetFontColor(fontColor);
    text.SetFontSize(fontSize);

    if ((fontStyle & FONT_STYLE_UNDERLINE) == FONT_STYLE_UNDERLINE)
    {
        text.SetUnderline();
    }

    Cell cell = new Cell(rowspan, colspan);
    cell.Add(new Paragraph(text));
    //cell.SetNextRenderer(new CellBorders(cell, borders));

    return cell;

这是创建表的方式,并在Web方法结束时将表添加到文档中:

Table table = new Table(6);
        table.SetWidthPercent(100);
        table.SetPadding(3);
        table.SetSpacingRatio(1);
        table.SetBorder(Border.NO_BORDER);

1 回答

  • 2

    你有两个问题:

    • 文本未正确对齐( Headers )单元格中的文本对齐方式是使用 SetTextAlignment() 方法设置的 . SetHorizontalAlignment 设置包装文本的容器的对齐方式 .

    • 边框未按预期显示 . 首先,在iText 7中定义边框并不像在iText 5中那样定义默认的单元格行为 . 由于默认的tableRenderer没有使用border属性,因此除非您定义自定义渲染器并且make,否则_438169将无效 . 自己使用 property .

    定义自定义边框的正确方法是在单个单元格的级别上执行此操作 . 在这里您需要记住,由于边框重叠,如果您不希望边框显示,则需要将所有重叠边框设置为NO_BORDER:

    for(int j = 0; j<3; j++){
            Cell dCell = new Cell();
            //When setting borders to NO_BORDER, remember to set that border to NO_BORDER in all adjoining cells
            dCell.SetBorderLeft(Border.NO_BORDER);
            dCell.SetBorderRight(Border.NO_BORDER);
            dCell.Add(new Paragraph(String.Format("Entry, {0}-{1}",i,j)));
            //Dashed, striped, grooved and more can be specified
            if(i != 4) dCell.SetBorderBottom(new DashedBorder(1f));
            tab.AddCell(dCell);
        }
    

    上面的片段将导致条目之间没有边框的单元格 .

    for(int k = 4; k<6;k++){
            Cell d2Cell = new Cell();
            //Only the right-most border will not show, since the left-most borders of the neighbouring cells still get drawn
            d2Cell.SetBorderRight(Border.NO_BORDER);
            d2Cell.Add(new Paragraph(String.Format("Entry, {0}-{1}",i,k)).SetFontColor(Color.RED));
            //Specific borders apart from NO_BORDER do override the default
            if(i!=4) d2Cell.SetBorderBottom(new DashedBorder(1f));
            tab.AddCell(d2Cell);
        }
    

    上面的片段在最右边的单元格的右边只有没有边框 .

    下面是一个自包含的代码片段,它构造一个类似问题的表:

    public void CreateTable(string dest)
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
            Document doc = new Document(pdfDoc);
    
            Table tab = new Table(6);
            //Table should take up the entire width
            tab.SetWidthPercent(100);
            tab.SetPadding(3);
            //Because table takes up the entire width, this has no visual effect, but otherwise it will center the table
            tab.SetHorizontalAlignment(HorizontalAlignment.CENTER);
    
    
            //Header cell
            Cell hCell = new Cell(1, 6);
            hCell.Add(new Paragraph("Centered Header"));
            //Text is aligned by calling SetTextAlignment
            hCell.SetTextAlignment(TextAlignment.CENTER);
    
            tab.AddCell(hCell);
            //Subheaders
            Cell shCellL = new Cell(1, 3);
            shCellL.Add(new Paragraph("Left aligned data"));
            shCellL.SetTextAlignment(TextAlignment.LEFT);
    
            Cell shCellR = new Cell(1, 3);
            shCellR.Add(new Paragraph("Right aligned data"));
            shCellR.SetTextAlignment(TextAlignment.RIGHT);
    
            tab.AddCell(shCellL);
            tab.AddCell(shCellR);
            //col names
            for (int i = 0; i < 6; i++)
            {
                Cell colName = new Cell();
                colName.Add(new Paragraph(String.Format("Col {0}", i)));
                colName.SetTextAlignment(TextAlignment.CENTER);
                tab.AddCell(colName);
            }
            //data cols
            for (int i = 1; i < 5; i++)
            {
                Cell nC = new Cell();
                nC.Add(new Paragraph("" + i));
                tab.AddCell(nC);
                for (int j = 0; j < 3; j++)
                {
                    Cell dCell = new Cell();
                    //When Setting borders to NO_BORDER, remember to Set that border to NO_BORDER in all adjoining cells
                    dCell.SetBorderLeft(Border.NO_BORDER);
                    dCell.SetBorderRight(Border.NO_BORDER);
                    dCell.Add(new Paragraph(String.Format("Entry, {0}-{1}", i, j)));
                    //Dashed, striped, grooved and more can be specified
                    if (i != 4) dCell.SetBorderBottom(new DashedBorder(1f));
                    tab.AddCell(dCell);
                }
                for (int k = 4; k < 6; k++)
                {
                    Cell d2Cell = new Cell();
                    //Only the rightmost border will not show, since the left-most borders of the neighbouring cells still get drawn
                    d2Cell.SetBorderRight(Border.NO_BORDER);
                    d2Cell.Add(new Paragraph(String.Format("Entry, {0}-{1}", i, k)).SetFontColor(Color.RED));
                    //Specific borders apart from NO_BORDER do override the default
                    if (i != 4) d2Cell.SetBorderBottom(new DashedBorder(1f));
                    tab.AddCell(d2Cell);
                }
            }
    
            //footer cell
            Cell fCell = new Cell(1, 6);
            fCell.Add(new Paragraph("footer"));
            tab.AddCell(fCell);
    
            //Add table to document
            doc.Add(new Paragraph("Complex Table example"));
            doc.Add(tab);
            doc.Close();
        }
    

相关问题