Save PDF to Stream in C# - System.ArgumentNullException: Value cannot be null. Parameter name: pdfObject

Hi,

Following file Test.pdf (226.8 KB) cannot be saved to stream after load using latest Aspose.PDF.

Code:

using (var memory = new System.IO.MemoryStream())
using (var pdf = new Aspose.Pdf.Document("Test.pdf"))
{
    pdf.Save(memory);
}

System.ArgumentNullException: Value cannot be null.
Parameter name: pdfObject
Stack Trace:
#=zSOP84LlsPfp3wdfx_rD5StCwF3tn.#=zHPwqB0c=(#=zvujjjfT38ycX2manl0C5rmxgBZ4qq6EKLw== #=zPsLdZsOkikAL)
#=zRqQdYDh3WYzqQNPvmP1sf33fGxBY.#=z_4OPO1rMtjC4()
#=zRqQdYDh3WYzqQNPvmP1sf33fGxBY.#=znxlrIYM=(Stream #=z3z$F55E=)
Document.#=zh7YHIaLJTSkO(Stream #=z$CIfKyo=, SaveOptions #=zgGb7E$RCz7_R)
Document.Save(Stream output)

Thanks.

@WorkZone

We were able to replicate the issue in our environment with Aspose.PDF for .NET 20.4 and have logged it as PDFNET-47951 in our issue tracking system. We will surely work to resolve it and keep you updated with its resolution status. Please be patient and spare us some time.

We are sorry for the inconvenience.

@asad.ali
Has this been fixed yet or is it still in progress?
Because i encoutered this exact case when converting a pdf to pdf-a within a docker container.
Using Aspose.PDF 20.10.0 on .Net Core 3.1

Code:

sourcePdf.Convert(logStream, PdfFormat.PDF_A_3B, ConvertErrorAction.None)

Exception:

System.ArgumentNullException: Value cannot be null. (Parameter ‘key’)
at System.Collections.Generic.Dictionary2.FindEntry(TKey key) at System.Collections.Generic.Dictionary2.TryGetValue(TKey key, TValue& value)
at #=zKJdTtvmPovuYF34ftul_qeDvr15AmL0I4g==.#=zZJwb_Eg=(#=z9R2f5vM= #=zBg9spEs=)
at #=zKJQcramcEoB9rhEl41u7CUPW3PSjfL0dOg==.#=zIz2huD_saJWP.#=zpfWUX6BdSInL(#=z9R2f5vM= #=zBg9spEs=, #=z7VvkNh0=& #=zqPraf6A=)
at #=zk9rdEbv879fNF5od57qb_UhgNL0CpssF7f8vZ$Q=.#=z9HjgC7Q6FDaA(#=zAwsqtPGBY0z8CK2enrNnh1lnhdRw #=zbywhCBs=, #=zolyaG9M68lYdAGBuxsamec8XJRXv #=zIDnOVs8iUXnN)
at #=zc$uQjzsVN2$Tas6TMYL5evEBY9YfjettOZXZJb4MSTf3udTRHjqov18=.#=zZi763YNxb8Dm(#=zigVcVy0kF4TRalKjQfSZwcYEHcgRyhXISOYJy89zQBEgDdBz12CmBt8= #=zgPaE9kU=, #=z3tDFAvFbCZ4PoKhyWn1BZYH2ZAOoB3i6FLnXqfmLsh9o #=zYU9E9JQ=, Page #=zcwftyS8=, #=zL771QQSY2vALEHFs_bOiZy_Uo9hALzgCfvgL2S2PnFBhO61cOg== #=zAj5bAswTHXoo)
at #=zc$uQjzsVN2$Tas6TMYL5evEBY9YfjettOZXZJb4MSTf3udTRHjqov18=.#=zSc$iSnLB5aNb(Page #=zcwftyS8=, OperatorCollection #=zcPnyEPM=, Resources #=zSZt9qIM=, Int32 #=z4pyhXPRAjx4J, #=zL771QQSY2vALEHFs_bOiZy_Uo9hALzgCfvgL2S2PnFBhO61cOg== #=zAj5bAswTHXoo)
at #=zc$uQjzsVN2$Tas6TMYL5evEBY9YfjettOZXZJb4MSTf3udTRHjqov18=.#=zD5ObXzSZT2Bw(Page #=zcwftyS8=, #=zL771QQSY2vALEHFs_bOiZy_Uo9hALzgCfvgL2S2PnFBhO61cOg== #=zAj5bAswTHXoo)
at #=zc$uQjzsVN2$Tas6TMYL5evEBY9YfjettOZXZJb4MSTf3udTRHjqov18=.#=zTvpcgTk=()
at #=zc$uQjzsVN2$Tas6TMYL5evEBY9YfjettOZXZJb4MSTf3udTRHjqov18=.#=z2rZQioQ=()
at #=zij1$XKXLjxO8cjD2N8zOgUrZzPRdoEhLjgo4e7bwlcblQ71TBFFKqlI=.#=zwkWXW7g=()
at #=zc$uQjzsVN2$Tas6TMYL5evEBY9YfjettOZXZJb4MSTf3udTRHjqov18=.#=zQIB0o7k=(XmlTextWriter #=zEkj9eM0=, PdfFormat #=zlui$idU=, Document #=zT1tPKRE=, Boolean #=zY2kGZtILlvwV, ConvertErrorAction #=zUDo4Jko=)
at Aspose.Pdf.Document.#=zwilC$ME=(XmlTextWriter #=zEkj9eM0=, PdfFormat #=zlui$idU=, Boolean #=zY2kGZtILlvwV, ConvertErrorAction #=zUDo4Jko=)
at Aspose.Pdf.Document.Convert(Stream outputLogStream, PdfFormat format, ConvertErrorAction action)

