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 May 26, 2020 1:25 pm

Hi,
I need to create a PDF which is result of merging multiple PDFs.(around 50 pdfs)
The resultant PDF should have a particular header image (20cmx15cm) and footer with page numbers and index.
Could you please share the steps / code (C#) to create a merged PDF with these features?

Thanks and Regards
Dipti K

dipti515
 
Posts: 8
Joined: Thu Apr 23, 2020 10:23 am

Wed May 27, 2020 5:45 am

Hello,

Thanks for your inquiry.
Please refer to the attached code file. If this doesn't meed your needs well, please share some sample files and your desired output with us, so that we could provide you with a corresponding solution.

Sincerely,
Rachel
E-iceblue support team

Code.zip
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Wed May 27, 2020 11:34 am

Thanks Rachel for the solution.
But the header that is coming after merging the files is too small.
When I increase the size of the image in DrawImage() of header function it gets overwritten on the content of the page.
I will share image that should come in header.
Kindly find the attachment.

dipti515
 
Posts: 8
Joined: Thu Apr 23, 2020 10:23 am

Thu May 28, 2020 7:20 am

Hello,

Thanks for your feedback.
I noticed that your header image size is 732px* 575px, which is relatively large. If you want to clearly display the content of the header image, as you know, you need to set a larger size when drawing the image, and this will most likely cause the image to cover the content of the PDF page.
What effect do you want to achieve? Please provide your expected PDF file for our reference, so that we could provide you with a corresponding solution.

Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Thu May 28, 2020 10:10 am

Hi,
Please find the format of the final pdf that I want to generate attached.

Thanks and Regards
Dipti K

dipti515
 
Posts: 8
Joined: Thu Apr 23, 2020 10:23 am

Fri May 29, 2020 4:14 am

Hi Dipti,

Thanks for your sharing.
Here I made a demo for you, please download and test. Kindly note the header image I used is cropped from your PDF document. If there is any question, just feel free to let us know.
http://www.e-iceblue.com/downloads/demo/21689Demo.zip

Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Fri May 29, 2020 5:26 am

Hi Rachel,
Thanks for the quick solution.
But it seems you have kept an empty space in input.pdf for adding header but in my case I have to merge multiple PDFs which are of different formats which should look like out.pdf.

Thanks and Regards
Dipti K

dipti515
 
Posts: 8
Joined: Thu Apr 23, 2020 10:23 am

Fri May 29, 2020 5:39 am

Hello,

Thanks for your prompt response.
Could you please provide some sample PDF files to be merged for reference? Then I will investigate further. You could send them to us([email protected]) via email. Thanks in advance.

Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Fri May 29, 2020 10:26 am

I have mailed sample PDFs.
Kindly check.

Thanks and Regards
Dipti K

dipti515
 
Posts: 8
Joined: Thu Apr 23, 2020 10:23 am

Mon Jun 01, 2020 8:42 am

Hi Dipti,

Thanks for your sharing and sorry for the late reply as weekend.
In order to leave enough space to draw the header image, you can use the “DrawTemplate” method to reduce the original page by a certain percentage. Below is the corresponding code for your reference. If there is any question, please feel free to write back.
Code: Select all
    static PdfDocument pdfDoc = null;
    static void Main(string[] args)
    {
        //Merge File
        string[] files = { "File1.pdf", "File2.pdf", "File3.pdf" };
        Stream stream = MergeFiles(files);
        pdfDoc = new PdfDocument();
        pdfDoc.LoadFromStream(stream);
        //Add header image
        pdfDoc = AddHeader(pdfDoc, 200, "header.png");
        pdfDoc.SaveToFile("out.pdf", FileFormat.PDF);
    }

    private static Stream MergeFiles(string[] files)
    {
        PdfDocumentBase doc = PdfDocument.MergeFiles(files);
        MemoryStream ms = new MemoryStream();
        doc.Save(ms);
        return ms;
    }

    //headerMaxHeight: Header maximum height, the unit is point
    private static PdfDocument AddHeader(PdfDocument pdf, float headerMaxHeight, string imagePath)
    {
        PdfDocument newPdf = new PdfDocument();
        //Load image
        PdfImage headerImage = PdfImage.FromFile(imagePath);
        //Convert unit
        PdfUnitConvertor unitCvtr = new PdfUnitConvertor();
        float pointWidth = unitCvtr.ConvertUnits(headerImage.Width, PdfGraphicsUnit.Pixel, PdfGraphicsUnit.Point);
        float pointHeight = unitCvtr.ConvertUnits(headerImage.Height, PdfGraphicsUnit.Pixel, PdfGraphicsUnit.Point);
           
        foreach (PdfPageBase page in pdf.Pages)
        {
            float newPageHeight = page.Size.Height - headerMaxHeight - 10;
            float pageScale = newPageHeight / page.Size.Height;
            float newPageWidth = page.Size.Width * pageScale;
            PdfPageBase newPage = newPdf.Pages.Add(page.Size, new PdfMargins(0));
            //If the image height > headerMaxHeight, zoom the image
            if (pointHeight > headerMaxHeight)
            {
                //Calculate horizontal scaling
                float hscale = page.Canvas.ClientSize.Width / pointWidth;
                //Calculate vertical scaling
                float vscale = headerMaxHeight / pointHeight;
                //Take the smaller value between the two values
                float imageScale = hscale < vscale ? hscale : vscale;
                //Recalculate the width and height of the image
                pointWidth = pointWidth * imageScale;
                pointHeight = pointHeight * imageScale;
            }
            float x = (page.Canvas.ClientSize.Width - pointWidth) / 2;
            float y = (headerMaxHeight - pointHeight) / 2;
            //Draw the image 
            newPage.Canvas.DrawImage(headerImage, x, y, pointWidth, pointHeight);
            x = (page.Canvas.Size.Width - newPageWidth) / 2;
            //Draw new page
            newPage.Canvas.DrawTemplate(page.CreateTemplate(), new PointF(x, headerMaxHeight), new SizeF(newPageWidth, newPageHeight));
        }
        return newPdf;
    } 


Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Wed Jun 10, 2020 9:02 am

Hi Rachel,
Sorry for the delay in response.
Thanks a lot for the solution,my header issue is resolved now :)
But when I am using the code shared previously for adding TOC, it is adding the TOC at last page.
Kindly find the code and sample PDF's used and the output file generated I have shared on the email sent yesterday.

