Add PDF Page Stamp

Add PDF Page Stamp on All Pages in a PDF File

PdfFileStamp class allows you to add PDF page stamp on all the pages of a PDF file. In order to add PDF page stamp, you first need to create objects of PdfFileStamp and Stamp classes. You also need to create the PDF page stamp using PdfFileStamp method of Stamp class. You can set other attributes like origin, rotation, background etc. using Stamp object as well. Then you can add the stamp in the PDF file using AddStamp method of PdfFileStamp class. Finally, save the output PDF file using Close method of PdfFileStamp class. The following code snippet shows you how to add PDF page stamp on all pages in a PDF file.

private static void AddPageStampOnAllPages()
{
    var dataDir = RunExamples.GetDataDir_AsposePdf_Images();  // Using dynamic path

    // Create PdfFileStamp object using 'using' block to ensure proper disposal
    using (var fileStamp = new Aspose.Pdf.Facades.PdfFileStamp())
    {
        // Open Document
        fileStamp.BindPdf(dataDir + "SourcePDF.pdf");

        // Create stamp
        var stamp = new Aspose.Pdf.Facades.Stamp();
        stamp.BindPdf(dataDir + "AddPageStampOnAllPages.pdf", 1);
        stamp.SetOrigin(20, 20);
        stamp.Rotation = 90.0F;
        stamp.IsBackground = true;

        // Add stamp to PDF file
        fileStamp.AddStamp(stamp);

        // Save updated PDF file with '_out' suffix
        fileStamp.Save(dataDir + "PageStampOnAllPages_out.pdf");
    }
}

Add PDF Page Stamp on Particular Pages in a PDF File

PdfFileStamp class allows you to add PDF page stamp on particular pages of a PDF file. In order to add PDF page stamp, you first need to create objects of PdfFileStamp and Stamp classes. You also need to create the PDF page stamp using BindPdf method of Stamp class. You can set other attributes like origin, rotation, background etc. using Stamp object as well. As you want to add PDF page stamp on particular pages of the PDF file, you also need to set the Pages property of the Stamp class. This property requires an integer array containing numbers of the pages on which you want to add the stamp. Then you can add the stamp in the PDF file using AddStamp method of PdfFileStamp class. Finally, save the output PDF file using Close method of PdfFileStamp class. The following code snippet shows you how to add PDF page stamp on particular pages in a PDF file.

private static void AddPageStampOnCertainPages()
{
    var dataDir = RunExamples.GetDataDir_AsposePdf_Images();  // Using dynamic path

    // Create PdfFileStamp object using 'using' block to ensure proper disposal
    using (var fileStamp = new Aspose.Pdf.Facades.PdfFileStamp())
    {
        // Open Document
        fileStamp.BindPdf(dataDir + "SourcePDF.pdf");

        // Create stamp
        var stamp = new Aspose.Pdf.Facades.Stamp();
        stamp.BindPdf(dataDir + "PageStampOnCertainPages.pdf", 1);
        stamp.SetOrigin(20, 20);
        stamp.Rotation = 90.0F;
        stamp.IsBackground = true;
        stamp.Pages = new[] { 1, 3 };  // Apply stamp to specific pages (1 and 3)

        // Add stamp to PDF file
        fileStamp.AddStamp(stamp);

        // Save updated PDF file with '_out' suffix
        fileStamp.Save(dataDir + "PageStampOnCertainPages_out.pdf");
    }
}

Add Page Number in a PDF File

PdfFileStamp class allows you to add page numbers in a PDF file. In order to add page numbers, you first need to create object of PdfFileStamp class. If you want to show page number like “Page X of N” while X being the current page number and N the total number of pages in the PDF file then you first need to get the page count using NumberOfpages property of PdfFileInfo class. In order to get the current page number you can use # sign in your text anywhere you like. You can format the page number text using FormattedText class. If you want to start the page numbering from a specific number then you can set StartingNumber property. Once you’re ready to add page number in the file, you need to call AddPageNumber method of PdfFileStamp class. Finally, save the output PDF file using Close method of PdfFileStamp class. The following code snippet shows you how to add page number in a PDF file.

