Category / Section
How to add different headers in automatically created pages
3 mins read
This article describes about to add different header in automatically created page. We can add different header with the help of PdfSection and PdfLayoutType. By using PDF section we can create one or more page with unique page header and using PdfLayoutType we can set text to draw in only first page or upcoming next pages.
The following assemblies are needed to be add the reference of your application.
Syncfusion assemblies:
- Syncfusion.Pdf.Base.dll
- Syncfusion.Compression.Base.dll
The same has been explained in the following code example.
C#:
private void button1_Click(object sender, EventArgs e) { //Create a new PDF document. PdfDocument doc = new PdfDocument(); //Add section PdfSection section1 = doc.Sections.Add(); //Add a page. PdfPage page = section1.Pages.Add(); //Create a header and draw the image. RectangleF rect = new RectangleF(0, 0, doc.Pages[0].GetClientSize().Width, 50); //Initialize new instance for header PdfPageTemplateElement header = new PdfPageTemplateElement(rect); PdfImage image = new PdfBitmap("../../Data/logo.png"); //Draw the image in the header. header.Graphics.DrawImage(image, new PointF(0, 0), new SizeF(200, 50)); //Add the header at the top. section1.Template.Top = header; //Read RTF document. StreamReader reader = new StreamReader("../../Data/input.rtf", Encoding.ASCII); string text = reader.ReadToEnd(); //Close reader reader.Close(); //Convert it to metafile. PdfMetafile metafile = (PdfMetafile)PdfImage.FromRtf(text, page.GetClientSize().Width, PdfImageType.Metafile); PdfMetafileLayoutFormat format = new PdfMetafileLayoutFormat(); // Set text to draw in first page only. format.Layout = PdfLayoutType.OnePage; //Draws image. metafile.Draw(page, 0, 50, format); //Create section to draw remaining content of text PdfSection subSection1 = doc.Sections.Add(); //Add a page PdfPage subPage1 = subSection1.Pages.Add(); //Set font PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 15); //Create a header and draw the string. PdfPageTemplateElement header1 = new PdfPageTemplateElement(rect); header1.Graphics.DrawString("Header demo", font, PdfBrushes.Red, new PointF(0, 0)); //Add the header at the top. subSection1.Template.Top = header1; // Set the text to paginate and draw in all pages. format.Layout = PdfLayoutType.Paginate; // Drawing the text in the page metafile.Draw(subPage1, 0, 50, format); // Removing the first page since it is already printed with header subSection1.Pages.Remove(subSection1.Pages[0]); //Save and close the document. doc.Save("Output.pdf"); doc.Close(true); //Open the saved pdf document System.Diagnostics.Process.Start("Output.pdf"); }
Sample:
http://www.syncfusion.com/downloads/support/directtrac/general/ze/Sample1469689121.zip