Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Tue Apr 23, 2019 10:54 am

Hi Supportteam,

how can I change the Header/Footer when the page orientation changes between pages?

Explanation:
I have a document that starts with a portrait orientated page having a grid with multiple pages and then i a add a new page switch the orientation to landscape and add an other grid with muliple pages.

Problem:
PdfPageTemplateElement can only set on document level and not on page level but requires a fixed height and width.

How can I add a dynamic Header and Footer area for this setting?

Looking forward to your suggestions!

PrintPro
 
Posts: 55
Joined: Tue Apr 16, 2019 6:17 pm

Wed Apr 24, 2019 8:10 am

Hello,

Thanks for your inquiry.
Please refer to the following sample code to add Header and Footer when the page orientation changes between pages. If there is any question, please provide your testing file as well as your desired effect of output, then we will look into it and guide you accordingly. You could send your file to us via email([email protected]).
Code: Select all
  static void Main(string[] args)
    {
       PdfDocument existingPdf = new PdfDocument();
       existingPdf.LoadFromFile(@"test.pdf");
       //create a new PDF file
       PdfDocument newPdf = new PdfDocument();
       PdfMargins margins = new PdfMargins(90, 72, 90, 72);
       foreach (PdfPageBase page in existingPdf.Pages)
       {
           PdfPageBase newPage = newPdf.Pages.Add(page.Size, new PdfMargins(0));
           newPdf.Template.Top = CreateHeaderTemplate(newPdf,page, margins);
           newPdf.Template.Bottom = CreateFooterTemplate(newPdf,page, margins);
           newPdf.Template.Left = new PdfPageTemplateElement(margins.Left, newPdf.PageSettings.Size.Height);
           newPdf.Template.Right = new PdfPageTemplateElement(margins.Right, newPdf.PageSettings.Size.Height);
           newPage.Canvas.DrawTemplate(page.CreateTemplate(), new PointF(-margins.Left, -margins.Top));
           }
       newPdf.SaveToFile("result.pdf");
    }
    //create template of header
    static PdfPageTemplateElement CreateHeaderTemplate(PdfDocument doc,PdfPageBase page, PdfMargins margins)
    {
            SizeF pageSize = page.Size;
            PdfPageTemplateElement headerSpace = new PdfPageTemplateElement(pageSize.Width, margins.Top);
            headerSpace.Foreground = false;
            float x = margins.Left;
            float y = 0;
            //draw picture in headerSpace
            PdfImage headerImage = PdfImage.FromFile(@"logo.png");
            float width = headerImage.Width / 3;
            float height = headerImage.Height / 3;
            headerSpace.Graphics.DrawImage(headerImage, x, margins.Top - height - 2, width, height);
            //draw line in headerSpace
            PdfPen pen = new PdfPen(PdfBrushes.Gray, 0.5f);
            headerSpace.Graphics.DrawLine(pen, x, y + margins.Top - 2, pageSize.Width - x, y + margins.Top - 2);
            return headerSpace;
       
    }
    //create template of footer
    static PdfPageTemplateElement CreateFooterTemplate(PdfDocument doc,PdfPageBase page, PdfMargins margins)
    {
           SizeF pageSize = page.Size;
           PdfPageTemplateElement footerSpace  = new PdfPageTemplateElement(pageSize.Width, margins.Bottom);
            footerSpace.Foreground = false;
            float x = margins.Left;
            float y = 0;
            //draw line in footerSpace
            PdfPen pen = new PdfPen(PdfBrushes.Gray, 0.5f);
            footerSpace.Graphics.DrawLine(pen, x, y, pageSize.Width - x, y);
            //draw text in footerSpace
            y = y + 5;
            PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 10f, FontStyle.Bold), true);
            PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Left);
            String footerText = "test";
            footerSpace.Graphics.DrawString(footerText, font, PdfBrushes.Black, x, y, format);
            return footerSpace;
 }

Sincerely,
Lisa
E-iceblue support team
User avatar

Lisa.Li
 
Posts: 1536
Joined: Wed Apr 25, 2018 3:20 am

Wed Apr 24, 2019 2:08 pm

Hi Lisa,
Great! Your code snippet leads me to the solution.
1. Create document and add all pages but set the margins so that we have space for the header/footer.
2. Save and reload the document (resets all margins to zero), so i can put new elements on any place.

This is even better in terms of freedom then using the PdfPageTemplateElement.

