C#: // Create a new PdfDocument PdfDocument document = new PdfDocument();
// Add a page PdfPage page=document.Pages.Add();
//Create the pdfgrid PdfGrid pdfGrid = new PdfGrid();
//Add grid columns pdfGrid.Columns.Add(5);
//Add the grid row for (int i = 0; i < 4; i++) pdfGrid.Rows.Add();
//Assign the values to table for (int i = 0; i < 30; i++) { pdfGrid.Rows[i].Cells[0].Value = i.ToString() + " Row ";
}
//Apply the pagination PdfGridLayoutFormat gformat = new PdfGridLayoutFormat(); gformat.Layout = PdfLayoutType.Paginate; gformat.Break = PdfLayoutBreakType.FitPage; gformat.PaginateBounds = new RectangleF(10, 10, page.GetClientSize().Width, page.GetClientSize().Height);
//Draw the table pdfGrid.Draw(page, new PointF(0, 10), gformat);
MemoryStream ms = new MemoryStream();
// Save and close the document. document.Save(ms); document.Close(true);
ms.Position = 0;
//Load the file stream that contains the grid. PdfLoadedDocument ldoc = new PdfLoadedDocument(ms);
//Create the document. PdfDocument finalDoc = new PdfDocument();
//set the margins finalDoc.PageSettings.SetMargins(0f);
for (int i = 0; i < ldoc.Pages.Count; i++) { //Get the templates from loadeddocument. PdfTemplate template = ldoc.Pages[i].CreateTemplate();
//Add the sections PdfSection section1 = finalDoc.Sections.Add(); PdfPage page1 = section1.Pages.Add();
//Draw the templates page1.Graphics.DrawPdfTemplate(template, new PointF(0,0),new SizeF(page1.GetClientSize().Width,page1.GetClientSize().Height));
//Including header to the section section1.Template.Top = AddHeader(finalDoc, DateTime.Now.Date.ToShortDateString()); } MemoryStream stream = new MemoryStream();
//Desired document is saved and opened SaveFileDialog saveDialog = new SaveFileDialog() { DefaultExt = ".pdf", Filter = "Adobe PDF Files(*.pdf)|*.pdf", FilterIndex = 1 }; saveDialog.ShowDialog();
using (Stream stream1 = saveDialog.OpenFile()) { finalDoc.Save(stream); }
//close the document finalDoc.Close(true); ldoc.Close(true); stream.Dispose(); private PdfPageTemplateElement AddHeader(PdfDocument doc, string title) {
RectangleF rect = new RectangleF(0, 0, doc.Pages[0].GetClientSize().Width, 50);
//Create page template PdfPageTemplateElement header = new PdfPageTemplateElement(rect); PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 16, PdfFontStyle.Bold);
//Set formattings for the text PdfStringFormat format = new PdfStringFormat(); format.Alignment = PdfTextAlignment.Center; format.LineAlignment = PdfVerticalAlignment.Middle;
//Draw title header.Graphics.DrawString(title, font, PdfBrushes.Black, new RectangleF(0, 0, header.Width, header.Height), format); header.Graphics.DrawRectangle(PdfPens.Blue, rect);
return header;
|