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

注意事项:

  1. 对于标题单元格,我通过调用 cell.SetHorizo​​ntalAlignment(Horizo​​ntalAlignment.CENTER) 设置水平对齐方式
  2. 我需要将一些文本的颜色设置为红色
  3. 我正在使用 table.AddCell 方法添加单元格
  4. 我将表格的边框(根据文档,这是默认单元格)设置为 Border.NO_BORDER。
  5. 这是一个用 C# 编写的 Web 应用程序
  6. 我已经下载了最新版本的 iText(版本 7.0.1)
  7. 我创建了一个自定义 CellRender,但这似乎没有效果。
  8. 最初我使用的是 iText 5,但我需要更好地控制表格的呈现,因为我需要知道我们到达的页面有多远。
  9. 这是我用来创建单元格的代码:

        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);
4

1 回答 1

2

你在这里有两个问题:

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

  2. 边框未按预期显示。首先,在表格上定义边框不会像在 iText 5 中那样定义 iText 7 中的默认单元格行为。由于默认 tableRenderer 不使用边框属性,table#SetBorder(Border.NO_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 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();
    }
于 2016-09-23T09:06:59.233 回答