【发布时间】:2021-06-10 20:43:08
【问题描述】:
我一直在尝试使用 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, 20);
Table table2 = new Table(UnitValue.CreatePercentArray(new float[] {10,20,40,10,10,20,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 来做这样的事情,任何建议都将不胜感激。如果您需要更多信息,请告诉我。
【问题讨论】: