We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Adding header to first page of PDF only

I have created a document and successfully added it to the page.
However the initial code below adds it to all the pages, the second block adds it to all but the first page.
I need to have the header on the first page only??

mxPage = mxDocument.Pages.Add();

RectangleF rect = new RectangleF(0, 0, doc.Pages[0].GetClientSize().Width, 192);


//Create page template

PdfPageTemplateElement header = new PdfPageTemplateElement(rect);

string physicalPath = Path.Combine(HostingEnvironment.WebRootPath, "images", "pdf_banner_210mm.jpg");

Stream file2 = new FileStream(physicalPath, FileMode.Open, FileAccess.Read, FileShare.Read);

PdfImage img = new PdfBitmap(file2);

//Draw the image in the Header.

header.Graphics.DrawImage(img, 0, 0, imageWidth, imageHeight);

file2.Dispose();

//Add header template at the top.

mxDocument.Template.Top = header; //Adds to ALL pages



I have tried the following:

PdfSection section1 = mxDocument.Sections.Add();

section1.Pages.Add();

section1.Pages.Add();

.........>

section1.Template.Top = header; //Adds to All but the First Page.




           


7 Replies

PV Prakash Viswanathan Syncfusion Team February 6, 2017 12:06 PM UTC

Hi Keith, 

Thanks for contacting Syncfusion support. 

We can add header for first page only using PdfSection. By adding the first page in one section and other pages to different section you can achieve your requirement. 

Refer below code snippet, 
PdfSection section1 = mxDocument.Sections.Add(); 
section1.Pages.Add(); 
 
//add new section and pages 
PdfSection section2 = mxDocument.Sections.Add(); 
section2.Pages.Add(); 
 
//Add header to first page section 
section1.Template.Top = header; 

Please let us know if you need any further assistance on this. 

Regards, 
Prakash V. 



KE Keith February 6, 2017 03:03 PM UTC

Thanks for your reply, this is a sample of my code using your code snipet:

This now gives me the first two pages with no header containing my content followed by two more pages (the first with a header the second without).

The content is obviously being written to the document prior to the sections?????

Hope this makes sense.

Public PdfDocument CreateDocument()
        {
//Creating new PDF document instance
mxDocument = new PdfDocument();
mxPageSize = mxDocument.PageSettings.Size;

//Setting margin
mxDocument.PageSettings.Margins.All = 0;

//Adding a new page
mxPage = mxDocument.Pages.Add();

//Adding sections
PdfSection section1 = mxDocument.Sections.Add();
section1.Pages.Add();

PdfSection section2 = mxDocument.Sections.Add();
section2.Pages.Add();

//Set graphics
mxGraphics = mxPage.Graphics;

//Adding Header
AddLogo(section1);
                //Add Content
                AddPdfGrid();

.............
//Insert another PdfGrid and loop through list adding content rows
//save document
}



private void AddLogo(PdfSection section)
        {
            RectangleF rect = new RectangleF(0, 0, mxDocument.Pages[0].GetClientSize().Width;, 192);

            //Create page template
            PdfPageTemplateElement header = new PdfPageTemplateElement(rect);

            string physicalPath = Path.Combine(mxHostingEnvironment.WebRootPath, "images", "pdf_banner_210mm.jpg");
            Stream file2 = new FileStream(physicalPath, FileMode.Open, FileAccess.Read, FileShare.Read);
            PdfImage img = new PdfBitmap(file2);

            //Draw the image in the Header.
            header.Graphics.DrawImage(img, 0, 0, imageWidth, imageHeight);
            file2.Dispose();

            //Add header template at the top.
            //mxDocument.Template.Top = header; //Adds to ALL pages
            section.Template.Top = header;
        }

private void AddPdfGrid()
        {
            //Create the grid
            PdfGrid headerGrid = new PdfGrid();

            //Add the rows
            PdfGridRow row1 = headerGrid.Rows.Add();
            PdfGridRow row2 = headerGrid.Rows.Add();

            //Add the column/s
            headerGrid.Columns.Add(1);
            headerGrid.Columns[0].Width = (mxPageSize.Width - 100);

            //Set the value to the specific cell.
            headerGrid.Rows[0].Cells[0].StringFormat = new PdfStringFormat(PdfTextAlignment.Center);
            headerGrid.Rows[0].Cells[0].Value = mxPaperDetails.DocumentTitle;

            headerGrid.Rows[1].Cells[0].StringFormat = new PdfStringFormat(PdfTextAlignment.Center);
            headerGrid.Rows[1].Cells[0].Value = String.Format("{0:dddd d MMMM yyyy}", mxPaperDetails.EventDate);

            //Specify the style for the PdfGridCell.
            PdfGridCellStyle GridCellStyle = new PdfGridCellStyle();

            topRowStyle.Borders.All = PdfPens.Blue;

            //Apply style
            headerGrid.Rows[0].Cells[0].Style = GridCellStyle;

            //Draw the PdfGrid.            
            headerGrid.Draw(mxPage, new PointF(50, 250));
        }


SK Surya Kumar Syncfusion Team February 7, 2017 12:11 PM UTC

 Hi Keith,  
Thanks for your update. 
In your code snippet, you have totally added 3 pages to the document  
                          //Adding a new page 
                        mxPage = mxDocument.Pages.Add(); 
 
                        //Adding sections 
                        PdfSection section1 = mxDocument.Sections.Add(); 
                        section1.Pages.Add(); 
 
                        PdfSection section2 = mxDocument.Sections.Add(); 
                        section2.Pages.Add(); 


You have added content to the first page and header to the next page, followed by an empty page. 
                         //Draw the PdfGrid.             
                          headerGrid.Draw(mxPage, new PointF(50, 250)); 


So, this is the reason for the creation of 3 pages as you mentioned, this can be fixed by writing the header and content to the same page as like below. 
            //Adding sections 
            PdfSection section1 = mxDocument.Sections.Add(); 
            mxPage = section1.Pages.Add(); 
 
            PdfSection section2 = mxDocument.Sections.Add(); 
            section2.Pages.Add(); 
 
Please find the sample from the below mentioned location: 
 
Please let us know if you need any further assistance in this regard. 
 
Regards, 
Surya Kumar. 



PS Pierre Smith August 12, 2021 03:12 PM UTC

There is still a problem with this solution 

if you let the data grid continue to a second page the header still gets added to the second page 


How can you let a data grid span multiple pages with the header only showing on the first page 





GK Gowthamraj Kumar Syncfusion Team August 13, 2021 02:11 PM UTC

Hi Pierre 
 
Thank you for your update.

 
We can add a header on the first page of a PDF document, when the grid data paginate to second page.  Please refer the below KB documentation for more information, 
 
If the above solution doesn’t satisfy your end requirement means, could you please share us the complete code snippet, output document, product version details to check the issue on our end. Thereby, we will check any other feasibilities to achieve your requirement and will share you the more details.  
 
Regards, 
Gowthamraj K 



PS Pierre Smith August 15, 2021 07:47 PM UTC

HI  Gowthamraj 


This Solution seems to be working for me. Thank you for the response with the right solution, much appreciated 


Regards



GK Gowthamraj Kumar Syncfusion Team August 16, 2021 06:01 AM UTC

Hi Pierre, 
 
Thank you for your update. We are glad to know that your problem has been solved. 
 
Please let us know if you need any further assistance with this. 
 
Regards, 
Gowthamraj K 


Loader.
Live Chat Icon For mobile
Up arrow icon