将列表数据插入 itext7 pdf 中的 table (xamarin android)

inserting list data into table in itext7 pdf (xamarin android)

我有一个列表,我需要把这个列表的数据放在一个pdf文件中。我搜索并找到了 itext7 库,您可以在其中创建 pdf 中的 table。我写了下面的代码:

 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、法律、小报...),我得到的 table 也很糟糕。我做错了什么? 这是我得到的 table: 提前致谢

您似乎误解了 Cell 构造函数参数。它们不表示单元格应位于的行和列,而是表示单元格应跨越的行数和列数。

而不是

new Cell(n, m)

对于各种nm,因此,您应该使用

new Cell()