Hi,
I am new to migradoc/pdfsharp, but have successfully created my document. But I have problems in formatting my migradoc. Especially setting margins in Migradoc. The funny thing is, I am able to set top margin successfully but not bottommargin.
Code:
PdfDocument pdf = new PdfDocument();
PdfPage page = pdf.AddPage(template.Pages[0]);
XGraphics gfx = XGraphics.FromPdfPage(page, XGraphicsPdfPageOptions.Append);
gfx.MUH = PdfFontEncoding.Unicode;
XFont font = new XFont("Verdana", 13, XFontStyle.Bold);
//gfx.DrawString("The following paragraph was rendered using MigraDoc:", font, XBrushes.Black,new XRect(0, 0, page.Width, page.Height), XStringFormats.Center);
this.doc = new Document();
//DefineStyles();
//Create invoice table
Section section = doc.AddSection();
PageSetup pageSetup = new PageSetup();
pageSetup.BottomMargin = "5.0cm";
pageSetup.TopMargin = "12cm";
section.PageSetup = pageSetup;
//section.PageSetup.HeaderDistance = "8.0cm";
Paragraph paragraph = new Paragraph();
paragraph.AddTab();
paragraph.AddPageField();
// Add paragraph to footer for odd pages.
section.Footers.Primary.Add(paragraph);
section.PageSetup.FooterDistance = "4.0cm";
//section.PageSetup.DifferentFirstPageHeaderFooter = true;
Table table = section.AddTable();
table.Style = "Table";
table.Borders.Color = Colors.Gray;
table.Borders.Visible = false;
table.Borders.Width = 0.25;
table.Borders.Left.Width = 0.5;
table.Borders.Right.Width = 0.5;
table.Rows.LeftIndent = 0;
//Add cols before rows
Column column = table.AddColumn("1cm");
column.Format.Alignment = ParagraphAlignment.Center;
column = table.AddColumn("7cm");
column.Format.Alignment = ParagraphAlignment.Left;
column = table.AddColumn("2cm");
column.Format.Alignment = ParagraphAlignment.Right;
column = table.AddColumn("2cm");
column.Format.Alignment = ParagraphAlignment.Right;
column = table.AddColumn("1cm");
column.Format.Alignment = ParagraphAlignment.Right;
column = table.AddColumn("2cm");
column.Format.Alignment = ParagraphAlignment.Right;
column = table.AddColumn("2cm");
column.Format.Alignment = ParagraphAlignment.Right;
//Create header row
Row row = table.AddRow();
row.HeadingFormat = true;
row.Format.Font.Bold = true;
row.Shading.Color = Colors.LightGray;
row.HeightRule = RowHeightRule.Exactly;
row.Height = "0.5cm";
row.Borders.Style = BorderStyle.None;
row.Cells[0].AddParagraph("No");
row.Cells[1].AddParagraph("Description");
row.Cells[2].AddParagraph("Price");
row.Cells[3].AddParagraph("Quantity");
row.Cells[4].AddParagraph("VAT");
row.Cells[5].AddParagraph("Amount");
row.Cells[6].AddParagraph("Currency");
int rowCount = 1;
Decimal netSum = 0;
Decimal vatSum = 0;
Decimal grossSum = 0;
foreach (RCInvoiceDataRecord data in ssData)
{
row = table.AddRow();
row.HeightRule = RowHeightRule.Auto;
//row.Height = "1cm";
rowCount++;
row.Cells[0].AddParagraph(data.ssSTInvoiceData.ssNo.ToString());
row.Cells[1].AddParagraph(data.ssSTInvoiceData.ssDescription);
row.Cells[2].AddParagraph(data.ssSTInvoiceData.ssPrice.ToString());
row.Cells[3].AddParagraph(data.ssSTInvoiceData.ssQuantity.ToString());
row.Cells[4].AddParagraph(data.ssSTInvoiceData.ssVAT.ToString());
row.Cells[5].AddParagraph(data.ssSTInvoiceData.ssAmount.ToString());
row.Cells[6].AddParagraph(data.ssSTInvoiceData.ssCurrency);
netSum = netSum + data.ssSTInvoiceData.ssAmount;
}
table.SetEdge(0, 0, 7, rowCount, Edge.Box, BorderStyle.Single, 0.75, Color.Empty);
//Add Net Sum Row
row = table.AddRow();
row.Borders.Visible = false;
row.Cells[0].AddParagraph("Net Sum");
row.Cells[0].Format.Font.Bold = false;
row.Cells[0].Format.Alignment = ParagraphAlignment.Right;
row.Cells[0].MergeRight = 4;
row.Cells[5].AddParagraph(netSum.ToString("0.00") + " " + ssCurrency);
row.Cells[5].MergeRight = 1;
//Add Sum VAT Row
row = table.AddRow();
row.Borders.Visible = false;
row.Cells[0].AddParagraph("Sum VAT");
row.Cells[0].Format.Font.Bold = false;
row.Cells[0].Format.Alignment = ParagraphAlignment.Right;
row.Cells[0].MergeRight = 4;
row.Cells[5].AddParagraph(vatSum.ToString("0.00") + " " + ssCurrency);
row.Cells[5].MergeRight = 1;
//Add Gross Sum Row
row = table.AddRow();
row.Borders.Visible = false;
row.Cells[0].AddParagraph("Gross Sum");
row.Cells[0].Format.Font.Bold = true;
row.Cells[0].Format.Alignment = ParagraphAlignment.Right;
row.Cells[0].MergeRight = 4;
row.Cells[5].AddParagraph(netSum.ToString("0.00") + " " + ssCurrency);
row.Cells[5].MergeRight = 1;
row.Cells[5].Format.Font.Bold = true;
// Create a renderer and prepare (=layout) the document
MigraDoc.Rendering.DocumentRenderer docRenderer = new DocumentRenderer(doc);
docRenderer.PrepareDocument();
// Render the paragraph. You can render tables or shapes the same way.
//docRenderer.RenderObject(gfx, XUnit.FromCentimeter(2.5), XUnit.FromCentimeter(14), "2cm", table);
//docRenderer.RenderObject(gfx, XUnit.FromCentimeter(16), XUnit.FromCentimeter(25), "11cm", paragraph.Clone());
gfx.Dispose();
int pages = docRenderer.FormattedDocument.PageCount;
for (int i = 1; i <= pages; ++i)
{
PdfPage curr_page = pdf.AddPage(template.Pages[0]);
PageInfo pageInfo = docRenderer.FormattedDocument.GetPageInfo(i);
curr_page.Width = pageInfo.Width;
curr_page.Height = pageInfo.Height;
page.Orientation = pageInfo.Orientation;
using (XGraphics gfx1 = XGraphics.FromPdfPage(curr_page))
{
// HACKĀ²
gfx1.MUH = PdfFontEncoding.Unicode;
//gfx1.MFEH = PdfFontEmbedding.Default;
docRenderer.RenderPage(gfx1, i);
}
}
ssPDF_Output = pdf;
- does not work as i see the table in this section overlaps with the footer of my default template. Will be great if someone can help me. Thanks and have a nice day
Pav