Category / Section
How to print PDF files in windows service?
You can print a PDF document in Windows Service using the silent printing feature supported by Syncfusion WPF PDF Viewer.
Please find the Steps to be followed to print a PDF document in Windows Service below:
Step 1: Create a Windows Service application and add the necessary features to the application as instructed in the below link:
Step 2: Define the printing process to print the PDF document silently using PdfViewerControl.
void PrintPDF()
{
PdfViewerControl pdfViewerControl = new PdfViewerControl();
// Load the PDF document to be printed.
pdfViewerControl.Load(“Pass the full path of the PDF file to be printed here…”);
// Print the PDF document silently using the printer name.
pdfViewerControl.Print(“Pass your printer name here…”);
}
Note:
You need to specify the printer name by passing it as a parameter in the Print method as mentioned in the above code example. Otherwise, the service cannot detect the printer even though it is a default printer.
Step 3: Perform the printing as single threaded, when the service starts.
protected override void OnStart(string[] args)
{
// Set the threading model to STA
Thread thread = new Thread(PrintPDF);
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
}