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.

Mon Oct 16, 2017 12:53 pm

Hello,
Evaluating Spire.PDF and have some questions with regards to Page Headers and Footers;

First:
How can I insert a Table into the Page Header or footer? The Table may be complex.

Secondly,
How can I insert Page Headers and footers from another document? (Header contain tables with images, etc...)

Third;
I need to exclude page Header and footers for certain pages (first 2 pages), is this possible?

Thanks!

alacroce
 
Posts: 4
Joined: Thu May 25, 2017 2:59 pm

Tue Oct 17, 2017 6:41 am

Hello alacroce,

Thanks for your inquiry.
For requirement one. Please refer to the below code to draw table in the header and footer.
Code: Select all
 //create new PDF document
            PdfDocument doc = new PdfDocument();
            //set margin   
            PdfUnitConvertor unitCvtr = new PdfUnitConvertor();
            PdfMargins margin = new PdfMargins();
            margin.Top = unitCvtr.ConvertUnits(2.54f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
            margin.Bottom = margin.Top;
            margin.Left = unitCvtr.ConvertUnits(4.17f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
            margin.Right = margin.Left;
            //apply template in PDF document
            SetDocumentTemplate1(doc, PdfPageSize.A4, margin);
            //add two pages in PDF
            PdfPageBase page = doc.Pages.Add();
            PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Trebuchet MS", 14f, FontStyle.Italic));
            PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Right);
            page.Canvas.DrawString("This is page1",font,PdfBrushes.Brown,new PointF());

            PdfPageBase page2 = doc.Pages.Add();
            page2.Canvas.DrawString("This is page2", font, PdfBrushes.Brown, new PointF());
            doc.SaveToFile("TableHeaderFooter.pdf");

 protected void SetDocumentTemplate1(PdfDocument doc, SizeF pageSize, PdfMargins margin)
        {
            //set a top page template
            PdfPageTemplateElement topSpace = new PdfPageTemplateElement(pageSize.Width, margin.Top);
            PdfPageTemplateElement footerSpace = new PdfPageTemplateElement(pageSize.Width, margin.Bottom);
            topSpace.Foreground = true;
            doc.Template.Top = topSpace;
            footerSpace.Foreground = true;
            doc.Template.Bottom = footerSpace;

            String[] data
= {
   "PartNo;Description;OnHand;OnOrder;Cost;ListPrice",
   "900;Dive kayak;24;16;1356.75;3999.95",
   "912;Underwater Diver Vehicle;5;3;504;1680",
   "1313;Regulator System;165;216;117.5;250",
   "1314;Second Stage Regulator;98;88;124.1;365",
       };
            String[][] dataSource
                = new String[data.Length][];
            for (int i = 0; i < data.Length; i++)
            {
                dataSource[i] = data[i].Split(';');
               
            }
            PdfTable table = new PdfTable();
            table.Style.CellPadding = 2;
            table.Style.BorderPen = new PdfPen(PdfBrushes.Blue, 0.75f);
            table.Style.HeaderStyle.StringFormat = new PdfStringFormat(PdfTextAlignment.Center);
            table.Style.HeaderSource = PdfHeaderSource.Rows;
            table.Style.HeaderRowCount = 1;
            table.Style.ShowHeader = true;
            table.Style.HeaderStyle.BackgroundBrush = PdfBrushes.CadetBlue;
            table.DataSource = dataSource;
            foreach (PdfColumn column in table.Columns)
            {
                column.StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);
            }

            //draw table in the header
            table.Draw(topSpace.Graphics);
            //draw table in the footer
            table.Draw(footerSpace.Graphics);}


