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

Free Support Forum - aspose.com

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

Dear Aspose,

Hello.
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";

            Directory.CreateDirectory(txt_outpath);

            Directory.CreateDirectory(png_outpath);

            // 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

                    imageStream.Close();

                }

                // Create TextAbsorber object to extract text

                TextAbsorber absorber = new TextAbsorber();

                // Accept the absorber for first page

                pdfDocument.Pages[pageCount].Accept(absorber);

                // 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

                tw.WriteLine(extractedText);

                // Close the stream

                tw.Close();
           
            }

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.

@shun1985

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.

@asad.ali

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.

@shun1985

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.

@asad.ali

Here is the console application.
ConsoleApp1.zip (10.6 KB)

@asad.ali

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

@shun1985

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.

@asad.ali

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)

@shun1985

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.

@asad.ali

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.

@shun1985

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

@asad.ali

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

@shun1985

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