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;
}
}