Thank's for your support.

Code: Select all
string headerText = "Page: ";
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Impact", 15f));
SizeF size = font.MeasureString(headerText);

System.IO.Stream stream = new System.IO.MemoryStream();
pdfDoc.SaveToStream(stream);

PdfDocument pdfDocNew = new PdfDocument();
pdfDocNew.LoadFromStream(stream);

for (int i = 0; i < pdfDoc.Pages.Count; i++)
{
     pdfDocNew.Pages[i].Canvas.DrawString(headerText + (i+1) ,  font, PdfBrushes.RoyalBlue, 10,10);
}


Lisa.Li wrote:Hello,

Thanks for your inquiry.
Please refer to the following sample code to add Header and Footer when the page orientation changes between pages. If there is any question, please provide your testing file as well as your desired effect of output, then we will look into it and guide you accordingly. You could send your file to us via email([email protected]).
Code: Select all
  static void Main(string[] args)
    {
       PdfDocument existingPdf = new PdfDocument();
       existingPdf.LoadFromFile(@"test.pdf");
       //create a new PDF file
       PdfDocument newPdf = new PdfDocument();
       PdfMargins margins = new PdfMargins(90, 72, 90, 72);
       foreach (PdfPageBase page in existingPdf.Pages)
       {
           PdfPageBase newPage = newPdf.Pages.Add(page.Size, new PdfMargins(0));
           newPdf.Template.Top = CreateHeaderTemplate(newPdf,page, margins);
           newPdf.Template.Bottom = CreateFooterTemplate(newPdf,page, margins);
           newPdf.Template.Left = new PdfPageTemplateElement(margins.Left, newPdf.PageSettings.Size.Height);
           newPdf.Template.Right = new PdfPageTemplateElement(margins.Right, newPdf.PageSettings.Size.Height);
           newPage.Canvas.DrawTemplate(page.CreateTemplate(), new PointF(-margins.Left, -margins.Top));
           }
       newPdf.SaveToFile("result.pdf");
    }
    //create template of header
    static PdfPageTemplateElement CreateHeaderTemplate(PdfDocument doc,PdfPageBase page, PdfMargins margins)
    {
            SizeF pageSize = page.Size;
            PdfPageTemplateElement headerSpace = new PdfPageTemplateElement(pageSize.Width, margins.Top);
            headerSpace.Foreground = false;
            float x = margins.Left;
            float y = 0;
            //draw picture in headerSpace
            PdfImage headerImage = PdfImage.FromFile(@"logo.png");
            float width = headerImage.Width / 3;
            float height = headerImage.Height / 3;
            headerSpace.Graphics.DrawImage(headerImage, x, margins.Top - height - 2, width, height);
            //draw line in headerSpace
            PdfPen pen = new PdfPen(PdfBrushes.Gray, 0.5f);
            headerSpace.Graphics.DrawLine(pen, x, y + margins.Top - 2, pageSize.Width - x, y + margins.Top - 2);
            return headerSpace;
       
    }
    //create template of footer
    static PdfPageTemplateElement CreateFooterTemplate(PdfDocument doc,PdfPageBase page, PdfMargins margins)
    {
           SizeF pageSize = page.Size;
           PdfPageTemplateElement footerSpace  = new PdfPageTemplateElement(pageSize.Width, margins.Bottom);
            footerSpace.Foreground = false;
            float x = margins.Left;
            float y = 0;
            //draw line in footerSpace
            PdfPen pen = new PdfPen(PdfBrushes.Gray, 0.5f);
            footerSpace.Graphics.DrawLine(pen, x, y, pageSize.Width - x, y);
            //draw text in footerSpace
            y = y + 5;
            PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 10f, FontStyle.Bold), true);
            PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Left);
            String footerText = "test";
            footerSpace.Graphics.DrawString(footerText, font, PdfBrushes.Black, x, y, format);
            return footerSpace;
 }

Sincerely,
Lisa
E-iceblue support team

PrintPro
 
Posts: 55
Joined: Tue Apr 16, 2019 6:17 pm

Thu Apr 25, 2019 1:20 am

Hello,

Thanks for your valuable feedback. If you need further assistance, please contact us without any hesitation. Wish you all the best!

Sincerely.
Lisa
E-iceblue support team
User avatar

Lisa.Li
 
Posts: 1536
Joined: Wed Apr 25, 2018 3:20 am

Return to Spire.PDF