问题描述
我一直在尝试使用 iText7 和 不同的列大小 在表格中创建一个 PDF 文档,在我的代码中,我已经设置了每列的宽度.但我无法得到想要的结果。
版本 - itext7-core:7.1.15
这是部分代码:
private void ManipulatePdf(string pdfPath)
{
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(pdfPath));
Document doc = new Document(pdfDoc);
doc.SetMargins(20,20,20);
Table table2 = new Table(UnitValue.CreatePercentArray(new float[] {10,40,10,10 })).UseAllAvailableWidth();
table2.SetWidth(UnitValue.CreatePercentValue(100));
table2.SetFixedLayout();
table2.AddCell(getCellm1(300));
table2.AddCell(getCellm2(240));
table2.AddCell(getCellm3(100));
table2.AddCell(getCellm4(100));
table2.AddCell(getCellm5(100));
table2.AddCell(getCellm6(300));
table2.AddCell(getCellm6(300));
doc.Add(table2);
doc.Close();
}
private Cell getCellm7(int cm)
{
Cell cell = new Cell(1,7);
Paragraph p = new Paragraph(
String.Format("%smm",10 * cm)).SetFontSize(8);
p.SetTextAlignment(iText.Layout.Properties.TextAlignment.CENTER);
cell.Add(p);
return cell;
}
private Cell getCellm6(int cm)
{
Cell cell = new Cell(1,6);
Paragraph p = new Paragraph(
String.Format("%smm",10 * cm)).SetFontSize(8);
p.SetTextAlignment(iText.Layout.Properties.TextAlignment.CENTER);
cell.Add(p);
return cell;
}
private Cell getCellm5(int cm)
{
Cell cell = new Cell(1,5);
Paragraph p = new Paragraph(
String.Format("%smm",10 * cm)).SetFontSize(8);
p.SetTextAlignment(iText.Layout.Properties.TextAlignment.CENTER);
cell.Add(p);
return cell;
}
private Cell getCellm4(int cm)
{
Cell cell = new Cell(1,4);
Paragraph p = new Paragraph(
String.Format("%smm",10 * cm)).SetFontSize(8);
p.SetTextAlignment(iText.Layout.Properties.TextAlignment.CENTER);
cell.Add(p);
return cell;
}
private Cell getCellm3(int cm)
{
Cell cell = new Cell(1,3);
Paragraph p = new Paragraph(
String.Format("%smm",10 * cm)).SetFontSize(8);
p.SetTextAlignment(iText.Layout.Properties.TextAlignment.CENTER);
cell.Add(p);
return cell;
}
private Cell getCellm2(int cm)
{
Cell cell = new Cell(1,2);
Paragraph p = new Paragraph(
String.Format("%smm",10 * cm)).SetFontSize(8);
p.SetTextAlignment(iText.Layout.Properties.TextAlignment.CENTER);
cell.Add(p);
return cell;
}
private Cell getCellm1(int cm)
{
Cell cell = new Cell(1,1);
Paragraph p = new Paragraph(
String.Format("%smm",10 * cm)).SetFontSize(8);
p.SetTextAlignment(iText.Layout.Properties.TextAlignment.CENTER);
cell.Add(p);
return cell;
}
从上面的代码得到不同的列大小,我想要的结果如下:
这就是我希望列的外观:
如果有人使用 iText 做这样的事情,任何建议将不胜感激。如果您需要更多信息,请告诉我。
解决方法
您的百分比值 (10,20,40,10,10
) 加起来是 120,而不是 100。调用 .UseAllAvailableWidth()
等效于 table2.SetWidth(UnitValue.CreatePercentValue(100));
- 无需同时执行它们。
您还可以将数字传递给 Cell
构造函数(例如 new Cell(1,4)
)。这些数字实际上并不表示单元格的位置,而是表示单元格的行跨度和列跨度。这就是意外布局的真正原因。除非您希望单元格占据多行或多列,否则您不必向单元格构造函数传递任何内容。
修正上述错误后,结果如下:
参考代码:
private void ManipulatePdf(string pdfPath) {
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(pdfPath));
Document doc = new Document(pdfDoc);
doc.SetMargins(20,20);
Table table2 = new Table(UnitValue.CreatePercentArray(new float[] {8,15,38,8,8}))
.UseAllAvailableWidth();
table2.SetFixedLayout();
table2.AddCell(CreateCell("S.No"));
table2.AddCell(CreateCell("Item Code"));
table2.AddCell(CreateCell("Description"));
table2.AddCell(CreateCell("Qty"));
table2.AddCell(CreateCell("Unit"));
table2.AddCell(CreateCell("Unit Price"));
table2.AddCell(CreateCell("P.Unit"));
doc.Add(table2);
doc.Close();
}
private Cell CreateCell(String text) {
Cell cell = new Cell();
Paragraph p = new Paragraph(text).SetFontSize(8);
p.SetTextAlignment(TextAlignment.CENTER);
cell.Add(p);
return cell;
}