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 Dec 10, 2024 11:27 am

I'm evaluating Spire.PDF with one particular use case in mind which like to merge one PDF into another. Let's call these a parent and a child PDFs.
I would like that parent's header/footer is repeated within added pages and that child PDF pages can be rescaled to fit the available area precisely.
Is that possible with Spire.PDF?

As the parent PDF is generated, it would be possible to prepare pages with a header and footer where the child PDF will be inserted.
The only problematic part is merging child PDF pages over parent PDF pages with rescaling.

marko.stijak
 
Posts: 1
Joined: Tue Dec 10, 2024 11:18 am

Wed Dec 11, 2024 7:06 am

Hello,

Thank you for your inquiry.

Based on your described requirements, I have provided the following demo for you, with the input document used attached.
Code: Select all
using Spire.Pdf;
using Spire.Pdf.Graphics;
using Spire.Pdf.Texts;
using System.Drawing;

// Create a new PdfDocument object for the parent PDF document
PdfDocument parent = new PdfDocument();

// Load the parent PDF document from the specified file path
parent.LoadFromFile(path + "parent.pdf");

// Get the first page of the parent PDF document
PdfPageBase pageBase = parent.Pages[0];

// Get the size of the parent PDF page
SizeF parentPageSize = pageBase.Size;

// Initialize a PdfTextFinder object to search for text on the specified page
PdfTextFinder finder = new PdfTextFinder(pageBase);

// Set the search option to find whole words only
finder.Options.Parameter = Spire.Pdf.Texts.TextFindParameter.WholeWord;

// Search for all occurrences of "MY HEADER" in the page and store the found text fragments
List<PdfTextFragment> finds = finder.Find("MY HEADER");

// Calculate the Y position of the header by adding the height of the found text fragment to its Y position
float HeaderY = finds[0].Positions[0].Y + finds[0].Sizes[0].Height;

// Search for all occurrences of "MY FOOTER" in the page and store the found text fragments
finds = finder.Find("MY FOOTER");

// Calculate the Y position of the footer from the bottom of the page by subtracting the Y position of the found text fragment from the page height
float FooterY = parentPageSize.Height - finds[0].Positions[0].Y;

// Calculate the target size for the content area (excluding header and footer)
SizeF targetSize = new SizeF(parentPageSize.Width, parentPageSize.Height - HeaderY - FooterY);

// Create a new PdfDocument object for the first child PDF document
PdfDocument child1 = new PdfDocument();
// Load the first child PDF document from the specified file path
child1.LoadFromFile(path + "child1.pdf");
// Change the page size of the first child document to match the target size and return the modified document
PdfDocument newChild1 = ChangePage(child1, targetSize);

// Create a new PdfDocument object for the second child PDF document
PdfDocument child2 = new PdfDocument();
// Load the second child PDF document from the specified file path
child2.LoadFromFile(path + "child2.pdf");
// Change the page size of the second child document to match the target size and return the modified document
PdfDocument newChild2 = ChangePage(child2, targetSize);

// Initialize an index variable to keep track of the current page in the parent document
int index = 1;
// Loop through each page of the first child document
for (int i = 0; i < child1.Pages.Count; i++)
{
    // Draw the content of the current page of the first child document onto the corresponding page of the parent document, at the Y position of the header
    newChild1.Pages[i].CreateTemplate().Draw(parent.Pages[index].Canvas, new PointF(0, HeaderY));
    // Increment the index to move to the next page in the parent document
    index++;
}

// Loop through each page of the second child document
for (int i = 0; i < child2.Pages.Count; i++)
{
    // Draw the content of the current page of the second child document onto the corresponding page of the parent document, at the Y position of the header
    newChild2.Pages[i].CreateTemplate().Draw(parent.Pages[index].Canvas, new PointF(0, HeaderY));
    // Increment the index to move to the next page in the parent document
    index++;
}

// Save the modified parent PDF document to the specified output file path
parent.SaveToFile(path + "out.pdf");

// Close the parent PDF document to release resources
parent.Close();
// Close the first child PDF document to release resources
child1.Close();
// Close the second child PDF document to release resources
child2.Close();

static PdfDocument ChangePage(PdfDocument sourcePDF,SizeF targetSize)
{
    //Create a new PDF document
    PdfDocument newPdf = new PdfDocument();

    //Loop through the pages in the original PDF
    foreach (PdfPageBase page in sourcePDF.Pages)
    {
        //Add pages of the custom size (6.5*8.5 inches) to the new PDF
        PdfPageBase newPage = newPdf.Pages.Add(targetSize, new PdfMargins(0));
        //Create a PdfTextLayout instance
        PdfTextLayout layout = new PdfTextLayout();
        //Set text layout as one page (if not set the content will not scale to fit page size)
        layout.Layout = PdfLayoutType.OnePage;
        //Create templates based on the pages in the original PDF
        PdfTemplate template = page.CreateTemplate();
        //Draw the templates onto the pages in the new PDF
        template.Draw(newPage, new PointF(0, 0), layout);
    }

    return newPdf;
}


If the above solution does not meet your needs, please provide your PDF document, and I will create a new demo for you accordingly.

Sincerely,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 3011
Joined: Wed Jun 27, 2012 8:50 am

Return to Spire.PDF