private static void AddPageNumberInPdfFile()
{
    var dataDir = RunExamples.GetDataDir_AsposePdf_Images();  // Using dynamic path

    // Create PdfFileStamp object using 'using' block to ensure proper disposal
    using (var fileStamp = new Aspose.Pdf.Facades.PdfFileStamp())
    {
        // Open Document
        fileStamp.BindPdf(dataDir + "StampPDF.pdf");

        // Get total number of pages
        int totalPages = new Aspose.Pdf.Facades.PdfFileInfo(dataDir + "StampPDF.pdf").NumberOfPages;

        // Create formatted text for page number
        var formattedText = new Aspose.Pdf.Facades.FormattedText($"Page # of {totalPages}",
            System.Drawing.Color.AntiqueWhite,
            System.Drawing.Color.Gray,
            Aspose.Pdf.Facades.FontStyle.TimesBoldItalic,  // Updated to full enum name
            Aspose.Pdf.Facades.EncodingType.Winansi,      // Updated to full enum name
            false, 12);

        // Set starting number for first page; you might want to start from 2 or more
        fileStamp.StartingNumber = 1;
        // Add page number in upper right corner
        fileStamp.AddPageNumber(formattedText, (int)PageNumPosition.PosUpperRight);

        // Save updated PDF file with '_out' suffix
        fileStamp.Save(dataDir + "AddPageNumber_out.pdf");
    }
}

// Add PDF Page Numbers
public enum PageNumPosition
{
    PosBottomMiddle, PosBottomRight, PosUpperRight, PosSidesRight, PosUpperMiddle, PosBottomLeft, PosSidesLeft, PosUpperLeft
}

Custom Numbering style

The PdfFileStamp class offers the feature to add Page Number information as stamp object inside PDF document. Prior to this release, the class only supported 1,2,3,4 as page numbering style. However, there has been a requirement from some customers to use custom numbering style when placing page number stamp inside PDF document. In order to accomplish this requirement, NumberingStyle property has been introduced, which accepts the values from NumberingStyle enumeration. Specified below are values offered in this enumeration.

  • LettersLowercase.
  • LettersUppercase.
  • NumeralsArabic.
  • NumeralsRomanLowercase.
  • NumeralsRomanUppercase.
private static void AddCustomPageNumberInPdfFile()
{
    var dataDir = RunExamples.GetDataDir_AsposePdf_Images();  // Using dynamic path

    // Create PdfFileStamp object using 'using' block to ensure proper disposal
    using (var fileStamp = new Aspose.Pdf.Facades.PdfFileStamp())
    {
        // Open Document
        fileStamp.BindPdf(dataDir + "StampPDF.pdf");

        // Get total number of pages
        int totalPages = new Aspose.Pdf.Facades.PdfFileInfo(dataDir + "StampPDF.pdf").NumberOfPages;

        // Create formatted text for page number
        var formattedText = new Aspose.Pdf.Facades.FormattedText($"Page # of {totalPages}",
            System.Drawing.Color.AntiqueWhite,
            System.Drawing.Color.Gray,
            Aspose.Pdf.Facades.FontStyle.TimesBoldItalic,  // Updated to full enum name
            Aspose.Pdf.Facades.EncodingType.Winansi,      // Updated to full enum name
            false, 12);

        // Specify numbering style as Numerals Roman UpperCase
        fileStamp.NumberingStyle = Aspose.Pdf.NumberingStyle.NumeralsRomanUppercase;

        // Set starting number for first page; you might want to start from 2 or more
        fileStamp.StartingNumber = 1;

        // Add page number in upper right corner
        fileStamp.AddPageNumber(formattedText, (int)PageNumPosition.PosUpperRight);

        // Save updated PDF file with '_out' suffix
        fileStamp.Save(dataDir + "AddCustomPageNumber_out.pdf");
    }
}

// Add PDF Page Numbers
public enum PageNumPosition
{
    PosBottomMiddle, PosBottomRight, PosUpperRight, PosSidesRight, PosUpperMiddle, PosBottomLeft, PosSidesLeft, PosUpperLeft
}