Using Aspose.Pdf 8.4.1, I’m seeing an odd behavior when attempting to add text stamps to a PDF page that has a CropBox defined. Using the code below, only the version using offsets to compensate for the crop box (including negative values for right and top) accurately places the stamps in the corners of the displayable page. The left bottom origin seem to still work off the MediaBox, while the right top point seems to be adjusted in the wrong direction.
[Fact]
public void Pdf_TextStamp_With_CropBox()
{
var outputPath = @“c:\temp\croptest.pdf”;
using (var outputStream = new MemoryStream())
{
string placeHolderText = “STAMPING MARGIN TEST”;
var pdfBuilder = new Pdf(outputStream);
var pdfSection = pdfBuilder.Sections.Add();
var text = new Text(placeHolderText) {Margin = {Top = 100}};
text.TextInfo.Alignment = Aspose.Pdf.Generator.AlignmentType.Center;
text.TextInfo.FontSize = 16;
text.TextInfo.IsTrueTypeFontBold = true;
pdfSection.AddParagraph(text);
pdfBuilder.Close();
using (var pdf = new Aspose.Pdf.Document(outputStream))
{
//crop the page by 50 all around
var page = pdf.Pages[1];
page.CropBox = new Aspose.Pdf.Rectangle(50, 50, page.MediaBox.URX - 50, page.MediaBox.URY - 50);
pdf.Save();
}
using (var pdf = new Aspose.Pdf.Document(outputStream))
{
var page = pdf.Pages[1];
AddStamp(page, HorizontalAlignment.Left, 0, VerticalAlignment.Bottom, 0, “<=LEFT BOTTOM CORNER”);
AddStamp(page, HorizontalAlignment.Left, 0, VerticalAlignment.Top, 0, “<=LEFT TOP CORNER”);
AddStamp(page, HorizontalAlignment.Right, 0, VerticalAlignment.Bottom, 0, “RIGHT BOTTOM CORNER=>”);
AddStamp(page, HorizontalAlignment.Right, 0, VerticalAlignment.Top, 0, “RIGHT TOP CORNER=>”);
pdf.Save(outputPath + “.zeroMargins.pdf”);
}
using (var pdf = new Aspose.Pdf.Document(outputStream))
{
var page = pdf.Pages[1];
var bottomOffset = Math.Max(page.CropBox.LLY, page.TrimBox.LLY);
var leftOffset = Math.Max(page.CropBox.LLX, page.TrimBox.LLX);
var rightOffset = page.MediaBox.URX - (Math.Min(page.CropBox.URX, page.TrimBox.URX));
var topOffset = page.MediaBox.URY - (Math.Min(page.CropBox.URY, page.TrimBox.URY));
AddStamp(page, HorizontalAlignment.Left, leftOffset, VerticalAlignment.Bottom, bottomOffset, “<=LEFT BOTTOM CORNER”);
AddStamp(page, HorizontalAlignment.Left, leftOffset, VerticalAlignment.Top, topOffset, “<=LEFT TOP CORNER”);
AddStamp(page, HorizontalAlignment.Right, rightOffset, VerticalAlignment.Bottom, bottomOffset, “RIGHT BOTTOM CORNER=>”);
AddStamp(page, HorizontalAlignment.Right, rightOffset, VerticalAlignment.Top, topOffset, “RIGHT TOP CORNER=>”);
pdf.Save(outputPath + “.offsetMargins.pdf”);
}
using (var pdf = new Aspose.Pdf.Document(outputStream))
{
var page = pdf.Pages[1];
var bottomOffset = Math.Max(page.CropBox.LLY, page.TrimBox.LLY);
var leftOffset = Math.Max(page.CropBox.LLX, page.TrimBox.LLX);
var rightOffset = (page.MediaBox.URX - (Math.Min(page.CropBox.URX, page.TrimBox.URX))) * -1;
var topOffset = (page.MediaBox.URY - (Math.Min(page.CropBox.URY, page.TrimBox.URY))) * -1;
AddStamp(page, HorizontalAlignment.Left, leftOffset, VerticalAlignment.Bottom, bottomOffset, “<=LEFT BOTTOM CORNER”);
AddStamp(page, HorizontalAlignment.Left, leftOffset, VerticalAlignment.Top, topOffset, “<=LEFT TOP CORNER”);
AddStamp(page, HorizontalAlignment.Right, rightOffset, VerticalAlignment.Bottom, bottomOffset, “RIGHT BOTTOM CORNER=>”);
AddStamp(page, HorizontalAlignment.Right, rightOffset, VerticalAlignment.Top, topOffset, “RIGHT TOP CORNER=>”);
pdf.Save(outputPath + “.offsetWithNegativeRightTopMargins.pdf”);
}
}
}
private void AddStamp(Page page, HorizontalAlignment hAlign, double hMargin, VerticalAlignment vAlign, double vMargin, string text)
{
var stamp = new TextStamp(text)
{
HorizontalAlignment = hAlign,
VerticalAlignment = vAlign,
};
switch (hAlign)
{
case HorizontalAlignment.Left:
stamp.LeftMargin = hMargin;
break;
case HorizontalAlignment.Right:
stamp.RightMargin = hMargin;
break;
}
switch (vAlign)
{
case VerticalAlignment.Bottom:
stamp.BottomMargin = vMargin;
break;
case VerticalAlignment.Top:
stamp.TopMargin = vMargin;
break;
}
stamp.TextState.Font = FontRepository.FindFont(“Helvetica”);
stamp.TextState.ForegroundColor = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.Black);
stamp.TextState.BackgroundColor = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.White);
page.AddStamp(stamp);
}
Hi Barry,
We are sorry for the inconvenience. While testing the scenario with the latest version of Aspose.Pdf for .NET, I’ve managed to reproduce the issue and logged it in our bug tracking system as PDFNEWNET-35828 for further investigation and resolution. We’ll notify you via this forum thread as soon as it’s resolved.
Please feel free to contact us for any further assistance.
Best Regards.
The issues you have found earlier (filed as PDFNET-35828) have been fixed in Aspose.PDF for .NET 25.10.