System.NullReferenceException on Pages.Add() Method

Hi

I’d like to create a PDF Document, which must be linearized, from a bunch of PNG-Files, which can be up to 500. I am using the following code:

Document pdfDocument = new Document();

foreach (string file in Directory.GetFiles(“C:\Temp\PNG”))
{
Page page = pdfDocument.Pages.Add();

FileStream fs = new FileStream(file, FileMode.Open);
byte[] tmpBytes = new byte[fs.Length];
fs.Read(tmpBytes, 0, Convert.ToInt32(fs.Length));

MemoryStream mystream = new MemoryStream(tmpBytes);
// Instantiate BitMap object with loaded image stream
Bitmap b = new Bitmap(mystream);

// Set margins so image will fit, etc.
page.PageInfo.Margin.Bottom = 0;
page.PageInfo.Margin.Top = 0;
page.PageInfo.Margin.Left = 0;
page.PageInfo.Margin.Right = 0;

page.CropBox = new Aspose.Pdf.Rectangle(0, 0, b.Width, b.Height);
// Create an image object
Aspose.Pdf.Image image1 = new Aspose.Pdf.Image();
// Add the image into paragraphs collection of the section
page.Paragraphs.Add(image1);
// Set the image file stream
image1.ImageStream = mystream;
// Save resultant PDF file

fs.Flush();
fs.Close();

pdfDocument.Optimize();
pdfDocument.Save(“C:\Temp\Output.pdf”);
}

Troubles so far:
- Save Method must be called in foreach block, otherwise it will quit with a OutOfMemoryException when adding a large number of PNG
- After creating 5 Pages in the PDF it crashes with NullReferenceException on Page page = pdfDocument.Pages.Add();
- The size of the first page is correct, the size of the following pages are too small
- When trying to do this Document pdfDocument = new Document(“C:\Temp\Output.pdf”); I get a Aspose.Pdf.Exceptions.InvalidPdfFileFormatException "Incorect file format"

I am using Aspose.Pdf 10.7.0.0

Regards

Christian

Hi Christian,


Thanks for contacting support.

I have tested the scenario using Aspose.Pdf for .NET 10.7.0 in VS2010 application while using some of my sample image files and as per my observations, the conversion process keeps on running and appears to hang. I have tried placing Save(…) and Optimize(…) of pdfDocument object inside foreach loop and have observed same behavior. For the sake of correction, I have logged this problem
as PDFNEWNET-39345 in our issue tracking system. We will further
look into the details of this problem and will keep you updated on the status
of correction. Please be patient and spare us little time. We are sorry for
this inconvenience.

Hi Christian,


Adding more to my previous post, can you please confirm if the problem related to Exception which you are experiencing is occurring for certain image files or its occurring for all the images you use. In case its related to specific images, can you please share the image files, so that we can again test the scenario at our end.

Hi

Thank you for your reply. This problems are related to any images.

May I ask you, when you would think you can fix this problem? For us this is very urgent, because we have to deliver something in the next one or two weeks!

What I have to say also is, that I am disappointed about that behaviour. Whe had an issue in the barcode library https://forum.aspose.com/t/10389 and had to wait 1.5 years for a new version that is working. Now we spent 4.5 K in the new library and the very first thing I have tested comes to an issue!

Regards

Christian

Hi Christian,


Thanks for sharing the details.

Please note that our utmost intention is to get the issues fixed ASAP but sometimes due to some priority issues or due to complexity of issue (dependency of resolution over the implementation of new feature), the resolution takes sometime. Anyways, as soon as the above stated issue is resolved, we will let you know.

The issues you have found earlier (filed as PDFNET-39345) have been fixed in Aspose.PDF for .NET 18.3. This message was posted using BugNotificationTool from Downloads module by asad.ali