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 Aug 23, 2021 5:17 pm

I need to use the content of one PDF as the header or footer of another PDF. Do you support it?The header or header PDF is the same page height and width as the content PDF.Can I convert the content into the header or footer of the content PDF by traversing the header or header PDF.

raochenglong
 
Posts: 20
Joined: Mon Aug 23, 2021 5:11 pm

Tue Aug 24, 2021 8:36 am

Hi,

Thank you for your inquiry.
Our Spire.PDF could achieve your requirement. To help us have a better investigation on your real case, please provide you sample PDF files, then we will provide corresponding solution in terms of your files for you. You could attach them here or send them to us via email ([email protected]). Thanks in advance.

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1657
Joined: Wed Apr 07, 2021 2:50 am

Wed Aug 25, 2021 1:34 am

Annika.Zhou wrote:Hi,

Thank you for your inquiry.
Our Spire.PDF could achieve your requirement. To help us have a better investigation on your real case, please provide you sample PDF files, then we will provide corresponding solution in terms of your files for you. You could attach them here or send them to us via email ([email protected]). Thanks in advance.

Sincerely,
Annika
E-iceblue support team

The attachment has been sent, waiting for your reply

raochenglong
 
Posts: 20
Joined: Mon Aug 23, 2021 5:11 pm

Wed Aug 25, 2021 11:12 am

Hello,

Thank you for sharing the file via email
Please refer to the following code to achieve your requirement. If there is any questions, please feel free to contact us.
Code: Select all
            PdfDocument pdf = new PdfDocument();
            PdfDocument contentPdf = new PdfDocument();           
            PdfDocument headerP = new PdfDocument();
            PdfDocument footerP = new PdfDocument();
            contentPdf.LoadFromFile("body.pdf");
            headerP.LoadFromFile("header.pdf");
            footerP.LoadFromFile("footer.pdf");         

            //Split the header PDF
            PdfDocument newHeaderPdf = new PdfDocument();
            //Remove all the margins
            newHeaderPdf.PageSettings.Margins.All = 0;
            //Set the page size of new Pdf
            newHeaderPdf.PageSettings.Width = footerP.Pages[0].Size.Width;
            newHeaderPdf.PageSettings.Height = 70;
            PdfPageBase newHeaderPages = newHeaderPdf.Pages.Add();
            PdfTextLayout format = new PdfTextLayout();
            format.Break = PdfLayoutBreakType.FitPage;
            format.Layout = PdfLayoutType.Paginate;
            //Draw the page in the new page
            headerP.Pages[0].CreateTemplate().Draw(newHeaderPages, new PointF(0, 0), format);
            newHeaderPdf.SaveToFile("headern.pdf");

            //Split the footer PDF
            PdfDocument newFooterPdf = new PdfDocument();
            newFooterPdf.PageSettings.Margins.All = 0;
            newFooterPdf.PageSettings.Width = footerP.Pages[0].Size.Width;
            newFooterPdf.PageSettings.Height = 30;
            PdfPageBase newFooterPage = newFooterPdf.Pages.Add();
            footerP.Pages[0].CreateTemplate().Draw(newFooterPage, new PointF(0, 0), format);

            PdfTemplate hTemplate = newHeaderPdf.Pages[0].CreateTemplate();
            PdfTemplate fTemplate = newFooterPdf.Pages[0].CreateTemplate();

            //Draw template on pdf
            foreach (PdfPageBase page in contentPdf.Pages)
            {
                float width = page.Size.Width;
                float height = page.Size.Height;
                PdfTemplate template = page.CreateTemplate();
                //The default margin is 40
                PdfPageBase newPage = pdf.Pages.Add(new SizeF(width+80,height+ hTemplate.Height+fTemplate.Height+ 80));
               
                newPage.Canvas.DrawTemplate(hTemplate, new PointF(0, 0), new SizeF(width,70));
                newPage.Canvas.DrawTemplate(template,new PointF(0,70),new SizeF(width,height));
                newPage.Canvas.DrawTemplate(fTemplate, new PointF(0, height+70),new SizeF(width, 30));
            }
            pdf.SaveToFile("output.pdf"); 

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1657
Joined: Wed Apr 07, 2021 2:50 am

Wed Sep 08, 2021 9:56 am

Hello,

Hope you're doing well!
How is your issue going? Did the code we provided work for you? Any feedback will be greatly appreciated.

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1657
Joined: Wed Apr 07, 2021 2:50 am

Return to Spire.PDF

cron