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.

Thu Jul 02, 2015 7:26 pm

I am generating a multipage PDF document. I would like all pages EXCEPT the first page to have a header. How do I set up a header template that does not print on the first page?

Thanks

Chris

cherring@cybercsi.com
 
Posts: 5
Joined: Thu Oct 16, 2014 4:56 pm

Fri Jul 03, 2015 7:29 am

Hello,

Thanks for your inquiry.
Here is the code for your reference.
Code: Select all
PdfDocument doc = new PdfDocument();
            PdfPageBase page0 = doc.Pages.Add();       
            PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Trebuchet MS", 14f, FontStyle.Regular));
            for (int i = 0; i < 10; i++)
            {
                page0 = doc.Pages.Add();
                page0.Canvas.DrawString("This is a PDF text header", font1, PdfBrushes.Red, new PointF(0, 0));             
            }         
            doc.SaveToFile("5310.pdf");


Best Regards,
Sweety

E-iceblue support team
User avatar

sweety1
 
Posts: 539
Joined: Wed Mar 11, 2015 1:14 am

Mon Jul 06, 2015 9:02 am

Hello,

Have you tried the code?
Has your issue been resolved?
Thanks for your feedback.

Best Regards,
Sweety

E-iceblue support team
User avatar

sweety1
 
Posts: 539
Joined: Wed Mar 11, 2015 1:14 am

Mon Jul 06, 2015 5:01 pm

My header is contained in a PdfPageTemplateElement, which I have assigned to [document].Template.Top. Is there a way to set up my header template so that it does not render on the first page?

cherring@cybercsi.com
 
Posts: 5
Joined: Thu Oct 16, 2014 4:56 pm

Tue Jul 07, 2015 6:48 am

Hello,

Thanks for your reply.
There is a solution for you. You can create two PDF documents separately. Then, merge them. Suppose your PDF document has 10 pages. First create the first Pdf document which only has the first page and don't have the header. Then, create the second Pdf document which has template. Next, use the merge method in our website to merge the two PDF document. Finally, you get the document you want.
http://www.e-iceblue.com/Tutorials/Spire.PDF/Spire.PDF-Program-Guide/Merge-PDF-Files-with-New-Method-in-C.html
If there are any questions, welcome to get it back to us.

Best Regards,
Sweety

E-iceblue support team
User avatar

sweety1
 
Posts: 539
Joined: Wed Mar 11, 2015 1:14 am

Tue Jul 07, 2015 7:21 pm

Thanks for your reply. In my case, I don't know how many pages my document will be, so your proposed solution does not seem workable.

Let me explain what I am trying to accomplish; perhaps there is a better approach.

I am generating an Invoice. I use a PdfGrid to render the line-items on the invoice. If there are many line items, the grid might flow to a second (or third, etc) page. When this happens, the second and subsequent pages need to have a header.

What is the best way to accomplish this?

cherring@cybercsi.com
 
Posts: 5
Joined: Thu Oct 16, 2014 4:56 pm

Wed Jul 08, 2015 9:41 am

Hello,

Thanks for your reply.
We will look into it and let you know ASAP.

Best Regards,
Sweety

E-iceblue support team
User avatar

sweety1
 
Posts: 539
Joined: Wed Mar 11, 2015 1:14 am

Thu Jul 09, 2015 7:22 am

Hello,

Thanks for your information.
So sorry that our product doesn't support this feature at this stage. We will add it in our future upgrade.

Best Regards,
Sweety

E-iceblue support team
User avatar

sweety1
 
Posts: 539
Joined: Wed Mar 11, 2015 1:14 am

Wed Aug 19, 2015 3:06 am

Hello,

Sorry to keep you waiting.
The new version of Spire.Pdf( Spire.PDF Pack Version:3.5 ) supported the feature you metioned has been released.
The new version added the new properties
Code: Select all
section.Pages.Add().AllowContainTopDocTemplates = false;

to let your header template does not render to the first page.
Here is the full code for your reference.
Code: Select all
private void button3_Click(object sender, EventArgs e)
        {
            //Create a pdf document.
            PdfDocument doc = new PdfDocument();

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

            SetDocumentTemplate(doc, PdfPageSize.A4, margin);           
            PdfSection section = doc.Sections.Add();
            PdfPageBase page = section.Pages.Add();
            page.AllowContainTopDocTemplates = false;
            DrawPage(page);
            doc.SaveToFile("output-1.pdf");
            System.Diagnostics.Process.Start("output-1.pdf");
        }
        static void SetDocumentTemplate(PdfDocument doc, SizeF pageSize, PdfMargins margin)
        {
            PdfPageTemplateElement leftSpace = new PdfPageTemplateElement(margin.Left, pageSize.Height);
            doc.Template.Left = leftSpace;

            PdfPageTemplateElement topSpace = new PdfPageTemplateElement(pageSize.Width, margin.Top);
            topSpace.Foreground = true;
            doc.Template.Top = topSpace;

           
            //draw header label
            PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial", 10f));
            PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Left);
            String label = "Welcome to use Spire.PDF component";
            float y = 0;
            float x = 0;
            topSpace.Graphics.DrawString(label, font1, PdfBrushes.PaleVioletRed, x, y, format);

            label = "E-mail :";
            SizeF size1 = font1.MeasureString(label, format);
            y = y + size1.Height + 3;
            PdfTrueTypeFont font2 = new PdfTrueTypeFont(new Font("Arial", 10f));
            SizeF size2 = font2.MeasureString("E-mail : ", format);
            PdfTrueTypeFont font3 = new PdfTrueTypeFont(new Font("Arial", 10f));
            SizeF size3 = font3.MeasureString("Support@e-iceblue.com", format);
            float y1 = y + size3.Height - size2.Height;
            topSpace.Graphics.DrawString("E-mail : ", font2, PdfBrushes.PaleVioletRed, x, y1, format);

            x = x + size2.Width;
            topSpace.Graphics.DrawString("Support@e-iceblue.com", font3, PdfBrushes.Black, x, y, format);
           
           
       

            PdfPageTemplateElement rightSpace = new PdfPageTemplateElement(margin.Right, pageSize.Height);
            doc.Template.Right = rightSpace;

            PdfPageTemplateElement bottomSpace = new PdfPageTemplateElement(pageSize.Width, margin.Bottom);
            bottomSpace.Foreground = true;
            doc.Template.Bottom = bottomSpace;

           
        }
        static void DrawPage(PdfPageBase page)
        {

            PdfGrid grid = new PdfGrid();
            //Add column
            grid.Columns.Add(4);
            for (int i = 0; i < 90; i++)
            {
                PdfGridRow row = grid.Rows.Add();
                row.Height = 50f;
            }
            grid.Draw(page, new PointF(10, 70));
        }


Best Regards,
Sweety

E-iceblue support team
User avatar

sweety1
 
Posts: 539
Joined: Wed Mar 11, 2015 1:14 am

Thu Aug 20, 2015 9:10 am

Hello,

Have you tried the Spire.PDF Pack Version:3.5?
Have you tried the code?
Has your issue been resolved?
Thanks for your feedback.

Best Regards,
Sweety
E-iceblue support team
User avatar

sweety1
 
Posts: 539
Joined: Wed Mar 11, 2015 1:14 am

Return to Spire.PDF