C# 用于C的iText PDF第7版存在问题#

C# 用于C的iText PDF第7版存在问题#,c#,pdf,itext,itext7,C#,Pdf,Itext,Itext7,我正在使用iText PDF生成一个PDF文件,其中包含一个表格,该表格应按如下方式排列: ------------------------------------------------ | HEADER | ------------------------------------------------ | some data goes here | more data here | -

我正在使用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                            |
 ------------------------------------------------
然而,该表绘制如下:

 ------------------------------------------------
|                   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#的对象模型似乎有细微的不同。“Col 1”值为1的行下面的行被拆分为第2、3和4列

注意事项:

  • 对于标题单元格,我通过调用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);
    

    这里有两个问题:

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

  • 边框未按预期显示。 首先,在iText 7中定义表的边框并不像在iText 5中那样定义默认单元格行为。由于默认tableRenderer不使用border属性,
    table#SetBorder(border.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 j=0;j这里有两个问题:

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

  • 边框未按预期显示。 首先,在表上定义边框不会像在iText 5中那样在iText 7中定义默认单元格行为。由于默认的tableRenderer不使用border属性,
    table#SetBorder(border.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 j=0;j)对于标题单元格,您是否尝试过将文本或段落的水平对齐方式设置为
    TextAlignment.CENTER
    SetHorizontalAlignment
    方法)?对于边框,您可以在单元格本身上指定边框属性。(
    单元格#setOrder/setOrderLeft/…
    ).第一条注释中的小错误,使用的方法是
    SetTextAlignment()
    感谢Samuel为我指出了正确的方向:D。我没有做的是在尝试设置我想要的边界之前删除边界。今天晚些时候我有时间的时候,我会写一个正确的答案。我们正在编写更多关于表、渲染器和边界的文档,因为这些问题似乎一直在我脑海中浮现标题单元格,您是否尝试将文本或段落的水平对齐方式设置为
    TextAlignment.CENTER
    SetHorizontalAlignment
    方法)?至于边框,您可以在单元格本身上指定边框属性。(
    单元格#setOrder/setOrderLeft/…
    ).第一条注释中的小错误,使用的方法是
    SetTextAlignment()
    感谢Samuel为我指出了正确的方向:D。我没有做的是在尝试设置我想要的边界之前删除边界。今天晚些时候我有时间时,我会写一个正确的答案。我们正在编写更多关于表格、渲染器和边界的文档,因为这些问题似乎一直在出现