I have attached the pdf file and code that i am using to place stamp, so i get blank space coordinate in system.drawing.rect that i convert them using below code, it works for most of the files but not working for one i attached. stamp needs to be movable that is why i am not using imagestamp which makes it flatten.
foreach (var pageKv in pages.OrderBy(x => x.Key))
{
int pageIndex = pageKv.Key;
var info = pageKv.Value;
Aspose.Pdf.Page newPage;
lock (_asposeLock)
{
newPage = pdfDocument.Pages[pageIndex];
}
await SendSignalRUpdate(projectId, fileId, Guid.Empty, $"Stamping page {pageIndex} of {totalPages}");
if (!info.BlankSpace.IsEmpty)
{
int bmpWidth = info.bitmapWidth;
int bmpHeight = info.bitmapHeight;
double scaleX = bmpWidth / newPage.MediaBox.Width;
double scaleY = bmpHeight / newPage.MediaBox.Height;
// create rotated stamp bytes stream for Aspose and compute rectangle
MemoryStream stampStreamForAspose = null;
Aspose.Pdf.Rectangle rect;
// create rotated bytes and compute rect inside a short lock to ensure page properties consistent
lock (_asposeLock)
{
using var rotatedTemp = RotateImageBytes(stampBytes, info.Rotation);
stampStreamForAspose = new MemoryStream();
rotatedTemp.CopyTo(stampStreamForAspose);
stampStreamForAspose.Position = 0;
double x = info.BlankSpace.X / scaleX;
double y = info.BlankSpace.Y / scaleY;
double w = info.BlankSpace.Width / scaleX;
double h = info.BlankSpace.Height / scaleY;
double left = x;
double bottom = newPage.MediaBox.Height - y - h;
rect = new Aspose.Pdf.Rectangle(left, bottom, left + w, bottom + h);
var stampAnnot = new Aspose.Pdf.Annotations.StampAnnotation(newPage, rect)
{
Modified = DateTime.Now,
Image = stampStreamForAspose
};
newPage.Annotations.Add(stampAnnot);
// restore rotation explicitly on the page
newPage.Rotate = info.Rotation;
}
streamsForAspose.Add(stampStreamForAspose);
}
test-document.pdf (2.3 MB)