Hi I'm trying to generate a pdf with some data (header, paragraph, table, footer etc.) using PDFSharp+Migradoc where the table data is taken from a WPF datagrid. But whenever there are many rows in the table the footer text is overlapping with table data and if the PDF goes to next page then header text is overlapping with table data (sample pdf:
https://filebin.net/u78dhz0ngwhh1pdh)
How do I fix this?
What I have tried:
void AddHeader(MigraDoc.DocumentObjectModel.Section se, string text, int size, bool addBorder = false)
{
MigraDoc.DocumentObjectModel.Paragraph pa = se.Headers.Primary.AddParagraph();
pa.AddFormattedText(text, TextFormat.Bold);
pa.Format.Alignment = ParagraphAlignment.Center;
pa.Format.Font.Name = "Times New Roman";
pa.Format.Font.Size = size;
pa.Format.Font.Color = MigraDoc.DocumentObjectModel.Colors.Blue;
if (addBorder)
{
pa.Format.Borders.Bottom = new MigraDoc.DocumentObjectModel.Border() { Width = "0.5pt", Color = MigraDoc.DocumentObjectModel.Colors.DarkGray };
}
}
void AddParagraph(MigraDoc.DocumentObjectModel.Section se, string text, ParagraphAlignment alignment, int size)
{
MigraDoc.DocumentObjectModel.Paragraph p = se.AddParagraph();
p.AddFormattedText(text);
p.Format.Alignment = alignment;
p.Format.Font.Name = "Times New Roman";
p.Format.Font.Size = size;
}
void AddParagraph(MigraDoc.DocumentObjectModel.HeaderFooter hf, string text, ParagraphAlignment alignment, int size)
{
MigraDoc.DocumentObjectModel.Paragraph p = hf.AddParagraph();
p.AddFormattedText(text);
p.Format.Alignment = alignment;
p.Format.Font.Name = "Times New Roman";
p.Format.Font.Size = size;
}
MigraDoc.DocumentObjectModel.Tables.Table CreateTable(double[] columnWidths)
{
MigraDoc.DocumentObjectModel.Tables.Table tab = new MigraDoc.DocumentObjectModel.Tables.Table();
CreateTableAndAddColumns(tab, columnWidths);
return tab;
}
void AddRowWithHeaders(MigraDoc.DocumentObjectModel.Tables.Table tab, string[] headers)
{
MigraDoc.DocumentObjectModel.Tables.Row rw = tab.AddRow();
rw.Shading.Color = MigraDoc.DocumentObjectModel.Colors.PaleGoldenrod;
for (int i = 0; i < headers.Length; i++)
{
MigraDoc.DocumentObjectModel.Tables.Cell cl = rw.Cells[i];
cl.AddParagraph(headers[i]);
}
}
void AddRowWithData(MigraDoc.DocumentObjectModel.Tables.Table tab, Entry entry)
{
MigraDoc.DocumentObjectModel.Tables.Row rw = tab.AddRow();
rw.Format.Alignment = ParagraphAlignment.Left;
rw.Format.Font.Size = 9;
MigraDoc.DocumentObjectModel.Tables.Cell cl;
cl = rw.Cells[0];
cl.AddParagraph(entry.SlNo.ToString());
cl = rw.Cells[1];
cl.AddParagraph(entry.Description);
cl = rw.Cells[2];
cl.AddParagraph(entry.UnitString);
cl = rw.Cells[3];
cl.AddParagraph(entry.UOM);
cl = rw.Cells[4];
cl.AddParagraph(entry.RateString);
cl = rw.Cells[5];
cl.AddParagraph(entry.Amount.ToString("F2", CultureInfo.InvariantCulture));
}
void AddTotalTable(MigraDoc.DocumentObjectModel.Document doc, double[] columnWidths, string total)
{
MigraDoc.DocumentObjectModel.Tables.Table tTab = CreateTable(columnWidths);
MigraDoc.DocumentObjectModel.Tables.Row _row = tTab.AddRow();
_row.Format.Font.Bold = true;
_row.Cells[0].MergeRight = 3;
MigraDoc.DocumentObjectModel.Tables.Cell _cell = _row.Cells[0];
_cell.Format.Alignment = ParagraphAlignment.Right;
_cell.AddParagraph("Total :");
_cell = _row.Cells[4];
_cell.AddParagraph(total);
doc.LastSection.Add(tTab);
}
void AddSpacer(MigraDoc.DocumentObjectModel.Section se, int size)
{
MigraDoc.DocumentObjectModel.Paragraph p = se.AddParagraph();
p.Format.SpaceAfter = size;
}
void CreateTableAndAddColumns(MigraDoc.DocumentObjectModel.Tables.Table table, double[] columnWidths)
{
table.Borders.Width = 0.75;
table.Rows.Alignment = RowAlignment.Center;
foreach (double width in columnWidths)
{
table.AddColumn(Unit.FromCentimeter(width));
}
}
private void GenPreview(object parameter)
{
MigraDoc.DocumentObjectModel.Document doc = new MigraDoc.DocumentObjectModel.Document();
MigraDoc.DocumentObjectModel.Section se = doc.AddSection();
AddHeader(se, "Amceable Ork, Inc.", 17);
AddHeader(se, "A UNIT OF UNITED ASPOSE LLC", 15);
AddHeader(se, MySelectedItem, 12);
AddHeader(se, "A17-2/NEW SIREN ROAD (WEST), MUMABI-11", 12, true);
AddSpacer(se, 10);
se.AddParagraph();
AddParagraph(se, "\nDate: "+ DateTime.Now.ToString("dd-MMM-yyyy")+" \n\n", ParagraphAlignment.Right, 10);
AddParagraph(se, "To\nM/s T@@@@@@@@@@@\nGSTIN: 19#########\nKOLKATA\n\n\n\nDear Sir,\n\nPlease find enclosed herewith the payment for Rs. 999999999999999999999 through IMPS of your following bills\n\n", ParagraphAlignment.Left, 12);
MigraDoc.DocumentObjectModel.Tables.Table tab = CreateTable(new double[] {1.5, 7.5, 1.5, 1.5, 2, 2.5});
AddRowWithHeaders(tab, new string[] {"SL No.", "Description", "Unit", "UOM", "Rate", "Amount"});
foreach (Entry entry in Entries)
{
AddRowWithData(tab, entry);
}
doc.LastSection.Add(tab);
AddTotalTable(doc, new double[] {1.5, 7.5, 1.5, 1.5, 2, 2.5}, "55555555.00");
se.Footers.Primary.AddParagraph();
se.Footers.Primary.AddParagraph();
se.Footers.Primary.AddParagraph();
se.Footers.Primary.AddParagraph();
AddParagraph(se.Footers.Primary, "Kindly acknowledge receipt of the same.\n\nThanking you,\n\nYours faithfully,\nfor AMCEABLE ORK, INC.\n\n\n(M. K. LODHA)", ParagraphAlignment.Left, 12);
PdfDocumentRenderer pdfRenderer = new PdfDocumentRenderer(false);
pdfRenderer.Document = doc;
pdfRenderer.RenderDocument();
pdfRenderer.PdfDocument.Save("file.pdf");
Process.Start("file.pdf");
}