Add stamp image in particular page using AsposePdfAddStamp

Hi Team,

We have requirement add stamp image, in particular pdf page. Currently it is adding in each page.

Is there any page parameter to pass in AsposePdfAddStamp, So only particular that page will show the stamp.

Thank you.

@rakasaha7

To add an image stamp to a specific page in a PDF document using Aspose, you can utilize the PdfFileStamp class in C# or the Document class in Java. Here’s how you can do it in both languages:

C# Example

In C#, you can use the PdfFileStamp class to bind the PDF document and then create an image stamp. You can specify the page number where you want the stamp to appear by setting the Pages property of the stamp. Here’s a sample code snippet:

using System.IO;
using Aspose.Pdf;
using Aspose.Pdf.Facades;

namespace Aspose.Pdf.Examples.CSharp.AsposePDFFacades.StampsWatermarks
{
    public class AddImageStampPage
    {
        public static void Run()
        {
            // Path to the PDF document
            string dataDir = "path_to_your_pdf_directory/";
            
            // Create PdfFileStamp object
            PdfFileStamp fileStamp = new PdfFileStamp();
            
            // Open Document
            fileStamp.BindPdf(dataDir + "AddImageStamp-Page.pdf");
            
            // Create stamp
            Aspose.Pdf.Facades.Stamp stamp = new Aspose.Pdf.Facades.Stamp();
            stamp.BindImage(dataDir + "aspose-logo.jpg");
            stamp.SetOrigin(200, 200);
            stamp.Rotation = 90.0F;
            stamp.IsBackground = true;
            
            // Set the specific page for the stamp
            stamp.Pages = new int[] { 1 }; // Change 1 to your desired page number
            
            // Add stamp to PDF file
            fileStamp.AddStamp(stamp);
            
            // Save updated PDF file
            fileStamp.Save(dataDir + "AddImageStamp-Page_out.pdf");
            
            // Close fileStamp
            fileStamp.Close();
        }
    }
}

Java Example

In Java, you can achieve the same by using the Document class to open the PDF and then adding the image stamp directly to the desired page. Here’s how you can do it:

package com.aspose.pdf.examples.AsposePdfExamples.StampsAndWatermarks;

import com.aspose.pdf.Document;
import com.aspose.pdf.ImageStamp;
import com.aspose.pdf.Rotation;

public class AddingImageStampInPDFFile {
    public static void main(String[] args) {
        // Open document
        Document pdfDocument = new Document("input.pdf");
        
        // Create image stamp
        ImageStamp imageStamp = new ImageStamp("sample.jpg");
        imageStamp.setBackground(true);
        imageStamp.setXIndent(100);
        imageStamp.setYIndent(100);
        imageStamp.setHeight(300);
        imageStamp.setWidth(300);
        imageStamp.setRotate(Rotation.on270);
        imageStamp.setOpacity(0.5);
        
        // Add stamp to a specific page
        pdfDocument.getPages().get_Item(1).addStamp(imageStamp); // Change 1 to your desired page number
        
        // Save output document
        pdfDocument.save("PageNumberStamp_output.pdf");
    }
}

Summary

In both examples, you can specify the page number where the stamp should be added. Make sure to adjust the page number in the Pages property for C# or in the get_Item() method for Java to target the correct page. This way, the stamp will only appear on the specified page rather than on all pages of the PDF.

If you have any further questions or need additional assistance, feel free to ask!

we are using Javascript via C++ for AsposePDF

@rakasaha7
We are looking into it and will be sharing our feedback with you shortly.

Hi rakasaha7,
Thank you for your question!

Unfortunately, the current functionality of the “Aspose.PDF for JavaScript via C++” only supports adding the stamp to the entire document.

Thank you.

Thank you for heads up.

Is it possible to add parameter for page number to add stamp in one particular page.

If yes please let us know. As we are under process of buying license for AsposePDF Javascript via C++ (Pdf replace text).

Moreover, please let us know, if any kind of variable can be added (like pdfreplace text) to replace with stamp easily instead giving X&Y indents.

Thank you for your consideration.

Thank you for your message and for the suggestions.

We are planning to implement the functionality to add a stamp to a specific page of the document. This feature is expected to be included in our release 25.4, which is scheduled for approximately one month from now, as the preparation for the current release, 25.3, has already been completed.

Additionally, could you please provide more details on your request regarding the use of a variable for replacing the stamp, similar to how pdfreplace works for text?

We are very grateful that you have decided to purchase the license and are in the final stages of the process.

Thank you for your consideration, and we look forward to hearing from you.

Thank you for your detailed update regarding the upcoming functionality and the release schedule. I appreciate your transparency and am pleased to hear about the plan to include the functionality for adding a stamp to specific pages in the release 25.4.

Regarding the request for using a variable to replace the stamp, similar to the pdfreplacetextfunctionality, I would be happy to provide more specifics. Here are a few details that might help:

  • In PdfReplaceText, we are giving one text to be replaced and new text (e.g. Java to be replaced with Javascript). So wherever ‘Java’ text are there will be replaced with ‘Javascript’.

  • Similarly, in AsposePdfAddStamp, wherever ‘Java’ text are will be replaced with Stamp image. If this functionality would be implemented then our efforts will be reduced for more productivity.

  • As we are processing more than 150 pdf documents, so it is not possible to define X&Y indent for each pdf, instead it is easy to replace text with stamp image.

Please let me know if additional information is needed, and I’ll be glad to elaborate further.

Thank you once again for your time and efforts. I look forward to your response.

Thank you for the detailed explanation regarding the functionality and your request.
At first glance, it seems that one way to automate the process of determining X&Y coordinates for stamp placement could be through the use of the AsposePdfFindText or AsposePdfFindHiddenText functions. These methods might help identify the location of specific text in the document, which could then be used to determine where the stamp should be placed.

Please let me know if you’d like further assistance or if additional details are needed. I’d be happy to assist further.

Thank you for your prompt response.

I will try AsposePdfFindText as per your suggestion and will let you know in case of any failure.

Thanks again for your assistance :blush:.

You’re very welcome! We’re glad we could help.
Good luck, and don’t hesitate to contact us if anything comes up!

I tried with AsposePdfFindText and then AsposePdfAddStamp. I got the exact coodinations, however the text is still showing which i was trying to find.

the text and stamp image are overwriting each other.

Could you please assist on this.

Thank you.

The text and stamp are separate entities, which is why they might overlap. Could you please provide more details about the task?

Additionally, perhaps setting the opacity of the stamp using setOpacity might help you.
You can find more details on this functionality on the following page AsposePdfAddStamp.

Looking forward to your response.

I tried by making the text color white and reduced the opacity. However, the stamp is showing in each page.

Hope soon we will have release 25.4 to have functionality for adding stamp in particular page.

Thank you.

Hello!
As mentioned earlier, we plan to include the ability to add a stamp on specific pages in the 25.4 release.
Thank you!