Hi,
I want to export pdf with multiple tables. table locations should be dynamic (not fixed) means one table below the another table regardless of number of records in first table. number of tables can be 20-25.
To acheive this, I tried to bind these tables inside parent PdfGrid. But it does not display content of child PdfGrids.
I have provided my code for better understanding of my problem.
using (PdfDocument document = new PdfDocument())
{
var headerStyle = new PdfGridCellStyle();
headerStyle.Font = new PdfTrueTypeFont(new Font("Times New Roman", 10, FontStyle.Bold));
headerStyle.CellPadding = new PdfPaddings(2, 2, 2, 2);
var cellStyle = new PdfGridCellStyle();
cellStyle.Font = new PdfTrueTypeFont(new Font("Times New Roman", 10, FontStyle.Regular));
cellStyle.CellPadding = new PdfPaddings(2, 2, 2, 2);
document.PageSettings.Margins.All = 10;
document.PageSettings.Orientation = PdfPageOrientation.Landscape;
PdfPage page = document.Pages.Add();
if (document.Pages.Count > 0)
{
RectangleF bounds = new RectangleF(0, 0, page.GetClientSize().Width, 100);
PdfPageTemplateElement header = new PdfPageTemplateElement(bounds);
PdfBitmap image = new PdfBitmap(Server.MapPath("~/Content/cdn/logo/mahaveer-excel-header.jpg"));
header.Graphics.DrawImage(image, 0, 0);
document.Template.Top = header;
}
PdfGrid parentPdfGrid = new PdfGrid();
var parentPdfRow = 0;
parentPdfGrid.Style.AllowHorizontalOverflow = true;
parentPdfGrid.Columns.Add(1);
foreach (var account in model.Accounts)
{
var column = 0;
PdfGrid headerPdfGrid = new PdfGrid();
headerPdfGrid.Columns.Add(3);
//Add header
headerPdfGrid.Headers.Add(1);
PdfGridRow headerPdfGrid_Header = headerPdfGrid.Headers[0];
headerPdfGrid_Header.Cells[column].Value = "Group Ledger";
headerPdfGrid_Header.Cells[column].ColumnSpan = 2;
headerPdfGrid_Header.Cells[column].StringFormat.Alignment = PdfTextAlignment.Center;
headerPdfGrid_Header.Cells[++column].Value = "Report Date: " + DateTime.UtcNow.ToString("dd-MMM-yyyy");
headerPdfGrid_Header.Cells[column].StringFormat.Alignment = PdfTextAlignment.Center;
headerPdfGrid_Header.Height = 15f;
headerPdfGrid_Header.ApplyStyle(headerStyle);
column = 0;
PdfGridRow headerPdfGrid_Row = headerPdfGrid.Rows.Add();
headerPdfGrid_Row.Cells[column].Value = "From: " + request.From.ToString("dd-MMM-yyyy");
headerPdfGrid_Row.Cells[++column].Value = "To: " + request.To.ToString("dd-MMM-yyyy");
headerPdfGrid_Header.Cells[column].ColumnSpan = 2;
headerPdfGrid_Row.Height = 15f;
headerPdfGrid_Row.ApplyStyle(cellStyle);
column = 0;
headerPdfGrid_Row = headerPdfGrid.Rows.Add();
headerPdfGrid_Row.Cells[column].Value = "Group: " + model.AccountMasterName;
headerPdfGrid_Row.Cells[++column].Value = "Account Code: " + account.AccountNumber.ToString();
headerPdfGrid_Header.Cells[column].ColumnSpan = 2;
headerPdfGrid_Row.Height = 15f;
headerPdfGrid_Row.ApplyStyle(cellStyle);
column = 0;
headerPdfGrid_Row = headerPdfGrid.Rows.Add();
headerPdfGrid_Row.Cells[column].Value = "Account: " + account.Account;
headerPdfGrid_Header.Cells[column].ColumnSpan = 3;
headerPdfGrid_Row.Height = 15f;
headerPdfGrid_Row.ApplyStyle(cellStyle);
//headerPdfGrid.Draw(page, new PointF(10, 10));
PdfGridRow parentRow = parentPdfGrid.Rows.Add();
parentRow.Cells[0].Value = headerPdfGrid; // NOTE: - This binding is not happening
//Tried below thing as well
//parentPdfGrid.Rows[parentPdfRow++].Cells[0].Value = headerPdfGrid;
//parentRow = parentPdfGrid.Rows.Add();
//parentRow.Cells[0].Value = "";
PdfGrid pdfGrid = new PdfGrid();
pdfGrid.Columns.Add(7);
column = 0;
//Add header
pdfGrid.Headers.Add(1);
PdfGridRow pdfGridHeader = pdfGrid.Headers[0];
pdfGridHeader.Cells[column].Value = "Date";
pdfGridHeader.Cells[column].StringFormat.Alignment = PdfTextAlignment.Center;
pdfGridHeader.Cells[++column].Value = "Type";
pdfGridHeader.Cells[column].StringFormat.Alignment = PdfTextAlignment.Center;
pdfGridHeader.Cells[++column].Value = "Code";
pdfGridHeader.Cells[column].StringFormat.Alignment = PdfTextAlignment.Center;
pdfGridHeader.Cells[++column].Value = "Narration";
pdfGridHeader.Cells[column].StringFormat.Alignment = PdfTextAlignment.Center;
pdfGridHeader.Cells[++column].Value = "Debit";
pdfGridHeader.Cells[column].StringFormat.Alignment = PdfTextAlignment.Center;
pdfGridHeader.Cells[++column].Value = "Credit";
pdfGridHeader.Cells[column].StringFormat.Alignment = PdfTextAlignment.Center;
pdfGridHeader.Cells[++column].Value = "Balance";
pdfGridHeader.Cells[column].StringFormat.Alignment = PdfTextAlignment.Center;
pdfGridHeader.Height = 15f;
pdfGridHeader.ApplyStyle(headerStyle);
foreach (var entry in account.Entries)
{
column = 0;
PdfGridRow pdfGridRow = pdfGrid.Rows.Add();
pdfGridRow.Cells[column].Value = entry.Date.ToString("dd-MMM-yyyy");
pdfGridRow.Cells[++column].Value = entry.VoucherType;
pdfGridRow.Cells[++column].Value = entry.VoucherCode;
pdfGridRow.Cells[++column].Value = entry.Narration;
pdfGridRow.Cells[++column].Value = entry.Debit > 0 ? entry.Debit.ToString() : string.Empty;
pdfGridRow.Cells[++column].Value = entry.Debit > 0 ? entry.Debit.ToString() : string.Empty;
pdfGridRow.Cells[++column].Value = $"{Math.Abs(entry.ClosingBalance)} ({(entry.ClosingBalance < 0 ? "Cr" : "Dr")})";
pdfGridRow.Height = 15f;
pdfGridRow.ApplyStyle(cellStyle);
}
//pdfGrid.Draw(page, new PointF(10, 10));
parentRow = parentPdfGrid.Rows.Add();
parentRow.Cells[0].Value = pdfGrid; // NOTE: - This binding is not happening
//Tried below thing as well
//parentPdfGrid.Rows[parentPdfRow++].Cells[0].Value = pdfGrid;
}
parentPdfGrid.RepeatHeader = true;
parentPdfGrid.AllowRowBreakAcrossPages = true;
parentPdfGrid.Draw(page, new PointF(10, 110));
document.Save($"Group Ledger.pdf", HttpContext.ApplicationInstance.Response, HttpReadType.Save);
Thanks & Regards,
Suchita
|
I want to export pdf with multiple tables. table locations should be dynamic (not fixed) means one table below the another table regardless of number of records in first table. number of tables can be 20-25. |
The Essential PDF supports maintaining the position of a PDF grid drawn on PDF page using PdfGridLayoutResult. It provides the rendered bounds of previously added grid, which can be used to place successive elements without overlapping. You can add multiple PDF grids using the bottom position of previously rendered PDF grid.
Please refer the below link for more information,
|
|
To acheive this, I tried to bind these tables inside parent PdfGrid. But it does not display content of child PdfGrids.
|
We can create nested Grid in a PDF document by using PDF library. Please refer the below link for more information,
Please let us know if you need any further assistance with this.
|
Thank you so much Gowthamraj K.
Links helped me to resolve my problems.
Thanks,
Suchita