0

I am working on converting word docs and excel to pdf and automate the whole process. We have a server application that prints all the .doc and .xls files using COM interop and we want to have those files converted to PDF instead of printing them. I referred to How do I convert Word files to PDF programmatically? and also http://www.microsoft.com/en-us/download/details.aspx?id=7 to convert the documents to PDF and i was able to convert them. When i was doing some more research on the topic i found that it is not advisable to call Office Interop from Server Side application (Office documents to PDF), so i thought of using something like a PDF printer driver using which i can configure to bypass the Save As dialog. The i downloaded PDF995 and used the following code to test the output

string printerName = "PDF995";
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = (@"C:\...\test.XLS");
startInfo.Arguments = "\"" + printerName + "\"";
startInfo.CreateNoWindow = true;
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
//startInfo.UseShellExecute = true;
startInfo.Verb = "printto";

using (Process process = new Process())
{
    process.StartInfo = startInfo;
    process.Start();
    if (!process.HasExited)
    {
        process.WaitForExit();
    }
}

It works in my box and i need to test it in the server. I just realized while testing it, i am getting exception "No Process associated with this object" at the line (!process.hasexited) only for .xls files but not for .doc and .ppt files. I am wondering why, i tried to look at the Process Monitor (https://technet.microsoft.com/en-us/sysinternals/bb896645.aspx) but could not find it. But it does work for .doc files without throwing the exception. Any idea why?

I would also like to know if there is a better way to go about it? I did take a look at the libraries like Aspose to convert word to pdf but the problem was we may have to purchase separate modules for word and another one for excel and one for slides which would be expensive. So we thought of using a printer driver which would be ideal to handle different types of documents.

Community
  • 1
  • 1
Pete sam
  • 31
  • 1
  • 7
  • if you're looking to automate, you should use a console app, not a windows app. different compliation parameters, then you can use the console app from other programs – deltree Feb 24 '15 at 15:41
  • I suppose you need to have an associated application to get the file printed correctly. Try to look for third-party components to get the job done. – Eugene Astafiev Feb 25 '15 at 09:59
  • deltree - I was using console application to test the code and i thought it would work in windows form application too. – Pete sam Feb 26 '15 at 16:44

0 Answers0