我有一个列表,我需要将该列表的数据放入pdf文件中。我搜索并找到了itext7库,您可以在其中创建pdf表。我写了以下代码:
var path2 = global::Android.OS.Environment.ExternalStorageDirectory.AbsolutePath;
filePath = Path.Combine(path2.ToString(), "Client's_Account.pdf");
stream = new FileStream(filePath, FileMode.Create);
PdfWriter writer = new PdfWriter(stream);
PdfDocument pdf2 = new iText.Kernel.Pdf.PdfDocument(writer);
Document document = new Document(pdf2,iText.Kernel.Geom.PageSize.TABLOID);
Paragraph header;
acc_info acc = new acc_info();
//foreach (var t in tableItems)
//{
// string txt = t.itembarcode + " " + t.itemsunitcode + " " + t.name + "\n";
// header = new Paragraph(txt);
// document.Add(header);
//}
iText.Layout.Element.Table table = new iText.Layout.Element.Table(5, false);
// table.SetWidth(400).SetFixedLayout();
table.AddCell(new Cell(1, 1).SetTextAlignment(iText.Layout.Properties.TextAlignment.CENTER).Add(new Paragraph("Date")));
table.AddCell(new Cell(1, 2).SetTextAlignment(iText.Layout.Properties.TextAlignment.CENTER).Add(new Paragraph("Reference")));
table.AddCell(new Cell(1, 3).SetTextAlignment(iText.Layout.Properties.TextAlignment.CENTER).Add(new Paragraph("Description")));
table.AddCell(new Cell(1, 4).SetTextAlignment(iText.Layout.Properties.TextAlignment.CENTER).Add(new Paragraph("Debit")));
table.AddCell(new Cell(1, 5).SetTextAlignment(iText.Layout.Properties.TextAlignment.CENTER).Add(new Paragraph("Credit")));
for (int i = 0; i < user.tableItems_accInfo.Count; i++)
{
int j = 0;
table.AddCell(new Cell(i + 2, j + 1).SetTextAlignment(iText.Layout.Properties.TextAlignment.CENTER).Add(new Paragraph(user.tableItems_accInfo[i].jvdate)));
j = j + 1;
table.AddCell(new Cell(i + 2, j + 1).SetTextAlignment(iText.Layout.Properties.TextAlignment.CENTER).Add(new Paragraph(user.tableItems_accInfo[i].jvref)));
j = j + 1;
table.AddCell(new Cell(i + 2, j + 1).SetTextAlignment(iText.Layout.Properties.TextAlignment.CENTER).Add(new Paragraph(user.tableItems_accInfo[i].desc)));
j = j + 1;
table.AddCell(new Cell(i + 2, j + 1).SetTextAlignment(iText.Layout.Properties.TextAlignment.CENTER).Add(new Paragraph(user.tableItems_accInfo[i].jvdebit)));
j = j + 1;
table.AddCell(new Cell(i + 2, j + 1).SetTextAlignment(iText.Layout.Properties.TextAlignment.CENTER).Add(new Paragraph(user.tableItems_accInfo[i].jvcredit)));
}
document.Add(table);
string txt = "\n \n Total Credit= " + tot_crdt.Text +"\n \n Total Debit= "+tot_dbt.Text+ "\n \n Total Balance= "+balance.Text;
header = new Paragraph(txt);
document.Add(header);
document.Close();
Toast.MakeText(this.Context, "PDF Generated", ToastLength.Short).Show();
但是即使更改pdf页面大小(A4,合法,小报...),我得到的表也是如此糟糕。我做错了什么?这是我得到的桌子:在此先感谢
您似乎误解了Cell
构造函数参数。它们不代表该单元格应进入的行和列,而是代表该单元格应跨越的行和列数。
代替
new Cell(n, m)
各种n
和m
,因此,你应该使用
new Cell()
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句