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

Free Support Forum - aspose.com

System.NullReferenceException: 'Object reference not set to an instance of an object.' exception when converting a document fron DOCx to PDF

Hai,

I was trying to convert a document from docx format to pdf. I am getting this exception "System.NullReferenceException: ‘Object reference not set to an instance of an object.’ for some documents. Other documents are converted fine. dont know why documents are throwing this error . Attaching one such file for reference
Business problems and Solutions (1).pdf (2.1 MB)

Using Aspose.PDF 22.5.0 version

Code:

class WithByteArray
{
    static void Main(string[] args)
    {
        
        string path = @"D:\POC\kb pdfs\";
        string fileName = "Business problems and Solutions (1)";

        string destFileName = path + fileName + ".pdf";

        byte[] pdfData = System.IO.File.ReadAllBytes(destFileName);

        byte[] wordData = null;

        byte[] data = convertPdfToDoc(destFileName);
        System.IO.File.WriteAllBytes(Path.Combine(path, "pdfToWord", fileName + "_converted.docx"), data);
     }

    private static byte[] convertPdfToDoc(string filePath)
    {
        byte[] pdfData = System.IO.File.ReadAllBytes(filePath);
        byte[] wordData = null;
        try
        {
            
            if (filePath != string.Empty)
            {
                DocSaveOptions saveOption = new DocSaveOptions();
                saveOption.Mode = DocSaveOptions.RecognitionMode.Textbox;
                saveOption.Format = DocSaveOptions.DocFormat.DocX;
                saveOption.RelativeHorizontalProximity = 2.5f;
                saveOption.AddReturnToLineEnd = true;
                saveOption.RecognizeBullets = true;

                using (MemoryStream memoryStream = new MemoryStream(pdfData))
                {
                    Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document(memoryStream);

                    using (MemoryStream outputStrteam = new MemoryStream())
                    {
                        pdfDocument.Save(outputStrteam, saveOption);
                        wordData = outputStrteam.ToArray();
                    }


                }
            }
        }

        catch(Exception ex)
        {

        }
        return wordData;
    }
}

@pooja.jayan

We have tested the scenario using the latest version of Aspose.PDF for .NET 22.5 and have not found the shared issue. Please make sure that you are using the latest version of Aspose.PDF for .NET 22.5 and same input PDF.

Hai,

I was using the same version of Aspose PDF for .NET
and the same code shared above.

@pooja.jayan

Could you please ZIP and attach the Visual Studio project that you are using? We will use the same project at our end for further testing. Thanks for your cooperation.