Print PDF in C#

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

PDF Print Through Windows Service with C#

I ended up using pdfium to do the job. With that code, the PDF file is sent to the printer correctly even when the windows service is running under the LocalService user.

PrinterSettings printerSettings = new PrinterSettings()
{
PrinterName = printerName,
Copies = 1
};

PageSettings pageSettings = new PageSettings(printerSettings)
{
Margins = new Margins(0, 0, 0, 0)
};

foreach (PaperSize paperSize in printerSettings.PaperSizes)
{
if (paperSize.PaperName == "A4")
{
pageSettings.PaperSize = paperSize;
break;
}
}

using (PdfDocument pdfDocument = PdfDocument.Load(filePath))
{
using (PrintDocument printDocument = pdfDocument.CreatePrintDocument())
{
printDocument.PrinterSettings = printerSettings;
printDocument.DefaultPageSettings = pageSettings;
printDocument.PrintController = (PrintController) new StandardPrintController();
printDocument.Print();
}
}

Thanks for the answers guys.

Printing a PDF in C#

you try this

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();


Related Topics



Leave a reply



Submit