I have a list and I need to put the data of this list in a pdf file. I searched and found the itext7 library where you can create a table in the pdf. I wrote the following code:
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();
but the table I get is so bad even if I change the pdf pagesize (A4, legal, tabloid...). what did I do wrong?
here's the table that I get:
thanks in advance
You seem to misinterpret the Cell
constructor parameters. They don't represent the row and column in which the cell shall go but instead the number of rows and columns the cell shall span.
Instead of
new Cell(n, m)
for various n
and m
, therefore, you should use
new Cell()