Add pages to existing PDF document using Aspose.PDF for .NET - An exception is thrown as object reference

I have a program that loops through Microsoft Office files, converts them to a PDF steam, and then adds them to a single PDF.

When adding the converted Word documents to the main PDF I receive the following error:

Object reference not set to an instance of an object.

Aspose.Total for .NET
Words 16.3.0.0
PDF 11.5.0.0

Sample Code:

Document masterPDF = new Document();
masterPDF.Pages.Add(Convert2PDF(fileName).Pages);

public static Document Convert2PDF(string path)
{
MemoryStream stream = new MemoryStream();

switch (Path.GetExtension(path).ToLower())
{
case ".pdf":
Document doc = new Document(path);
return doc;

case ".xls":
Workbook workbook = new Workbook(path);
workbook.Save(stream, Aspose.Cells.SaveFormat.Pdf);
break;
case ".doc":
case ".docx":
Aspose.Words.Document word = new Aspose.Words.Document(path);
WarningCallback warningCallback = new WarningCallback();
warningCallback.FileName = Path.GetFileName(path);
word.WarningCallback = warningCallback;
word.Save(stream, Aspose.Words.SaveFormat.Pdf);
break;
case ".ppt":
using (Aspose.Slides.Presentation pres = new Aspose.Slides.Presentation(path))
{
pres.Save(stream, Aspose.Slides.Export.SaveFormat.Pdf);
}
break;

default:
return new Document();
}

return new Document(stream);
}

public class WarningCallback : Aspose.Words.IWarningCallback
{
public string FileName { set; get; }
public void Warning(Aspose.Words.WarningInfo info)
{
//Log Warnings
}
}

Hi Jeff,

Can you please also share the input documents or the PDFs you are trying to merge?

Best Regards,

Thanks Muhammad,

I’m no longer having an issues, I have updated to the latest versions of PDF and Word.

Jeff

Hi Jeff,

Good to know that the issue has been resolved and thanks for sharing the details with us.

Best Regards,