Pdfptable 重复 table
Pdfptable getting duplicate table
我觉得这很有趣,但我不知道我卡在哪里。
我正在尝试使用 itextsharp 在 PDF 文档中创建一个 table,并成功创建了 table,如您所见图像
下面是我用来生成 PDF 的代码。
private static void TestPDF()
{
Document document = new Document(iTextSharp.text.PageSize.A4, 60, 60, 10, 0);
try
{
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(@"C:\PdfUpload\" + "testFile" + ".pdf", FileMode.Create));
document.Open();
PdfPTable table = new PdfPTable(new float[] { 4, 16, 4, 10, 4, 10, 4, 10, 10, 10, 4, 10, 10, 7, 6, 8 });
table.TotalWidth = 580;
//table.WidthPercentage = 120;
PdfPCell cell = null;
modifycell(cell, "", ref table, 2, 0);
modifycell(cell, "Formative Asssessment", ref table, 8, 0);
modifycell(cell, "Summetive Assessment", ref table, 3, 0);
modifycell(cell, "Final Result", ref table, 3, 0);
modifycell(cell, "Year", ref table, 0, 2);
modifycell(cell, "Module Name", ref table, 0, 2);
modifycell(cell, "Digital Assessment", ref table, 2, 0);
modifycell(cell, "Group work", ref table, 2, 0);
modifycell(cell, "Assignment", ref table, 2, 0);
modifycell(cell, "Subtotal", ref table, 2, 0);
modifycell(cell, "Examanation", ref table, 3, 0);
modifycell(cell, "Final (100%)", ref table, 0, 2);
modifycell(cell, "Symbol", ref table, 0, 2);
modifycell(cell, "Status (Pass/Fail)", ref table, 0, 2);
modifycell(cell, "%", ref table, 0, 0);
modifycell(cell, "Weighted Controbution (20%)", ref table, 0, 0);
modifycell(cell, "%", ref table, 0, 0);
modifycell(cell, "Weighted Controbution (10%)", ref table, 0, 0);
modifycell(cell, "%", ref table, 0, 0);
modifycell(cell, "Weighted Controbution (30%)", ref table, 0, 0);
modifycell(cell, "Weighted Controbution (60%)", ref table, 0, 0);
modifycell(cell, "Subminimum achieved", ref table, 0, 0);
modifycell(cell, "%", ref table, 0, 0);
modifycell(cell, "Weighted Controbution (40%)", ref table, 0, 0);
modifycell(cell, "Subminimum achieved", ref table, 0, 0);
table.WriteSelectedRows(0, -1, 8, 700, writer.DirectContent);
document.Add(table);
document.Close();
}
catch (Exception oException) { string sMessage = oException.ToString(); }
}
private static void modifycell(PdfPCell cell, string str, ref PdfPTable table, int Colspan = 0, int RowSpan = 0,
int Width = 0)
{
BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.EMBEDDED);
Font font = new Font(bf, 6);
cell = new PdfPCell(new Phrase(str, font));
if (RowSpan > 0)
cell.Rowspan = RowSpan;
if (Colspan > 0)
cell.Colspan = Colspan;
if (Width > 0)
cell.Width = Width;
cell.HorizontalAlignment = Element.ALIGN_CENTER;
cell.VerticalAlignment = Element.ALIGN_MIDDLE;
cell.BackgroundColor = new iTextSharp.text.BaseColor(220, 220, 220);
cell.Border = 1;
cell.BorderColorLeft = BaseColor.BLACK;
cell.BorderWidthLeft = .5f;
cell.BorderColorRight = BaseColor.BLACK;
cell.BorderWidthRight = .5f;
cell.BorderColorBottom = BaseColor.BLACK;
cell.BorderWidthBottom = .5f;
table.AddCell(cell);
}
为什么要添加两次 table?
你用这一行在绝对位置添加一次:
table.WriteSelectedRows(0, -1, 8, 700, writer.DirectContent);
然后用这一行再次添加它:
document.Add(table);
添加两次 table 不足为奇。下定决心,删除其中一行。
我觉得这很有趣,但我不知道我卡在哪里。 我正在尝试使用 itextsharp 在 PDF 文档中创建一个 table,并成功创建了 table,如您所见图像
下面是我用来生成 PDF 的代码。
private static void TestPDF()
{
Document document = new Document(iTextSharp.text.PageSize.A4, 60, 60, 10, 0);
try
{
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(@"C:\PdfUpload\" + "testFile" + ".pdf", FileMode.Create));
document.Open();
PdfPTable table = new PdfPTable(new float[] { 4, 16, 4, 10, 4, 10, 4, 10, 10, 10, 4, 10, 10, 7, 6, 8 });
table.TotalWidth = 580;
//table.WidthPercentage = 120;
PdfPCell cell = null;
modifycell(cell, "", ref table, 2, 0);
modifycell(cell, "Formative Asssessment", ref table, 8, 0);
modifycell(cell, "Summetive Assessment", ref table, 3, 0);
modifycell(cell, "Final Result", ref table, 3, 0);
modifycell(cell, "Year", ref table, 0, 2);
modifycell(cell, "Module Name", ref table, 0, 2);
modifycell(cell, "Digital Assessment", ref table, 2, 0);
modifycell(cell, "Group work", ref table, 2, 0);
modifycell(cell, "Assignment", ref table, 2, 0);
modifycell(cell, "Subtotal", ref table, 2, 0);
modifycell(cell, "Examanation", ref table, 3, 0);
modifycell(cell, "Final (100%)", ref table, 0, 2);
modifycell(cell, "Symbol", ref table, 0, 2);
modifycell(cell, "Status (Pass/Fail)", ref table, 0, 2);
modifycell(cell, "%", ref table, 0, 0);
modifycell(cell, "Weighted Controbution (20%)", ref table, 0, 0);
modifycell(cell, "%", ref table, 0, 0);
modifycell(cell, "Weighted Controbution (10%)", ref table, 0, 0);
modifycell(cell, "%", ref table, 0, 0);
modifycell(cell, "Weighted Controbution (30%)", ref table, 0, 0);
modifycell(cell, "Weighted Controbution (60%)", ref table, 0, 0);
modifycell(cell, "Subminimum achieved", ref table, 0, 0);
modifycell(cell, "%", ref table, 0, 0);
modifycell(cell, "Weighted Controbution (40%)", ref table, 0, 0);
modifycell(cell, "Subminimum achieved", ref table, 0, 0);
table.WriteSelectedRows(0, -1, 8, 700, writer.DirectContent);
document.Add(table);
document.Close();
}
catch (Exception oException) { string sMessage = oException.ToString(); }
}
private static void modifycell(PdfPCell cell, string str, ref PdfPTable table, int Colspan = 0, int RowSpan = 0,
int Width = 0)
{
BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.EMBEDDED);
Font font = new Font(bf, 6);
cell = new PdfPCell(new Phrase(str, font));
if (RowSpan > 0)
cell.Rowspan = RowSpan;
if (Colspan > 0)
cell.Colspan = Colspan;
if (Width > 0)
cell.Width = Width;
cell.HorizontalAlignment = Element.ALIGN_CENTER;
cell.VerticalAlignment = Element.ALIGN_MIDDLE;
cell.BackgroundColor = new iTextSharp.text.BaseColor(220, 220, 220);
cell.Border = 1;
cell.BorderColorLeft = BaseColor.BLACK;
cell.BorderWidthLeft = .5f;
cell.BorderColorRight = BaseColor.BLACK;
cell.BorderWidthRight = .5f;
cell.BorderColorBottom = BaseColor.BLACK;
cell.BorderWidthBottom = .5f;
table.AddCell(cell);
}
为什么要添加两次 table?
你用这一行在绝对位置添加一次:
table.WriteSelectedRows(0, -1, 8, 700, writer.DirectContent);
然后用这一行再次添加它:
document.Add(table);
添加两次 table 不足为奇。下定决心,删除其中一行。