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

时间:2016-09-21 16:06:19

标签: c# pdf itext itext7

我正在使用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列。

注意事项:

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

        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;
    
  11. 这是创建表的方式,并在Web方法结束时将表添加到文档中:

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

1 个答案:

答案 0 :(得分:2)

这里有两个问题:

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

  2. 边框未按预期显示。 首先,在iText 7中定义边框并不像在iText 5中那样定义默认单元格行为。由于默认的tableRenderer没有使用border属性,table#SetBorder(Border.NO_BORDER)将无效,除非您定义了自定义渲染器并自行使用该属性。

  3. 定义自定义边框的正确方法是在单个单元格的级别上执行此操作。在这里你需要记住,由于边框重叠,如果你不想要边框显示,你需要将所有重叠边框设置为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();
        }