Error occurs when converting into PNG with a certain input PDF file

Dear Aspose,

I’m trying to convert PDF files into PNG by each page.
However depending on input PDF file, an error as below occurs and conversion process stops.

Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object.
   at #=zjr9I_kCMLB35JyqolpZ05aVuiJDWBtV45w==.#=zWxbtAr4UJj6M()
   at #=zjr9I_kCMLB35JyqolpZ05aVuiJDWBtV45w==.#=zdvRsufPdD57X(Int32 #=zPzmtuJHygu5K, Int32& #=zY4iO00vql6sJ)
   at #=zjr9I_kCMLB35JyqolpZ05aVuiJDWBtV45w==.#=zg1dWcME=(Int32 #=zPzmtuJHygu5K)
   at Aspose.Pdf.PageCollection.#=z8ptdGYBwmJRgbl0quYl63gs=(Int32 #=zyUBcjts=)
   at Aspose.Pdf.PageCollection.#=z7MJAYiY=(Int32 #=zyUBcjts=)
   at Aspose.Pdf.PageCollection.get_Item(Int32 index)
   at ConsoleApp1.Program.Main(String[] args) in /app/src/ConsoleApp1/Program.cs:line 260

“The line 260” falls on “pngDevice.Process(pdfDocument.Pages[pageCount], imageStream);” in the part concerned of my script.

String filepath = args[0];

            String filename = Path.GetFileName(args[0]);
            // Make directories

            Directory.CreateDirectory(args[1] + "/" + filename);


            String txt_outpath = args[1] + "/" + filename + "/" + "txt";

            String png_outpath = args[1] + "/" + filename + "/" + "png";



            // Open document

            Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document(filepath);

            // Aspose.Pdf.Text.FolderFontSource source = new Aspose.Pdf.Text.FolderFontSource("../data/Fonts_windows");

            pdfDocument.Save(args[1] + "/" + filename + "/"+ "result.pdf");

            // Create Aspose.Pdf.RenderingOptions to enable font hinting

            RenderingOptions opts = new RenderingOptions();

            opts.UseFontHinting = true;

            for (int pageCount = 1; pageCount <= pdfDocument.Pages.Count; pageCount++)


                using (FileStream imageStream = new FileStream(png_outpath +"/"+ $"output{pageCount}.png", FileMode.Create))


                    // Create PNG device with specified attributes

                    // Width, Height, Resolution, Quality

                    // Quality [0-100], 100 is Maximum

                    // Create Resolution object

                    Resolution resolution = new Resolution(150);

                    PngDevice pngDevice = new PngDevice(resolution);

                    //// Set predefined rendering options

                    pngDevice.RenderingOptions = opts;

                    // Convert a particular page and save the image to stream

                    pngDevice.Process(pdfDocument.Pages[pageCount], imageStream);

                    // Close stream



                // Create TextAbsorber object to extract text

                TextAbsorber absorber = new TextAbsorber();

                // Accept the absorber for first page


                // Get the extracted text

                string extractedText = absorber.Text;

                // Create a writer and open the file

                TextWriter tw = new StreamWriter(txt_outpath +"/"+ $"output{pageCount}.txt");

                // Write a line of text to the file


                // Close the stream


Would you please give some clue to resolve this scenario?
I attached the input PDF file.
"20070215.pdf (934.3 KB)
And I find this error in the environment Linux (Ubuntu “18.04.5 LTS (Bionic Beaver)”) + Docker as well as Windows.


Would you kindly make sure that you are using Aspose.PDF for .NET 20.9 at your side. We tested using the same version and were unable to notice any issue in the Windows environment. Please let us know about the version which you are using at your side, we will further proceed to assist you accordingly.


Thank you for your reply.
I confirmed that the version is surely Aspose.PDF for .NET 20.9.
I’d appreciate that you could keep investigating.


Would you kindly share a sample console application which is able to replicate the issue in windows environment as well. This would help us investigating the scenario and address it accordingly.


Here is the console application. (10.6 KB)


In case the application above won’t work in Windows, I attached one which I have confirmed works fine one (14.1 KB)


We checked both console applications but they did not have any source/input file. Would you kindly also share the input file which we can use while testing the application.


Please use this pdf file as input.
( the same file I attached in the first post )
Sorry for not indicating it clearly.
20070215.pdf (934.3 KB)


Upon further investigation, we have found that the culprit was following line in your code:

pdfDocument.Save(args[1] + "/" + filename + "/"+ "result.pdf");

You were saving the Document after loading it and then trying to convert it into PNG. Please note that Document.Save() method acts like Dispose(). Once a document is saved, allocated resources are also freed from the memory. Which was why the API was unable to access page in order to convert it into PNG. Please try after removing that line or re-initializing Document object again after saving it.


Thank you for having found the cause !
I’ll fix the code and test it when I will be back to work from holidays until this weekend.


Please take your time while testing the suggested solution. In case you still face any issue, please let us know.


It works fine !
I rewrote the code so as to re-initialize Document object.
Thank you very much.


It is good to know that your issue has been resolved. Please let us know in case you face any further issue.