C# 用于C的iText PDF第7版存在问题#
我正在使用iText PDF生成一个PDF文件,其中包含一个表格,该表格应按如下方式排列:C# 用于C的iText PDF第7版存在问题#,c#,pdf,itext,itext7,C#,Pdf,Itext,Itext7,我正在使用iText PDF生成一个PDF文件,其中包含一个表格,该表格应按如下方式排列: ------------------------------------------------ | HEADER | ------------------------------------------------ | some data goes here | more data here | -
------------------------------------------------
| 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列
注意事项:
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;
Table table = new Table(6);
table.SetWidthPercent(100);
table.SetPadding(3);
table.SetSpacingRatio(1);
table.SetBorder(Border.NO_BORDER);
这里有两个问题:
SetTextAlignment()
方法设置单元格中的文本对齐方式SetHorizontalAlignment
设置包装文本的容器的对齐方式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。我没有做的是在尝试设置我想要的边界之前删除边界。今天晚些时候我有时间时,我会写一个正确的答案。我们正在编写更多关于表格、渲染器和边界的文档,因为这些问题似乎一直在出现