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. (Last updated on: November 16, 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

Adding header to first page of PDF only

Thread ID:

Created:

Updated:

Platform:

Replies:

128681 Feb 3,2017 02:46 PM UTC Feb 7,2017 12:11 PM UTC ASP.NET Web Forms 3
loading
Tags: PDF
Keith
Asked On February 3, 2017 02:46 PM UTC

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.




           


Prakash Viswanathan [Syncfusion]
Replied On 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. 


Keith
Replied On 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));
        }

Surya Kumar [Syncfusion]
Replied On 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. 


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

;