Manage Header and Footer

Add Header in a PDF File

PdfFileStamp class allows you to add header in a PDF file. In order to add header, you first need to create object of PdfFileStamp class. You can format the header text using FormattedText class. Once you’re ready to add header in the file, you need to call addHeader method of PdfFileStamp class. You also need to specify at least the top margin in the addHeader method. Finally, save the output PDF file using close method of PdfFileStamp class. The following code snippet shows you how to add header in a PDF file.

 public static void AddHeader() {
        // Create PdfFileStamp object
        PdfFileStamp fileStamp = new PdfFileStamp();

        // Open Document
        fileStamp.bindPdf(_dataDir + "sample.pdf");

        // Create formatted text for page number
        FormattedText formattedText = new FormattedText("Aspose - Your File Format Experts!", java.awt.Color.YELLOW,
                java.awt.Color.BLACK, FontStyle.Courier, EncodingType.Winansi, false, 14);

        // Add header
        fileStamp.addHeader(formattedText, 20);

        // Save updated PDF file
        fileStamp.save(_dataDir + "AddHeader_out.pdf");

        // Close fileStamp
        fileStamp.close();
    }

PdfFileStamp class allows you to add footer in a PDF file. In order to add footer, you first need to create object of PdfFileStamp class. You can format the footer text using FormattedText class. Once you’re ready to add footer in the file, you need to call addFooter method of PdfFileStamp class. You also need to specify at least the bottom margin in the addFooter method. Finally, save the output PDF file using close method of PdfFileStamp class. The following code snippet shows you how to add footer in a PDF file.

 public static void AddFooter() {
        // Create PdfFileStamp object
        PdfFileStamp fileStamp = new PdfFileStamp();

        // Open Document
        fileStamp.bindPdf(_dataDir + "sample.pdf");

        // Create formatted text for page number
        FormattedText formattedText = new FormattedText("Aspose - Your File Format Experts!", java.awt.Color.BLUE,
                java.awt.Color.GRAY, FontStyle.Courier, EncodingType.Winansi, false, 14);

        // Add footer
        fileStamp.addFooter(formattedText, 10);

        // Save updated PDF file
        fileStamp.save(_dataDir + "AddFooter_out.pdf");

        // Close fileStamp
        fileStamp.close();
    }

Add Image in Header of an Existing PDF File

PdfFileStamp class allows you to add image in the header of a PDF file. In order to add image in header, you first need to create object of PdfFileStamp class. After that, you need to call addHeader method of PdfFileStamp class. You can pass the image to the addHeader method. Finally, save the output PDF file using close method of PdfFileStamp class. The following code snippet shows you how to add image in header of PDF file.

public static void AddImageHeader() {
        // Create PdfFileStamp object
        PdfFileStamp fileStamp = new PdfFileStamp();

        // Open Document
        fileStamp.bindPdf(_dataDir + "sample.pdf");
        FileInputStream fs;
        try {
            fs = new FileInputStream(_dataDir + "aspose-logo.png");
            // Add Header
            fileStamp.addHeader(fs, 10);

            // Save updated PDF file
            fileStamp.save(_dataDir + "AddImage-Header_out.pdf");
        } catch (FileNotFoundException e) {

            e.printStackTrace();
        }

        // Close fileStamp
        fileStamp.close();
    }

PdfFileStamp class allows you to add image in the footer of a PDF file. In order to add image in footer, you first need to create object of PdfFileStamp class. After that, you need to call addFooter method of PdfFileStamp class. You can pass the image to the addFooter method. Finally, save the output PDF file using close method of PdfFileStamp class. The following code snippet shows you how to add image in the footer of PDF file.

    public static void AddImageFooter() {
        // Create PdfFileStamp object
        PdfFileStamp fileStamp = new PdfFileStamp();

        // Open Document
        fileStamp.bindPdf(_dataDir + "sample.pdf");
        FileInputStream fs;
        try {
            fs = new FileInputStream(_dataDir + "aspose-logo.png");
            // Add footer
            fileStamp.addFooter(fs, 10);

            // Save updated PDF file
            fileStamp.save(_dataDir + "AddImage-Footer_out.pdf");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        // Close fileStamp
        fileStamp.close();
    }