Stamp Document No Show Stamp

Hello,

I’m trying to add a stamp in the upper right corner to PDF files but with some I have the problem that it doesn’t display.

This is the code I’m using:
public static byte[] StampDocument(byte[] document, byte[] stamp)
{
using MemoryStream memoryStreamToPdf = new MemoryStream();
PdfFileStamp pdfDocument = new PdfFileStamp();

            using MemoryStream memoryStreamReader = new MemoryStream(document);
            pdfDocument.BindPdf(memoryStreamReader);

            var stampCreated = CreateStamp(stamp);
            using MemoryStream memoryStreamReaderStamp = new MemoryStream(stampCreated);

            Stamp imageStamp = SetImageStamp(memoryStreamReaderStamp, pdfDocument.PageHeight, pdfDocument.PageWidth);
            pdfDocument.AddStamp(imageStamp);
            pdfDocument.Save(memoryStreamToPdf);

            return memoryStreamToPdf.ToArray();
        }

private static byte[] CreateStamp(byte[] stamp)
        {
            using MemoryStream memoryStreamReaderStamp = new MemoryStream(stamp);
            using Bitmap bitmap = (Bitmap)Image.FromStream(memoryStreamReaderStamp);
            using Graphics graphics = Graphics.FromImage(bitmap);
            using Font arialFont = new Font("Arial", 8);
            using SolidBrush brush = new SolidBrush(Color.FromArgb(4, 4, 126).ToRgb());

            using MemoryStream stream = new MemoryStream();
            bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
            return stream.ToArray();
        }

private static Stamp SetImageStamp(Stream stamp, double pageDocumentHeight, double pageDocumentWidth)
        {
            int originX = Convert.ToInt32(pageDocumentWidth - 110);
            int originY = Convert.ToInt32(pageDocumentHeight - 80);

            Stamp imageStamp = new Stamp();
            imageStamp.BindImage(stamp);
            imageStamp.SetImageSize(100, 65);
            imageStamp.SetOrigin(originX, originY);
            imageStamp.Pages = new[] { 1 };
            return imageStamp;
        }

I cann’t attach the file because it has sensitive data.

A greeting,

@i.salazar

You can please share your sample file in a private message where only Aspose staff will have access to it. You can send it by clicking over username and pressing Blue Message Button.

I have send to you the file.

@i.salazar

We were able to notice the issue in our environment and it seems like your PDF already has images and stamp is being added behind existing image. Nevertheless, we have logged an issue as PDFNET-48762 in our issue tracking system for further investigation on this case. We will look into its details and keep you informed with the status of its rectification. Please be patient and spare us some time.

We are sorry for the inconvenience.

PS: We tested the scenario using Aspose.PDF for .NET 20.9.

OK, asad.

Thanks for your attention.
Is there any possibility to tell my stamp to overlay the images of the current PDF?

@i.salazar

We already tried setting Background property of ImageStamp as false but it did not give us much success. We are afraid that we cannot offer any alternatives at the moment before the logged ticket is investigated completely. We will let you know as soon as we have some definite updates in this regard. Please give us some time.

We are sorry for your inconvenience.

Hello @asad.ali.

Could you tell to me what’s the state about this subject?

Regards,

@i.salazar

We are afraid that earlier logged ticket is not yet resolved. It was recently logged in our issue management system and will be resolved on first come first serve basis. We will inform you as soon as we have some certain updates regarding its rectification. Please be patient and give us some time.

We are sorry for the inconvenience.

@i.salazar

We have investigated the earlier logged ticket and found that the page was rotated and this fact was not taken into consideration by the code. Hence, the stamp was out of the page area.

The page is rotated over 270 degrees. This means that page width (visible) is 595, not 842. And stamp position is set to 842 - 110 = 732 which is outside of page area. (Image position is calculated taking page rotation into consideration). PdfFileStamp does not have simple method which allows to determine if page is rotated or not; you can use DOM for this:

Document doc = new Document("Total.pdf");
Rectangle rect = doc.Pages[1].GetRect(true); //parameter of this method shows is page rotation is taken into consideration. 
//if it false then "raw" rectangle is returned (as specified in page dictionary); if true then page rotation matrix will be applied to the rectangle. 
int originX = Convert.ToInt32(rect.Width- 110);
int originY = Convert.ToInt32(rect.Height - 80);
//and further in the text....

Using DOM you can make this in more simple way:

Document doc = new Document("total.pdf");
ImageStamp stamp = new ImageStamp("stamp.jpg");
stamp.Width = 100;
stamp.Height = 65;
Rectangle rect = doc.Pages[1].GetPageRect(false);
stamp.XIndent = rect.Width - 100;
stamp.YIndent = rect.Height- 65;
doc.Pages[1].AddStamp(stamp);
doc.Save("output.pdf");

Also, ImageStmap has an ability to set horizontal and vertical alignment and the code may be written in the following way:

Document doc = new Document("total.pdf");
ImageStamp stamp = new ImageStamp("stamp.jpg");
stamp.Width = 100;
stamp.Height = 65;
stamp.HorizontalAlignment = HorizontalAlignment.Right;
stamp.VerticalAlignment = VerticalAlignment.Top;
stamp.RightMargin = 10;
stamp.TopMargin = 10;
doc.Pages[1].AddStamp(stamp);
doc.Save("output.pdf");

Hello @asad.ali,

Effectivaly, this code is completely functional.
One last question, How could I put the stamp in front of any text/image?
Actually, my stamp is background = false, but it shows behind other text/image.

Regards,

@i.salazar

Thanks for the feedback.

You can remove the opacity of the image stamp in order to display it over the text/image.