Free Support Forum - aspose.com

NullReferenceException during Postscript to PDF conversion for .NET

A NullReferenceException occurs when running the code found in the Postscript to PDF conversion example in the link below on the line “document.Save(device, options);”

https://docs.aspose.com/display/pagenet/Convert+PostScript+to+PDF

I have made sure that both streams, the device and options are not null, however the exception is thrown when calling the save function.

I am using Aspose.Page v20.6 on .NET Framework 4.7.1

@danielolaya

Would you kindly share your sample source PS/EPS file with us. We will test the scenario in our environment and address it accordingly.

Thank you for your response and assistance. I am currently running the code using this sample file. I have manually changed the file extension from .ps to .pdf, as .ps is not a supported upload format. Just change the file extension back to .ps and you should be able to use the sample file.

policy.pdf (44.4 KB)

@danielolaya

We have tested the scenario in our environment using following code snippet and were unable to notice any issue.

System.IO.FileStream pdfStream = new System.IO.FileStream(dataDir + "outputPDF_out.pdf", System.IO.FileMode.Create, System.IO.FileAccess.Write);
            // Initialize PostScript input stream
            System.IO.FileStream psStream = new System.IO.FileStream(dataDir + "policy.ps", System.IO.FileMode.Open, System.IO.FileAccess.Read);
            // If you want to convert Postscript file despite of minor errors set this flag
            bool suppressErrors = true;
            Aspose.Page.EPS.PsDocument document = new Aspose.Page.EPS.PsDocument(psStream);

            //Initialize options object with necessary parameters.
            Aspose.Page.EPS.Device.PdfSaveOptions options = new Aspose.Page.EPS.Device.PdfSaveOptions(suppressErrors);
            // If you want to add special folder where fonts are stored. Default fonts folder in OS is always included.
            //options.AdditionalFontsFolders = new string[] { @"{FONT_FOLDER}" };

            // Default page size is 595x842 and it is not mandatory to set it in PdfDevice
            Aspose.Page.EPS.Device.PdfDevice device = new Aspose.Page.EPS.Device.PdfDevice(pdfStream);
            // But if you need to specify size and image format use following line
            //Aspose.Page.EPS.Device.PdfDevice device = new Aspose.Page.EPS.Device.PdfDevice(pdfStream, new System.Drawing.Size(595, 842));

            try
            {
                document.Save(device, options);
            }
            finally
            {
                psStream.Close();
                pdfStream.Close();
            }

            //Review errors
            if (suppressErrors)
            {
                foreach (Aspose.Page.EPS.PsConverterException ex in options.Exceptions)
                {
                    Console.WriteLine(ex.Message);
                }
            }

outputPDF_out.pdf (4.1 MB)

Would you kindly share a sample console application that is able to reproduce the error in our environment. We will again test the scenario and address it accordingly.

Sure. I could not upload a zipped file containing the application. However, this is the code used that presents the problem on my environment. The exception is still being thrown at the “Document.save(device,options)” line.

using System;

namespace testAspose
{
    class Program
    {
        static void Main(string[] args)
        {
            asposeConvert();
            return;
        }
        public static void asposeConvert()
        {

            var path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
            string dataDir = path.Substring(6);

            System.IO.FileStream pdfStream = new System.IO.FileStream(dataDir + "\\outputPDF_out.pdf", System.IO.FileMode.Create, System.IO.FileAccess.Write);
            // Initialize PostScript input stream
            System.IO.FileStream psStream = new System.IO.FileStream(dataDir + "\\policy.ps", System.IO.FileMode.Open, System.IO.FileAccess.Read);
            // If you want to convert Postscript file despite of minor errors set this flag
            bool suppressErrors = true;
            Aspose.Page.EPS.PsDocument document = new Aspose.Page.EPS.PsDocument(psStream);

            //Initialize options object with necessary parameters.
            Aspose.Page.EPS.Device.PdfSaveOptions options = new Aspose.Page.EPS.Device.PdfSaveOptions(suppressErrors);
            // If you want to add special folder where fonts are stored. Default fonts folder in OS is always included.
            //options.AdditionalFontsFolders = new string[] { @"{FONT_FOLDER}" };

            // Default page size is 595x842 and it is not mandatory to set it in PdfDevice
            Aspose.Page.EPS.Device.PdfDevice device = new Aspose.Page.EPS.Device.PdfDevice(pdfStream);
            // But if you need to specify size and image format use following line
            //Aspose.Page.EPS.Device.PdfDevice device = new Aspose.Page.EPS.Device.PdfDevice(pdfStream, new System.Drawing.Size(595, 842));

            try
            {
                document.Save(device, options);
            }
            finally
            {
                psStream.Close();
                pdfStream.Close();
            }

            //Review errors
            if (suppressErrors)
            {
                foreach (Aspose.Page.EPS.PsConverterException ex in options.Exceptions)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }
    }
}

Thank you

@danielolaya

We have used the same code snippet in our environment to test the scenario again and did not notice any issue. We tested the scenario using Aspose.Page for .NET 20.6. Would you kindly make sure that you are using latest version of the API?