Error handling for Convert.ToPdf

I’m using the Convert to Pdf module to take Office documents and convert them to PDF, like this:

try
{
using var pdfDoc = new PDFDoc();
Convert.ToPdf(pdfDoc, inputFile);
pdfDoc.Save(outputFile, SDFDoc.SaveOptions.e_linearized);
return true;
}
catch (Exception e)
{
// I want to log errors here
return false;
}
It works fine, but I want to ensure I can trap errors and write them to a log if a user passes it a bad/corrupt document. As a test, I pass it a 0-byte file called Test.pptx, and instead of error getting thrown so that I can log it, a dialog box from PowerPoint itself gets prompted that there are no slides to print. This application is an unattended polling application, and I want to suppress any Office dialog prompts when bad documents get submitted.

Officially MS Office does not support unattended interaction, so while our SDK does best effort to avoid the issue, there can be cases where you would need to detect no conversions are happening and terminate running instances, as a sort of cleanup. Fortunately, at least with .NET Framework, this is just a few lines of C#.

Otherwise, the alternative is to switch to our internal, dependency free, office converter. You can do this by calling the following.

pdftron.PDFNet.Initialize(); // add your license key here if you have one
pdftron.PDF.Convert.Printer.SetMode(pdftron.PDF.Convert.Printer.Mode.e_prefer_builtin_converter);
...
Convert.ToPdf(pdfDoc, inputFile);

To learn more about our internal office converter see.

That makes sense. Unfortunately, from my understanding the dependency-free Office converter would require an additional license, correct?

I could do some kind of periodic checking for running stances of Office dialogs, etc, and kill them, though that approach does seem a bit messy.

I’m thinking I could also use the Convert.OfficeToPDF as kind of a pass-through, even without the additional license. In other words, pass each document to the Convert.OfficeToPDF just to see if it passes/fails. If it passes, then continue to the Convert.ToPdf, which our license does cover. It it fails, then write it to the error log. Would that be an appropriate workaround?

Office converter would require an additional license, correct?

It is a paid add-on yes.

I’m thinking I could also use the Convert.OfficeToPDF as kind of a pass-through,

The API Convert.ToPdf is a sort of swiss-army knife API that triggers different code paths based on the input file path, but also based on licensed features and what resources it can locate on the system (e.g. MS Office). Under the hood ToPdf may call other public API’s, such as OfficeToPDF if that feature is licensed.

On the other hand Convert.OfficeToPDF is the API to the internal converter, so if you did not have license for it, then it will always throw an exception.

I could do some kind of periodic checking for running stances of Office dialogs, etc, and kill them, though that approach does seem a bit messy.

You could always interact with MS Office directly yourself, here is a GitHub repo showing an example using C# code and MS assemblies. You would then be able to better manage the instances and deal with the automation issues.