print existing pdf file
This uses the installed pdf reader to print the file against the default printer on the machine.
string path = "" <- your path here.
if (path.EndsWith(".pdf"))
{
if (File.Exists(path))
{
ProcessStartInfo info = new ProcessStartInfo();
info.Verb = "print";
info.FileName = path;
info.CreateNoWindow = true;
info.WindowStyle = ProcessWindowStyle.Hidden;
Process p = new Process();
p.StartInfo = info;
p.Start();
p.WaitForInputIdle();
System.Threading.Thread.Sleep(3000);
if (false == p.CloseMainWindow())
p.Kill();
}
}
Print Pdf in C#
A very straight forward approach is to use an installed Adobe Reader or any other PDF viewer capable of printing:
Process p = new Process( );
p.StartInfo = new ProcessStartInfo( )
{
CreateNoWindow = true,
Verb = "print",
FileName = path //put the correct path here
};
p.Start( );
Another way is to use a third party component, e.g. PDFView4NET
How can I print files (eg. pdf) directly from within C# code?
"Print a file" is rather vague.
If you can find a .NET library (or write your own) to open the specific type of file, read the content, then render the content, you can use the in-built .NET classes such as the FixedDocument class (referenced in Auto print without dialog ) to build up your print output and send it to a printer.
If you want to print an arbitrary file on the filesystem, and are assuming that there is a program installed that can open and print that type of file, and that the program installed a "Print" verb into the right-click menu, then you should be able to use the "ProcessStartInfo" method from Mark's answer above. That simulates the user right-clicking on the file and selecting an option named "Print".
Either way, printing on Windows with no user input is a separate problem in itself. The print dialogue that appears is often part of the driver for that specific printer (rather than the generic Windows printer dialogue) and includes options that are specific to that printer, such as duplexing, or selecting a specific paper tray, etc. Many of these drivers provide no programmatic method of setting these options at all, and those that do often need code that is specific to that driver. You can programmatically specify the generic options (such as number of copies), but any extra features will either be disabled or will use default values.
Hope this helps
Print PDF file and Doc file using C#
This has worked in the past:
using System.Diagnostics.Process;
...
Process process = new Process();
process.StartInfo.FileName = pathToPdfOrDocFile;
process.UseShellExecute = true;
process.StartInfo.Verb = "printto";
process.StartInfo.Arguments = "\"" + printerName + "\"";
process.Start();
process.WaitForInputIdle();
process.Kill();
To print to the default printer, replace printto
with print
, and leave off the Arguments
line.
Related Topics
Distinct in Linq Based on Only One Field of the Table
Differencebetween Linq to Xml Descendants and Elements
Converting from Ienumerable to List
Programmatically Get the Version Number of a Dll
Does Using Tasks (Tpl) Library Make an Application Multithreaded
How to Find Out If a File Exists in C#/.Net
Combobox- Selectionchanged Event Has Old Value, Not New Value
Copy File on a Network Shared Drive
Creating a Temporary Directory in Windows
ASP.NET Asmx Web Service Returning Xml Instead of JSON
How to Create a Xsd Schema from a Class
How to Use the C#6 "Using Static" Feature
How to Stop Flickering C# Winforms
How Do C# Events Work Behind the Scenes
How to Provide Custom Cast Support for My Class
Illustrating Usage of the Volatile Keyword in C#
Why Can't You Use the Keyword 'This' in a Static Method in .Net