views:

547

answers:

3

I have a Windows Application written in C# VS 2008. The purpose of this application is to convert any file to PDF files. I have found code that works on converting the files however there is a small issue that I am coming across.

First here is the code:

        private void PrintToAdobePDF(string strInputFilePath)
    {
        ProcessStartInfo pProcInfo = new ProcessStartInfo();
        bool blResult;

        blResult = SetDefaultPrinter(D2P_Adobe_Printer);
        if (blResult)
        {
            pProcInfo.FileName = strInputFilePath;
            pProcInfo.Verb = "Print";
            pProcInfo.CreateNoWindow = true;
            pProcInfo.WindowStyle = ProcessWindowStyle.Hidden;

            Process pProc = Process.Start(pProcInfo);
            pProc.WaitForExit(1000);
            pProc.CloseMainWindow();
            pProc.Close();
        }

The issue I am having is that when the Process.Start() method is invoke it is running with TWO verbs instead of the one verb I specified ("Print"). It is running "Open" and "PrintTo" which is making the application that the original file is derived from open up and hang the application (i.e. jpg opens the Windows Fax and Picture Viewer). My question is how do I just use the "Print" verb within the Process.Start() method?

Thank you in advance

+1  A: 

What are sending in for strInputFilePath? The documentation says to only send the filename so if you are sending the whole path that could be causing the issue.

ajdams
I am sending the path and the original full filename (extension included) (the path is needed because this will ultimately be pulling files from a server. Should I just be sending the filename WITHOUT the extension?
mattgcon
According to the documentation yes..and if the solution lives on the same serve it should be okay.
ajdams
+1  A: 

No, sending the filename without the extension will fail.

Ultimately using System.Diagnostics.Process to print any arbitrary file is going to be unpredictable at best. It's all up to how your operating system handles each type of file, and whether or not your registry is properly configured to handle that file.

I'd guess that printing .doc files in this manner probably works OK, while other file types may not work so well.

In my opinion, you should find some constraint about the kinds of files you'll allow to "automagically" print, and build working solutions per type of file. Otherwise, you'll find a lot of unpredictable behavior.

Doug
That makes perfect sense. I did have a method for converting image files to PDF however, stupid me erased it and didnt save the code anywhere. Besides it wasnt that efficient and the images were getting messed up.
mattgcon
+1  A: 

Have you tried researching if it's possible to execute Adobe Reader with a command line parameter that accomplishes the same thing? Relying on the shell is iffy sometimes.

kprobst
I am a good programmer but I have not worked with command line parameters within C# before. Any help on that would be great. I do have the Adobe 9 SDK at my disposal.
mattgcon
Look at http://support.adobe.com/devsup/devsup.nsf/docs/52080.htm You should be able to execute the reader program directly, pass the path to the PDF file and include the switch that will print it.
kprobst
The suggestion about executing a command line parameter worked perfectly, thank you.
mattgcon