我有一个主表“表”,有 3 列宽度 {50f,25f,25f}。我创建了 2 个嵌套表。 在嵌套表 1 中,
PdfPTable nested12 = new PdfPTable(1);
PdfPCell cell1 = new PdfPCell(new Phrase("cell1 of nested table1", bodyFontnormalItalic));
cell1.Border = Rectangle.BOTTOM_BORDER;
nested12.AddCell(cell1);
PdfPCell cell2 = new PdfPCell(new Phrase("cell2 of nested table1", bodyFontLight));
cell2.Border = Rectangle.BOTTOM_BORDER;
nested12.AddCell(cell2);
PdfPCell cell3 = new PdfPCell(new Phrase("cell3 of nested table1", bodyFontLight));
cell3.Border = Rectangle.BOTTOM_BORDER;
nested12.AddCell(cell3);
PdfPCell nesthousing12 = new PdfPCell(nested12);
nesthousing12.Padding = 0f;
nesthousing12.Colspan = 1;
table.AddCell(nesthousing12);
嵌套表 2 是:
PdfPTable nestedTable2 = new PdfPTable(1);
PdfPCell cell4 = new PdfPCell(new Phrase("cell1 of nested table 2", bodyFontnormalItalic));
cell4.Border = Rectangle.BOTTOM_BORDER;
nestedTable2.AddCell(cell4); //here 2 cells of colspan=1 needed
PdfPCell cell5 = new PdfPCell(new Phrase("cell2 of nested table 2", bodyFontLight));
cell5.Border = Rectangle.BOTTOM_BORDER;
nestedTable2.AddCell(cell5);
PdfPCell cell6 = new PdfPCell(new Phrase("cell3 of nested table 3", bodyFontLight));
cell6.Border = Rectangle.BOTTOM_BORDER;
nestedTable2.AddCell(cell6);
PdfPCell nesthousing = new PdfPCell(nestedTable2);
nesthousing.Padding = 0f;
nesthousing.Colspan = 2;
table.AddCell(nesthousing);
对于嵌套表 2,我使用了 Colspan=2 。我需要波纹管格式的 nestedtable2。嵌套 table2 的第一行应该有 2 个单元格。
那么我怎样才能像这样创建呢?你能帮帮我吗?
上表是我需要的确切输出。所以我创建了一个包含 6 列的表。地址和其他详细信息的 3 列内表。
最佳答案
我忽略了你问题中的代码,因为我不明白它。我只理解你分享的图片,我写了一些伪代码,允许你创建一个如下所示的表:
这看起来或多或少像预期的结果,不是吗?现在由您将以下伪代码转换为 C#。
这是绘制主表边框的代码:
public class BorderEvent implements PdfPTableEvent {
public void tableLayout(PdfPTable table, float[][] widths, float[] heights, int headerRows, int rowStart, PdfContentByte[] canvases) {
float width[] = widths[0];
float x1 = width[0];
float x2 = width[width.length - 1];
float y1 = heights[0];
float y2 = heights[heights.length - 1];
PdfContentByte cb = canvases[PdfPTable.LINECANVAS];
cb.rectangle(x1, y1, x2 - x1, y2 - y1);
cb.stroke();
}
}
这是创建完整表格的代码:
public void createPdf(String dest) throws IOException, DocumentException {
Document document = new Document(PageSize.A4.rotate());
PdfWriter.getInstance(document, new FileOutputStream(dest));
document.open();
PdfPTable table = new PdfPTable(4);
table.setTableEvent(new BorderEvent());
table.setWidths(new int[]{1, 12, 8, 1});
table.getDefaultCell().setBorder(Rectangle.NO_BORDER);
// first row
PdfPCell cell = new PdfPCell(new Phrase("Main table"));
cell.setBorder(Rectangle.NO_BORDER);
cell.setColspan(4);
table.addCell(cell);
// second row
table.addCell("");
table.addCell("nested table 1");
table.addCell("nested table 2");
table.addCell("");
// third row
// third row cell 1
table.addCell("");
// third row cell 2
PdfPTable table1 = new PdfPTable(1);
table1.addCell("cell 1 of nested table 1");
table1.addCell("cell 2 of nested table 1");
table1.addCell("cell 2 of nested table 1");
table.addCell(new PdfPCell(table1));
// third row cell 3
PdfPTable table2 = new PdfPTable(2);
table2.getDefaultCell().setMinimumHeight(10);
table2.addCell("");
table2.addCell("");
cell = new PdfPCell(new Phrase("cell 2 of nested table 2"));
cell.setColspan(2);
table2.addCell(cell);
cell = new PdfPCell(new Phrase("cell 3 of nested table 2"));
cell.setColspan(2);
table2.addCell(cell);
table.addCell(new PdfPCell(table2));
// third row cell 4
table.addCell("");
// fourth row
cell = new PdfPCell();
cell.setColspan(4);
cell.setBorder(Rectangle.NO_BORDER);
cell.setMinimumHeight(16);
table.addCell(cell);
document.add(table);
document.close();
}
如您所见,嵌套表 2 需要有 2 列,以便您可以在其第一行中包含两个单元格。显然,您需要为用于第 2 行和第 3 行的两个单元格定义 colspan 为 2。
您可能已经注意到伪代码实际上是 Java 代码。解释该代码并将其移植到 C# 应该不是问题。
更新
你似乎有把简单的事情复杂化的本领。在您的原始绘图中,您在所需结果周围绘制了一个额外的矩形。在我的回答中,我模仿了 NestedTables4 中的那幅画。例如,您对结果不满意。
现在你已经简化了绘图,我做了一个 NestedTables5更容易理解的例子。结果如下所示:
尽管很简单,但您仍然使问题听起来很复杂。你说(我引用):我创建了一个包含 6 列的表格。 3 列的内表。但这没有意义,是吗?
如果你想在一个单独的表中创建这个完整的结构,使用两个内表,你需要一个有 7 列的表:
第一个单元格的 colspan 为 3,包含一个 1 列 3 行的表格。第二个单元格的 colspan 为 4,包含一个 2 列 3 行的表格。表格下部的第三个单元格始终具有 colspan 2。
这是基本逻辑。但是,为什么要把这样一个简单的表弄得这么复杂呢?如文档所述,PdfPTable
的设计方式使您可以一个接一个地添加 PdfPTable
的多个实例,甚至不会注意到有不同的表在起作用。
这就是我在 NestedTables5 中所做的例子。我为上半部分创建了一个包含 2 列的表格,为下半部分创建了一个包含 6 列的表格:
public void createPdf(String dest) throws IOException, DocumentException {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(dest));
document.open();
// Header part
PdfPTable table = new PdfPTable(2);
table.setWidths(new int[]{50, 50});
// first cell
PdfPTable table1 = new PdfPTable(1);
table1.getDefaultCell().setMinimumHeight(30);
table1.addCell("Address 1");
table1.addCell("Address 2");
table1.addCell("Address 3");
table.addCell(new PdfPCell(table1));
// second cell
PdfPTable table2 = new PdfPTable(2);
table2.addCell("Date");
table2.addCell("Place");
PdfPCell cell = new PdfPCell(new Phrase("References"));
cell.setMinimumHeight(40);
cell.setColspan(2);
table2.addCell(cell);
cell = new PdfPCell(new Phrase("destination"));
cell.setColspan(2);
table2.addCell(cell);
table.addCell(new PdfPCell(table2));
// second row
cell = new PdfPCell();
cell.setColspan(2);
cell.setMinimumHeight(16);
table.addCell(cell);
document.add(table);
// Body part
table = new PdfPTable(6);
table.setWidths(new int[]{ 1, 2, 6, 1, 2, 2 });
table.addCell("sl no");
table.addCell("qty");
table.addCell("Product");
table.addCell("units");
table.addCell("rate");
table.addCell("total");
table.setHeaderRows(1);
for (int i = 0; i < 6; ) {
table.getDefaultCell().setBorder(Rectangle.LEFT | Rectangle.RIGHT);
table.getDefaultCell().setMinimumHeight(16);
table.addCell(String.valueOf(++i));
table.addCell("");
table.addCell("");
table.addCell("");
table.addCell("");
table.addCell("");
}
table.getDefaultCell().setFixedHeight(3);
table.getDefaultCell().setBorder(Rectangle.LEFT | Rectangle.RIGHT | Rectangle.BOTTOM);
table.addCell("");
table.addCell("");
table.addCell("");
table.addCell("");
table.addCell("");
table.addCell("");
document.add(table);
document.close();
}
结果如第二幅图所示。参见 nested_tables5.pdf并将其与您需要的进行比较。
重要
看起来您正在使用 iTextSharp 创建发票,并且自 3 月 28 日(一周前)以来您一直在尝试这样做。以后请先阅读文档。这将为您节省大量时间,否则您可能会在反复试验 过程中损失这些时间。您可以下载一本名为 ZUGFeRD: The Future of Invoicing 的电子书免费,您甚至可以在线免费浏览该书:ZUGFeRD: The Future of Invoicing .它逐步解释了如何创建如下图所示的发票:
如果所有的例子都在那里,你为什么要重新发明轮子?
关于c# - iTextSharp 嵌套表格 colspan,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36259214/