Category / Section
How to use a large image in header?
1 min read
PdfPageTemplateElement is used for creating a template object for the header. Large images are first drawn in this template, then the template object of the header is applied to PdfDocuement header. You can also reuse the image for all pages in the header. By reusing the image you can reduce page size.
C#
//Creates a new Pdf Document PdfDocument document = new PdfDocument(); //Sets the font PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 20f); //Creates brush PdfBrush brush = PdfBrushes.Black; //Create PdfPageTemplateElement by setting width and height PdfPageTemplateElement template = new PdfPageTemplateElement(510, 100); PdfBitmap image = new PdfBitmap(filePath); //Draw image from the page template graphics template.Graphics.DrawImage(image, 0, 0, template.Width, template.Height); //Draw string from the page template graphics template.Graphics.DrawString("Welcome to First Page", font, brush, 40, 50); //Set image drawn template to document template's top position which makes image to appear in header document.Template.Top = template; //Adds page PdfPage page = document.Pages.Add(); //Save the document and dispose it document.Save("sample.pdf", Response, HttpReadType.Save); document.Close();
Sample Link:
https://www.syncfusion.com/downloads/support/directtrac/general/PdfHeader365101069.zip