Thanks

@patrick9

Sadly, the ticket is not yet resolved. However, we have updated its information based upon your post and will surely inform you as soon as it is fixed. Please be patient and spare us some time.

We apologize for the inconvenience.

I encountered this error today (5/28/21) and it still seems to be an issue. We’re attempting to revert to the 19.x release to circumvent the problem but would appreciate an update or fix. I’m using the 21.5 release and converting from a PDF to a JPG file using the code sample from the documentation.

Thanks!

@neofonda

We apologize for the inconvenience being caused due to this issue. We have recorded your concerns and have updated the information of the ticket accordingly. We will surely work on resolving the issue and update here as soon as it is resolved. Please give us some time.

Is there an alternative approach that we can use to convert a pdf to a jpg in a docker container using .NET Core 3.1?

@neofonda

Please share which approach you are using already to convert PDF to JPG? We will check it and share another approach with you. Furthermore, would you please make sure if libgidplus package is installed in your environment along with all MS Essential Fonts Package (msttcorefonts).

After installing the Microsoft Fonts, please check if those fonts are present in “/usr/share/fonts/truetype/msttcorefonts” directory. Otherwise, you can use the below line of code to specify font directory so that API can use them:

Aspose.Pdf.Text.FontRepository.Sources.Add(new FolderFontSource("<user's path to ms fonts>"));

We’ve installed the fonts as well as gdilib and we continue to get the error. This is our conversion implementation within the container. We’ve tried both approaches with no success.

Thanks!

static bool ConvertPdf()
        {
            try
            {
                using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
                {
                    // Create PdfConverter object
                    Aspose.Pdf.Facades.PdfConverter objConverter = new Aspose.Pdf.Facades.PdfConverter();

                    FileStream fs = new FileStream(@"C:\TestFile.pdf", FileMode.Open);

                    // Bind input pdf file
                    objConverter.BindPdf(fs);

                    // Initialize the converting process
                    objConverter.DoConvert();

                    objConverter.CoordinateType = Aspose.Pdf.PageCoordinateType.CropBox;

                    // Check if pages exist and then convert to image one by one
                    while (objConverter.HasNextImage())
                        objConverter.GetNextImage(ms, System.Drawing.Imaging.ImageFormat.Jpeg);

                    ms.Position = 0;

                    Aspose.Imaging.Image Img_Thumb = Aspose.Imaging.Image.Load(ms);

                    Img_Thumb.Save(@"C:\TestFile.jpg");

                    // Close the PdfConverter object
                    objConverter.Close();
                }

                return true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);

                return false;
            }
        }

        static bool ConvertPdf2()
        {
            try
            {
                using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
                {
                    // Create PdfConverter object
                    //Aspose.Pdf.Facades.PdfConverter objConverter = new Aspose.Pdf.Facades.PdfConverter();

                    FileStream fs = new FileStream(@"C:\TestFile.pdf", FileMode.Open);

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

                    // Create Resolution object
                    Resolution resolution = new Resolution(300);
                    // Create Jpeg device with specified attributes
                    // Width, Height, Resolution
                    JpegDevice JpegDevice = new JpegDevice(500, 700, resolution);

                    // Convert a particular page and save the image to stream
                    JpegDevice.Process(pdfDocument.Pages[1], ms);

                    ms.Position = 0;

                    Aspose.Imaging.Image Img_Thumb = Aspose.Imaging.Image.Load(ms);

                    Img_Thumb.Save(@"C:\TestFile.jpg");
                }

                return true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);

                return false;
            }
        }

@neofonda

You are already using two available method to convert PDF to Image. We are afraid that we cannot offer any other alternative in order to carry out this functionality. Furthermore, we certainly need to investigate this error which is why the ticket information has been updated accordingly. We will surely investigate the ticket from this perspective and let you know as soon as we have some additional updates regarding its fix. Please give us some time.

We really apologize for the inconvenience caused.

Is there any resolution to this? its been more than a year?

Stack Trace:
12:08:33 at #=zc_Iw$jN6VZ3CeoK8_qFF2K2nTSMuEG5H7w==.#=z7ttHaydFiHiz()
12:08:33 at #=zc_Iw$jN6VZ3CeoK8_qFF2K2nTSMuEG5H7w==…ctor(Stream #=zczkUVwg=, String #=zVqXGCUg=, Boolean #=z7Y3DWIU6Qnzq)
12:08:33 at Aspose.Pdf.Document.#=zmEASFSs=(Stream #=z9OwVsxU=, String #=zVqXGCUg=)
12:08:33 at Aspose.Pdf.Document…ctor(Stream input)
12:08:33 at InfrastructureTest.Services.DocumentServices.PDFServiceTest.Concat_pdf_to_pdf_success() in /jenkins/workspace/rvices_f-1178/test/InfrastructureTest/Services/DocumentServices/PDFServiceTest.cs:line 44
12:08:33 Failed ConvertTxtToPDF_success [236 ms]
12:08:33 Error Message:
12:08:33 System.ArgumentNullException : Value cannot be null. (Parameter ‘key’)
12:08:33 Stack Trace:
12:08:33 at System.Collections.Generic.Dictionary2.FindValue(TKey key) 12:08:33 at System.Collections.Generic.Dictionary2.TryGetValue(TKey key, TValue& value)

@vinayakbaddi6

We are afraid that the earlier logged ticket has not been yet resolved. There were other issues in the queue logged prior to this and we have been working on resolving them. However, we will update you once we have certain updates about ticket resolution. Your patience is highly appreciated on this matter.

We apologize for the inconvenience.

The issues you have found earlier (filed as PDFNET-47951) have been fixed in Aspose.PDF for .NET 23.5.