Sami
Sami

Reputation: 11

Print PDF file using Foxit Reader and C#

My program prints silently via the PDF reader Foxit Reader in a new process.

On occasion, my program attempts to print two PDFs at the same time, which causes one of them to fail to print.

Here is my code:

string filename = "file.pdf";

string fileDir1 = @"C:\Program Files (x86)\Foxit Software\Foxit Reader\FoxitReader.exe";

Process pdfProcess = new Process();
pdfProcess.StartInfo.FileName = fileDir1;
pdfProcess.StartInfo.Arguments = string.Format(@"/t {0} {1}", filename ,"pos-80");
pdfProcess.StartInfo.CreateNoWindow = true;
pdfProcess.StartInfo.WorkingDirectory = Path.GetDirectoryName(fileDir1);
pdfProcess.Start();

if (!pdfProcess.WaitForExit(2500))
{
    pdfProcess.Kill();

}

Please help me resolve this issue.

Upvotes: 1

Views: 3208

Answers (1)

WonderWorker
WonderWorker

Reputation: 9062

Make sure you have Foxit open.

using System.Diagnostics;

List<Process> Processlist = Process.GetProcesses().ToList();

This gives you a list of currently running processes.

foreach(Process p in Processlist)
{
    Console.WriteLine("Process " + p.Id + " is named '" + p.ProcessName + "'");

}

When running the above code, you should see the name of the Foxit process in your output window.

Alternatively, put a break-point on the foreach line and hover over the list to see all the names that way.

bool IsFoxitProcessRunning = false;

foreach(Process p in Processlist)
{
    if(p.ProcessName == "Foxit process name here") //Replace with the name of the foxit process
    {
        IsFoxitProcessRunning  = true;

    }

}

Now, only start a new Foxit process if one isn't already running.

if(!IsFoxitProcessRunning)
{
    //insert code to run next foxit process here.

}

Notes:

  • You may need to implement a queue to keep track of pdfs waiting to run.

  • You may also wish to alert IT support if a Foxit is waiting more 5 or 10 mins to run.

  • You could choose to make Processlist a class attribute, and periodically refresh Processlist using a Timer, by calling Processlist = Process.GetProcesses().ToList(); on the Tick event. every 30 seconds or so while a PDF is waiting to be printed.

Upvotes: 0

Related Questions