Thanks and Regards
Dpti K

dipti515
 
Posts: 8
Joined: Thu Apr 23, 2020 10:23 am

Wed Jun 10, 2020 9:40 am

Hi Dipti,

Thanks for your response.
I indeed noticed the issue you mentioned. This issue has been logged into our bug tracking system with the ticket SPIREPDF-3326 for further investigation. If there is any update, we will inform you.
Besides, I found if you save the PdfDocument obejct to a stream after adding the footer, and then re-load the stream to add the TOC, the TOC of the final document is correct, as shown in the following code.

Code: Select all
    string[] files = { "File1.pdf", "File2.pdf", "File3.pdf" };
    Stream stream = MergeFiles(files);
    pdfDoc = new PdfDocument();
    pdfDoc.LoadFromStream(stream);
    //Add header image
    pdfDoc = AddHeader(pdfDoc, 200, @"header1.png");
    AddFooterAndPageNums(pdfDoc);         
    using (MemoryStream ms = new MemoryStream())
    {
        pdfDoc.SaveToStream(ms, Spire.Pdf.FileFormat.PDF);
        PdfDocument pdf = new PdfDocument();
        pdf.LoadFromStream(ms);
        AddTableOfContent(pdf);
        pdf.SaveToFile("out.pdf", Spire.Pdf.FileFormat.PDF);
    }


Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Fri Aug 20, 2021 10:39 am

Hi Dipti,

Hope you are doing well.

Glad to inform you that we just released Spire.PDF Pack(Hot Fix) Version:7.8.8 which fixes the issue SPIREPDF-3326. Welcome to download it from the following links for testing.

Website link: https://www.e-iceblue.com/Download/down ... t-now.html
NuGet link: https://www.nuget.org/packages/Spire.PDF/7.8.8

Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Return to Spire.PDF