AddStamp with different page format in one PDF

Hello,

I’m currently using Aspose.Pdf to add watermarks to existing PDF.
I was wondering if it is possible (and how) to adapt the origin of the stamp page by page.
I currently calculate origin based on page width and height. It works perfectly for the first page (for instance Portrait format) but if the format change, it is not at the center anymore (for Landscape format e.g.).
Is there a way to set the stamp at the center, regardless the page format?

Thanks a lot!

Best regards,
Sylvie

PS: here is how I currently calculate origin:
// Create the filestamp object
PdfFileStamp fileStamp = new PdfFileStamp(pdfDoc);
Aspose.Pdf.Facades.Stamp stamp = new Aspose.Pdf.Facades.Stamp();
stamp.Rotation = water_RotationAngle;
stamp.Opacity = water_Opacity;
stamp.IsBackground = water_Background;
EncodingType waterEncodingType = EncodingType.Cp1250;
FormattedText Water_Text = new FormattedText(water_TextContent, System.Drawing.Color.Black, System.Drawing.Color.Empty, “Arial”, waterEncodingType, false, water_TextSize);
stamp.BindLogo(Water_Text);
double stampX = Math.Abs(Water_Text.TextWidth * Math.Sin(Math.PI * water_RotationAngle / 180.00));
double stampY = Math.Abs(Water_Text.TextWidth * Math.Cos(Math.PI * water_RotationAngle / 180.00));
stamp.SetOrigin(fileStamp.PageWidth / 2 - (float)stampX / 2 + water_PositionfromcenterX, fileStamp.PageHeight / 2 - (float)stampY / 2 + water_PositionfromcenterY);

fileStamp.AddStamp(stamp);

Hi Sylvie,

Thanks for your inquiry. While calculating X,Y coordinates for stamp, you can iterate through the document pages and calculate stamp coordinates according to each page. Please check following sample code snippet, hopefully it will help you to accomplish the task.

for (int i = 1; i <= doc.Pages.Count; i++)
{
    double stampX = Math.Abs(Water_Text.TextWidth * Math.Sin(Math.PI * 90 / 180.00));
    double stampY = Math.Abs(Water_Text.TextWidth * Math.Cos(Math.PI * 90 / 180.00));
    
    stamp.SetOrigin((float)doc.Pages[i].Rect.Width / 2 - (float)stampX / 2 + 100, (float)doc.Pages[i].Rect.Height / 2 - (float)stampY / 2 + 100);
    stamp.Pages = new int[] { i };
    
    fileStamp.AddStamp(stamp);
}
fileStamp.Save(myDir + "teststamp.pdf");

Please feel free to contact us for any further assistance.

Best Regards,

Hello!

Many thanks for your help, it now works exactly as expected!

Best regards,
Sylvie

Hi Sylvie,

Thanks for your feedback. It is good to know that above shared code helped you to accomplish the task.

Please keep using Aspose.Pdf and feel free to contact us for any further query and concern, we will more than happy to extend our support.

Best Regards,