Click here to Skip to main content
15,886,857 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Ok I think I found good solution to print PDF Files On Fly by using Sumatra PDF[^]
It's far more better than Acrobat Reader Solution since it don't open any window and it can be shipped with application startup path since there is portable version of it.


static void Main()
        {
            ProcessStartInfo startInfo = new ProcessStartInfo();
            startInfo.FileName = "SumatraPDF.exe";
            string args = string.Format("-print-to-default \"{0}\" -exit-when-done", "xx.pdf");
            startInfo.Arguments = args;
            startInfo.CreateNoWindow = true;
            startInfo.ErrorDialog = false;
            startInfo.UseShellExecute = false;
            Process process = Process.Start(startInfo); Console.WriteLine("Start Print.");
            //process.Exited += process_Exited;
        Recheck:
            if (!process.HasExited)
            {
                Console.WriteLine("Wait 1 Sec.");
                process.WaitForExit(1000);
                goto Recheck;
            }
            else
            {
                Console.WriteLine("Print Finish.");
                Console.ReadLine();
            }
        Console.ReadLine();
}


the only thing worry me here is "Goto" that's why I ask for others opinion before I implement it into my project.

Note #1: I care so much for performance and memory and I'm afraid of if the process gone Not Responding for some reason it may make my application hang waiting it till finish, Only work around I can think about that can solve this is to count how many seconds past and if(sec>30) for example I kill the process.

Notes #2: I will use this code mainly in loop to print multiply PDF files about 10 or more and every file can contain more than 1 page to print.

So is this good enough? or is there better way ?

Thanks.
Tor.
Posted
Comments
[no name] 2-Nov-16 2:55am    
Maybe, you could try some other component to print PDF silently, here is a sample -http://code.msdn.microsoft.com/Print-PDF-file-in-C-5b6e66b7#content

1 solution

C#
While (!process.HasExited) {
    Console.WriteLine("Wait 1 Sec.");
    process.WaitForExit(1000);
}
Console.WriteLine("Print Finish.");
Console.ReadLine();


Good luck.
 
Share this answer
 
Comments
tornadofay 6-Nov-15 14:48pm    
thanks,I don't know how i didn't see this.
any more suggestions ?
tornadofay 17-Nov-15 15:31pm    
thanks for your reply,
I will try the Community Edition, although I use MoonPdfPanel - A WPF-based PDF Viewer Control[^] to display PDF combined with the above Method for printing and it's running pretty good.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900