For requirement two, sorry it impossible to get the header and footer from an existing pdf because there's no concept of header and footer in a pdf and everything is drawn on the cavas.
For requirement three, if this is a new pdf, you could draw the headers for every page respectively excluding the first two pages. Below is the code for your reference.
Code: Select all
    //create new PDF document
            PdfDocument doc = new PdfDocument();

            //set margin   
            PdfUnitConvertor unitCvtr = new PdfUnitConvertor();
            PdfMargins margin = new PdfMargins();
            margin.Top = unitCvtr.ConvertUnits(0.635f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
            margin.Bottom = unitCvtr.ConvertUnits(2.54f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
            margin.Left = unitCvtr.ConvertUnits(0.635f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
            margin.Right = margin.Left;

            PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Trebuchet MS", 9f, FontStyle.Regular));
            PdfPageBase page = doc.Pages.Add(PdfPageSize.A4, margin, PdfPageRotateAngle.RotateAngle0, PdfPageOrientation.Portrait);
            PdfBrush brush = PdfBrushes.Red;
            PointF location = new PointF(0, 100);
            page.Canvas.DrawString("This is the first page", font1, brush, location);

            PdfPageBase page2 = doc.Pages.Add(PdfPageSize.A4, margin, PdfPageRotateAngle.RotateAngle0, PdfPageOrientation.Portrait);
            page2.Canvas.DrawString("This is the second page", font1, brush, location);

            PdfPageBase page3 = doc.Pages.Add(PdfPageSize.A4, margin, PdfPageRotateAngle.RotateAngle0, PdfPageOrientation.Portrait);
            page3.Canvas.DrawString("This is the third page", font1, brush, location);
            AddHeaderforPage(page3);
            AddFooterforPage(page3);

            PdfPageBase page4 = doc.Pages.Add(PdfPageSize.A4, margin, PdfPageRotateAngle.RotateAngle0, PdfPageOrientation.Portrait);
            page4.Canvas.DrawString("This is the fourth page", font1, brush, location);
            AddHeaderforPage(page4);
            AddFooterforPage(page4);
            doc.SaveToFile(@"headerfooter.pdf");
  private void AddHeaderforPage(PdfPageBase page)
        {
            String[] data
= {
   "PartNo;Description;OnHand;OnOrder;Cost;ListPrice",
   "900;Dive kayak;24;16;1356.75;3999.95",
   "912;Underwater Diver Vehicle;5;3;504;1680",
   "1313;Regulator System;165;216;117.5;250",
   "1314;Second Stage Regulator;98;88;124.1;365",
       };
            String[][] dataSource
                = new String[data.Length][];
            for (int i = 0; i < data.Length; i++)
            {
                dataSource[i] = data[i].Split(';');

            }
            PdfTable table = new PdfTable();
            table.Style.CellPadding = 2;
            table.Style.BorderPen = new PdfPen(PdfBrushes.Blue, 0.75f);
            table.Style.HeaderStyle.StringFormat = new PdfStringFormat(PdfTextAlignment.Center);
            table.Style.HeaderSource = PdfHeaderSource.Rows;
            table.Style.HeaderRowCount = 1;
            table.Style.ShowHeader = true;
            table.Style.HeaderStyle.BackgroundBrush = PdfBrushes.CadetBlue;
            table.DataSource = dataSource;
            foreach (PdfColumn column in table.Columns)
            {
                column.StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);
            }
            //draw table in the header
            table.Draw(page,new PointF());
        }
  private void AddFooterforPage(PdfPageBase page)
        {
            String[] data
= {
   "PartNo;Description;OnHand;OnOrder;Cost;ListPrice",
   "900;Dive kayak;24;16;1356.75;3999.95",
   "912;Underwater Diver Vehicle;5;3;504;1680",
   "1313;Regulator System;165;216;117.5;250",
   "1314;Second Stage Regulator;98;88;124.1;365",
       };
            String[][] dataSource
                = new String[data.Length][];
            for (int i = 0; i < data.Length; i++)
            {
                dataSource[i] = data[i].Split(';');

            }
            PdfTable table = new PdfTable();
            table.Style.CellPadding = 2;
            table.Style.BorderPen = new PdfPen(PdfBrushes.Blue, 0.75f);
            table.Style.HeaderStyle.StringFormat = new PdfStringFormat(PdfTextAlignment.Center);
            table.Style.HeaderSource = PdfHeaderSource.Rows;
            table.Style.HeaderRowCount = 1;
            table.Style.ShowHeader = true;
            table.Style.HeaderStyle.BackgroundBrush = PdfBrushes.CadetBlue;
            table.DataSource = dataSource;
            foreach (PdfColumn column in table.Columns)
            {
                column.StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);
            }
            //draw table in the footer
            table.Draw(page,new PointF(20,650));
        }


Sincerely,
Jane
E-iceblue support team
User avatar

Jane.Bai
 
Posts: 1156
Joined: Tue Nov 29, 2016 1:47 am

Tue Oct 17, 2017 12:22 pm

Thanks Jane,
For requirement two that was in reference to Word. I am evaluating both and will post to the Word forum.

Thanks again.

alacroce
 
Posts: 4
Joined: Thu May 25, 2017 2:59 pm

Wed Oct 18, 2017 1:40 am

Hello alacroce,

Thanks for your response.
Below is the code for requirement 2 regarding to Word document.
Code: Select all
Document doc1 = new Document();
            doc1.LoadFromFile(@"C:\Users\Administrator\Desktop\test11888.docx");
            HeaderFooter header = doc1.Sections[0].HeadersFooters.Header;
            HeaderFooter footer = doc1.Sections[0].HeadersFooters.Footer;

            Document doc2 = new Document();
            Section section = doc2.AddSection();
            //copy the header and footer of the first section in the original document
            foreach (DocumentObject obj in header.ChildObjects)
            {
                section.HeadersFooters.Header.ChildObjects.Add(obj.Clone());
            }

            foreach (DocumentObject obj in footer.ChildObjects)
            {
                section.HeadersFooters.Footer.ChildObjects.Add(obj.Clone());
            }

            string output = "11888.docx";
            doc2.SaveToFile(output, FileFormat.Docx);


Sincerely,
Jane
E-iceblue support team
User avatar

Jane.Bai
 
Posts: 1156
Joined: Tue Nov 29, 2016 1:47 am

Tue Oct 24, 2017 8:26 am

Hi,

Greetings from E-iceblue.
Has your issue got resolved?
Thanks in advance for your valuable feedback and time.

Sincerely,
Jane
E-iceblue support team
User avatar

Jane.Bai
 
Posts: 1156
Joined: Tue Nov 29, 2016 1:47 am

Return to Spire.PDF