如何使用 itextsharp 创建嵌套列?
How to create nested column using itextsharp?
如何使用 iext sharp 创建下面提到的 table。下面是我的代码,但我很困惑。帮我做一个这样的类型table。
我的代码
var subTable2 = new PdfPTable(new float[] { 100, 100, 100, 100, 100, 100,100,100 });
subTable2.TotalWidth = 510f;
subTable2.LockedWidth = true;
subTable2.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER;
subTable2.AddCell(new Phrase("Examination", time51));
subTable2.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER;
subTable2.AddCell(new Phrase("Board", time51));
subTable2.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER;
subTable2.AddCell(new Phrase("Month and Year of Passing", time51));
subTable2.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER;
subTable2.AddCell(new PdfPCell(new Phrase("Mark", time51)));
PdfPTable nested = new PdfPTable(1);
nested.AddCell("");
nested.AddCell("Obtained");
nested.AddCell("Out of");
PdfPCell nesthousing = new PdfPCell(nested);
nesthousing.Padding = 0f;
subTable2.AddCell(nesthousing);
subTable2.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER;
subTable2.AddCell(new Phrase("Percentage", time51));
subTable2.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER;
subTable2.AddCell(new Phrase("Calss/Grade", time51));
doc.Add(subTable2);
请看SimpleTable12. It creates a table that looks like this: simple_table12.pdf
您仍然可以通过使用填充或引入 descender/ascender 信息来微调单元格的高度,但这些都是细节。重要的是:如何组织细胞?
我写了这个方便的小方法:
public PdfPCell createCell(String content, int colspan, int rowspan, int border) {
PdfPCell cell = new PdfPCell(new Phrase(content, font));
cell.setColspan(colspan);
cell.setRowspan(rowspan);
cell.setBorder(border);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
return cell;
}
您传递一些内容、一个 colspan、一个 rowspan 和边框信息,它会为您创建您需要的 PdfPCell
。这使我们能够创建更易于阅读的代码:
public void createPdf(String dest) throws IOException, DocumentException {
font = new Font(FontFamily.HELVETICA, 10);
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(dest));
document.open();
PdfPTable table = new PdfPTable(8);
table.setWidthPercentage(100);
table.addCell(createCell("Examination", 1, 2, PdfPCell.BOX));
table.addCell(createCell("Board", 1, 2, PdfPCell.BOX));
table.addCell(createCell("Month and Year of Passing", 1, 2, PdfPCell.BOX));
table.addCell(createCell("", 1, 1, PdfPCell.TOP));
table.addCell(createCell("Marks", 2, 1, PdfPCell.TOP));
table.addCell(createCell("Percentage", 1, 2, PdfPCell.BOX));
table.addCell(createCell("Class / Grade", 1, 2, PdfPCell.BOX));
table.addCell(createCell("", 1, 1, PdfPCell.BOX));
table.addCell(createCell("Obtained", 1, 1, PdfPCell.BOX));
table.addCell(createCell("Out of", 1, 1, PdfPCell.BOX));
table.addCell(createCell("12th / I.B. Diploma", 1, 1, PdfPCell.BOX));
table.addCell(createCell("", 1, 1, PdfPCell.BOX));
table.addCell(createCell("", 1, 1, PdfPCell.BOX));
table.addCell(createCell("Aggregate (all subjects)", 1, 1, PdfPCell.BOX));
table.addCell(createCell("", 1, 1, PdfPCell.BOX));
table.addCell(createCell("", 1, 1, PdfPCell.BOX));
table.addCell(createCell("", 1, 1, PdfPCell.BOX));
table.addCell(createCell("", 1, 1, PdfPCell.BOX));
document.add(table);
document.close();
}
如您所见,这是计算正确的 colspan
和 rowspan
值并应用正确的边框的问题。
如何使用 iext sharp 创建下面提到的 table。下面是我的代码,但我很困惑。帮我做一个这样的类型table。 我的代码
var subTable2 = new PdfPTable(new float[] { 100, 100, 100, 100, 100, 100,100,100 });
subTable2.TotalWidth = 510f;
subTable2.LockedWidth = true;
subTable2.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER;
subTable2.AddCell(new Phrase("Examination", time51));
subTable2.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER;
subTable2.AddCell(new Phrase("Board", time51));
subTable2.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER;
subTable2.AddCell(new Phrase("Month and Year of Passing", time51));
subTable2.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER;
subTable2.AddCell(new PdfPCell(new Phrase("Mark", time51)));
PdfPTable nested = new PdfPTable(1);
nested.AddCell("");
nested.AddCell("Obtained");
nested.AddCell("Out of");
PdfPCell nesthousing = new PdfPCell(nested);
nesthousing.Padding = 0f;
subTable2.AddCell(nesthousing);
subTable2.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER;
subTable2.AddCell(new Phrase("Percentage", time51));
subTable2.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER;
subTable2.AddCell(new Phrase("Calss/Grade", time51));
doc.Add(subTable2);
请看SimpleTable12. It creates a table that looks like this: simple_table12.pdf
您仍然可以通过使用填充或引入 descender/ascender 信息来微调单元格的高度,但这些都是细节。重要的是:如何组织细胞?
我写了这个方便的小方法:
public PdfPCell createCell(String content, int colspan, int rowspan, int border) {
PdfPCell cell = new PdfPCell(new Phrase(content, font));
cell.setColspan(colspan);
cell.setRowspan(rowspan);
cell.setBorder(border);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
return cell;
}
您传递一些内容、一个 colspan、一个 rowspan 和边框信息,它会为您创建您需要的 PdfPCell
。这使我们能够创建更易于阅读的代码:
public void createPdf(String dest) throws IOException, DocumentException {
font = new Font(FontFamily.HELVETICA, 10);
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(dest));
document.open();
PdfPTable table = new PdfPTable(8);
table.setWidthPercentage(100);
table.addCell(createCell("Examination", 1, 2, PdfPCell.BOX));
table.addCell(createCell("Board", 1, 2, PdfPCell.BOX));
table.addCell(createCell("Month and Year of Passing", 1, 2, PdfPCell.BOX));
table.addCell(createCell("", 1, 1, PdfPCell.TOP));
table.addCell(createCell("Marks", 2, 1, PdfPCell.TOP));
table.addCell(createCell("Percentage", 1, 2, PdfPCell.BOX));
table.addCell(createCell("Class / Grade", 1, 2, PdfPCell.BOX));
table.addCell(createCell("", 1, 1, PdfPCell.BOX));
table.addCell(createCell("Obtained", 1, 1, PdfPCell.BOX));
table.addCell(createCell("Out of", 1, 1, PdfPCell.BOX));
table.addCell(createCell("12th / I.B. Diploma", 1, 1, PdfPCell.BOX));
table.addCell(createCell("", 1, 1, PdfPCell.BOX));
table.addCell(createCell("", 1, 1, PdfPCell.BOX));
table.addCell(createCell("Aggregate (all subjects)", 1, 1, PdfPCell.BOX));
table.addCell(createCell("", 1, 1, PdfPCell.BOX));
table.addCell(createCell("", 1, 1, PdfPCell.BOX));
table.addCell(createCell("", 1, 1, PdfPCell.BOX));
table.addCell(createCell("", 1, 1, PdfPCell.BOX));
document.add(table);
document.close();
}
如您所见,这是计算正确的 colspan
和 rowspan
值并应用正确的边框的问题。