Add PDF Header and Footer

PDF stamps are often used in contracts, reports, and restricted materials, to prove that the documents have been reviewed and marked as “read”, “qualified”, or “confidential”, etc. This article will show you how we can add image stamps and text stamps to PDF documents by using Aspose.PDF for PHP via Java.

If you will read the above code snippets line by line, you must find that the syntax and code logic is quite easy to understand.

Adding Text in Header of PDF File

You can use TextStamp class to add text in the header of a PDF file. TextStamp class provides properties necessary to create a text based stamp like font size, font style, and font color etc. In order to add text in the header, you need to create a Document object and a TextStamp object using required properties. After that, you can call AddStamp method of the Page to add the text in the header of the PDF.

You need to set the TopMargin property in such a way that it adjusts the text in the header area of your PDF. You also need to set HorizontalAlignment to Center and VerticalAlignment to Top.

The following code snippet shows you how to add text in the header of a PDF file with PHP.


    // Open document
    $document = new Document($inputFile);

    // Create header
    $textStamp = new TextStamp("Header Text");
    $horizontalAlignment = new HorizontalAlignment();
    $verticalAlignment = new VerticalAlignment();

    // Set properties of the stamp
    $textStamp->setTopMargin(10);
    $textStamp->setHorizontalAlignment($horizontalAlignment->Center);
    $textStamp->setVerticalAlignment($verticalAlignment->Top);

    $pages = $document->getPages();

    // Add footer on the 1st pages
    $page = $pages->get_Item(1);
    $page->addStamp($textStamp);
    
    // Save output document
    $document->save($outputFile);
    $document->close();

You can use TextStamp class to add text in the footer of a PDF file. TextStamp class provides properties necessary to create a text based stamp like font size, font style, and font color etc. In order to add text in the footer, you need to create a Document object and a TextStamp object using required properties. After that, you can call addStamp method of the Page to add the text in the footer of the PDF.

The following code snippet shows you how to add text in the footer of a PDF file with PHP.


    // Open document
    $document = new Document($inputFile);

    // Create header
    $textStamp = new TextStamp("Header Text");
    $horizontalAlignment = new HorizontalAlignment();
    $verticalAlignment = new VerticalAlignment();

    // Set properties of the stamp
    $textStamp->setBottomMargin(10);
    $textStamp->setHorizontalAlignment($horizontalAlignment->Center);
    $textStamp->setVerticalAlignment($verticalAlignment->Bottom);

    $pages = $document->getPages();

    // Add footer on the 1st pages
    $page = $pages->get_Item(1);
    $page->addStamp($textStamp);
    
    // Save output document
    $document->save($outputFile);
    $document->close();

Adding Image in Header of PDF File

You can use ImageStamp class to add image in the header of a PDF file. Image Stamp class provides properties necessary to create image based stamp like font size, font style, and font color etc. In order to add image in the header, you need to create a Document object and a Image Stamp object using required properties. After that, you can call addStamp method of the Page to add the image in the header of the PDF.


    // Open document
    $document = new Document($inputFile);
    
    // Create footer
    $imageStamp = new ImageStamp($inputImage);
    $horizontalAlignment = new HorizontalAlignment();
    $verticalAlignment = new VerticalAlignment();

    // Set properties of the stamp
    $imageStamp->setTopMargin(10);
    $imageStamp->setHorizontalAlignment($horizontalAlignment->Center);
    $imageStamp->setVerticalAlignment($verticalAlignment->Top);

    $pages = $document->getPages();

    // Add footer to 1st page
    $page = $pages->get_Item(1);
    $page->addStamp($imageStamp);

    // Save output document
    $document->save($outputFile);
    $document->close();

The following code snippet shows you how to add image in the header of a PDF file with PHP.

You can use Image Stamp class to add image in the footer of a PDF file. Image Stamp class provides properties necessary to create image based stamp like font size, font style, and font color etc. In order to add image in the footer, you need to create a Document object and an Image Stamp object using required properties. After that, you can call AddStamp method of the Page to add the image in the footer of the PDF.

