We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

FreeTextAnnotation ctor throws null reference exception

I’m using Aspose.Pdf, v17.4, to stamp hyperlinks onto documents. For the attached document, my process is failing, despite working on almost all other documents.

I’m loading the attached file and feeding the bytes into a method that does the following:
  1. creates a Document object with the byte[]
  2. decrypts the document object
  3. attempts to create a new FreeTextAnnotation on document.Pages[0], using a new Rectangle, and new DefaultAppearance
When I step over the code reference in line 3 above, the exception StackTrace shows it coming from the following location:

at Aspose.Pdf.Annotations.FreeTextAnnotation.hulrgtdcn4a36jvghassc3d9p8uvbscd (Annotation )
at Aspose.Pdf.Annotations.TextStyle. ()
at Aspose.Pdf.Annotations.FreeTextAnnotation…ctor(Page page, Rectangle rect, DefaultAppearance appearance)

The relevant lines of code are as follows:

// load the file from disk and pass it into my method
var fileBytes = File.ReadAllBytes(inputName);
var stampedBytes = _docStampingService.StampCertLinkOntoDocument(fileBytes);

// my method signature
public byte[] StampCertLinkOntoDocument(byte[] pdfBytes)

// create the document and decrypt it
var document = new Aspose.Pdf.Document(new MemoryStream(pdfBytes));

// I loop through each of the pages, but it is the very first page that fails
foreach (Page page in document.Pages)
var pageHeight = page.ArtBox.Height;
var pageWidth = page.ArtBox.Width;

var textAnnotation = new FreeTextAnnotation(page, new Rectangle(pageWidth - (pageWidth * .99), pageHeight - (pageHeight * .04),
pageWidth - (pageWidth * .4), pageHeight - (pageHeight * .01)),
new DefaultAppearance(FontRepository.FindFont(“TimesNewRoman”), pageHeight * 0.014, Color.Blue));

My actual code continues on doing other things, but for the attached document, my code fails when it attempts to create the new FreeTextAnnotation. I have inspected the code at run-time, and the pageHeight & pageWidth variables are correctly set to what I would expect.

Can I get some assistance with this issue? The vast majority of the time my code works, but for a very small set of documents like the one attached, I can’t successfully create the FreeTextAnnotation I need to create.

Thank you.


Thank you for contacting support. We managed to replicate the said error in our environment. It has been logged under the ticket ID PDFNET-42795 in our bug tracking system. We have linked your post to this ticket and will notify you once it is fixed. We are sorry for the inconvenience caused.

As a workaround, please alter your last line of code and pass the font name as string:
new DefaultAppearance(FontRepository.FindFont(“TimesNewRoman”), pageHeight * 0.014, Color.Blue));
new DefaultAppearance(“TimesNewRoman”, pageHeight * 0.014, Color.Blue));