Add Page Stamp to PDF using C#

Create page stamp programmaticaly using Aspose.PDF for .NET Library

How to add Page Stamps to PDF using .NET Library

In order to work with page stamp into PDF file, we’ll use Aspose.PDF for .NET API which is a feature-rich, powerful and easy to use document manipulation API for net platform. Open NuGet package manager, search for Aspose.PDF and install. You may also use the following command from the Package Manager Console.

Package Manager Console

PM > Install-Package Aspose.PDF

Add Page Stamp to PDF Document C#


You need Aspose.PDF for .NET to try the code in your environment.

  1. Load the PDF with an instance of Document.
  2. Open a PDF document using Document object.
  3. Create a Page Stamp and define its properties.
  4. Add the Stamp to Page using AddStamp method

Add Page Stamp to PDF - C#

static Aspose.Pdf.PdfPageStamp GetPageStamp(int pageNum)
{
    var pageStampFile = Path.Combine(dataDir, "PageStamp.pdf");
    return new Aspose.Pdf.PdfPageStamp(pageStampFile, pageNum)
    {
        Height = 800,
        Background = true
    };
}

var inputFile = Path.Combine(dataDir, "sample.pdf");
var outputFile = Path.Combine(dataDir, "AddPageStamp_out.pdf");        
var pdfDocument = new Aspose.Pdf.Document(inputFile);
var bluePageStamp = GetPageStamp(1);
var plumPageStamp = GetPageStamp(2);

for (int i = 1; i < 5; i++)
{
    if (i % 2 == 0)
        pdfDocument.Pages[i].AddStamp(bluePageStamp);
    else
        pdfDocument.Pages[i].AddStamp(plumPageStamp);            
}
pdfDocument.Save(outputFile);