The following code snippet shows you how to add image in the footer of a PDF file with PHP.


    // Open document
    $document = new Document($inputFile);
    
    // Create footer
    $imageStamp = new ImageStamp($inputImage);
    $horizontalAlignment = new HorizontalAlignment();
    $verticalAlignment = new VerticalAlignment();

    // Set properties of the stamp
    $imageStamp->setBottomMargin(10);
    $imageStamp->setHorizontalAlignment($horizontalAlignment->Center);
    $imageStamp->setVerticalAlignment($verticalAlignment->Bottom);

    $pages = $document->getPages();

    // Add footer to 1st page
    $page = $pages->get_Item(1);
    $page->addStamp($imageStamp);

    // Save output document
    $document->save($outputFile);
    $document->close();

Adding different Headers in one PDF File

We know that we can add TextStamp in Header/Footer section of the document by using TopMargin or Bottom Margin properties, but sometimes we may have the requirement to add multiple header/footers in a single PDF document. Aspose.PDF for PHP via Java explains how to do this.

In order to accomplish this requirement, we will create individual TextStamp objects (number of objects depends upon the number of Header/Footers required)and will add them to PDF document. We may also specify different formatting information for individual stamp object. In following example, we have created Document object and three TextStamp objects and then we have used addStamp method of the Page to add the text in the header section of the PDF. The following code snippet shows you how to add image in the footer of a PDF file with Aspose.PDF for PHP via Java.


    // Open document
    $document = new Document($inputFile);

    // Create three stamps
    $stamp1 = new TextStamp("Header 1");
    $stamp2 = new TextStamp("Header 2");
    $stamp3 = new TextStamp("Header 3");
    
    $horizontalAlignment = new HorizontalAlignment();
    $verticalAlignment = new VerticalAlignment();
    $fontStyles = new FontStyles();

    // Set stamp alignment (place stamp on page top, centered horiznotally)
    $stamp1->setVerticalAlignment ($verticalAlignment->Top);
    $stamp1->setHorizontalAlignment($horizontalAlignment->Center);

    // Specify the font style as Bold
    $stamp1->getTextState()->setFontStyle($fontStyles->Bold);
    // Set the text fore ground color information as red
    $stamp1->getTextState()->setForegroundColor((new Color())->getRed());
    // Specify the font size as 14
    $stamp1->getTextState()->setFontSize(14);

    // Now we need to set the vertical alignment of 2nd stamp object as Top
    $stamp2->setVerticalAlignment($verticalAlignment->Top);
    // Set Horizontal alignment information for stamp as Center aligned
    $stamp2->setHorizontalAlignment($horizontalAlignment->Center);
    // Set the zooming factor for stamp object
    $stamp2->setZoom(10);

    // Set the formatting of 3rd stamp object
    // Specify the Vertical alignment information for stamp object as TOP
    $stamp3->setVerticalAlignment($verticalAlignment->Top);
    // Set the Horizontal alignment inforamtion for stamp object as Center aligned
    $stamp3->setHorizontalAlignment ($horizontalAlignment->Center);
    // Set the rotation angle for stamp object
    $stamp3->setRotateAngle(35);
    // Set pink as background color for stamp
    $stamp3->getTextState()->setBackgroundColor ((new Color())->getPink());  
    // Change the font face information for stamp to Verdana
    $stamp3->getTextState()->setFont ((new FontRepository())->findFont("Verdana"));

    // First stamp is added on first page;
    $document->getPages()->get_Item(1)->addStamp($stamp1);
    // // Second stamp is added on second page;
    $document->getPages()->get_Item(2)->addStamp($stamp2);
    // Third stamp is added on third page.
    $document->getPages()->get_Item(3)->addStamp($stamp3);
    
    // Save output document
    $document->save($outputFile);
    $document->close();