Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Wed May 29, 2024 12:36 pm

Hello Support team,
I found an internal "object reference not set to an instance of an object" exception when my code call method "page.CreateTemplate()".

this is my code:

Code: Select all
PdfDocument pdfDocument = new();
pdfDocument.LoadFromFile("file.pdf")
using MemoryStream ms = new();
using PdfDocument destinationPdfDocument = new();
SizeF newPageSize;
SizeF pageVisibleSize;
float resizeMargin;
float widthFactor;
float heightFactor;
float resizeXFactor;
float resizeYFactor;
PdfPageBase newPage;
PdfMargins newPageMargins;
PdfPageBase page;
for (int i = 0; i < pdfDocument.Pages.Count; i++)
{
   page = pdfDocument.Pages[i];
   newPageMargins = (PdfMargins)margins.Clone();
   newPageSize = PdfPageSize.A4;
   if ((page.IsLandscape() && !page.IsRotated())
      || (!page.IsLandscape() && page.IsRotated()))
   {
      newPageSize = new SizeF(PdfPageSize.A4.Height, PdfPageSize.A4.Width);
   }

   pageVisibleSize = page.GetVisibleSize();
   widthFactor = (newPageSize.Width - (newPageMargins.Left + newPageMargins.Right)) / pageVisibleSize.Width;
   heightFactor = (newPageSize.Height - (newPageMargins.Top + newPageMargins.Bottom)) / pageVisibleSize.Height;
   resizeXFactor = widthFactor;
   resizeYFactor = heightFactor;
   if (heightFactor < widthFactor)
   {
      resizeXFactor = heightFactor;
      resizeMargin = (newPageSize.Width - (pageVisibleSize.Width * heightFactor)) / 2;
      newPageMargins.Left = resizeMargin;
      newPageMargins.Right = resizeMargin;
   }
   else if (heightFactor > widthFactor)
   {
      resizeYFactor = widthFactor;
      resizeMargin = (newPageSize.Height - (pageVisibleSize.Height * widthFactor)) / 2;
      if (resizeMargin > newPageMargins.Top)
      {
         newPageMargins.Top = resizeMargin;
         newPageMargins.Bottom = resizeMargin;
      }
   }

   newPage = destinationPdfDocument.Pages.Add(newPageSize, newPageMargins);
   newPage.Canvas.ScaleTransform(resizeXFactor, resizeYFactor);
   newPage.Canvas.DrawTemplate(page.CreateTemplate(), PointF.Empty);
}
destinationPdfDocument.SaveToStream(ms);


and this is the error message :
System.NullReferenceException: Object reference not set to an instance of an object.
at spr鉟.逯()
at Spire.Pdf.Graphics.PdfTemplate..ctor(sprꅏ A_0)
at Spire.Pdf.PdfPageBase.CreateTemplate()

i can't share the document for a GDPR policy, but i can send if you provide me a safer way.

Regards
Fabrizio

fabrizio.lazzarotto
 
Posts: 20
Joined: Mon Feb 28, 2022 4:16 pm

Thu May 30, 2024 7:20 am

Hi,

Thanks for your inquiry.
Could you please send the test file to 'support@e-iceblue.com' via email? Please rest assured that we will never disclose or publish your files, and guarantee that your files will only be used to investigate your problem. You can also remove your core data as long as it can allow us to reproduce the issue. Thank you for your assistance.

Sincerely,
Doris
E-iceblue support team
User avatar

Doris.Liu
 
Posts: 330
Joined: Mon Nov 07, 2022 8:10 am

Thu May 30, 2024 2:26 pm

Hello Doris, the attachment has been sent by support's email.

Regards
Fabrizio

fabrizio.lazzarotto
 
Posts: 20
Joined: Mon Feb 28, 2022 4:16 pm

Fri May 31, 2024 10:51 am

Hi,

Thanks for your sharing.
I have reproduced your issue and logged it into our bug tracking system with ticket number SPIREPDF-6803. Our dev team will investigate and fix it. Once it's resolved, we will update you immediately.
Sorry for any inconvenience caused!

Sincerely,
Doris
E-iceblue support team
User avatar

Doris.Liu
 
Posts: 330
Joined: Mon Nov 07, 2022 8:10 am

Fri Jun 28, 2024 11:31 am

Hi,

Thank you for your patience
Glad to inform you we just released Spire.PDF 10.6.25, which contains the fix of SPIREPDF-6803. Please download it from the following link.

website: https://www.e-iceblue.com/Download/down ... t-now.html
nuget: https://www.nuget.org/packages/Spire.PDF/10.6.25

Sincerely,
Doris
E-iceblue support team
User avatar

Doris.Liu
 
Posts: 330
Joined: Mon Nov 07, 2022 8:10 am

Return to Spire.PDF