Category / Section
How to print document in windows service using WinForms PDF Viewer?
1 min read
The user can print a PDF document in Windows Service using the silent printing feature supported by the Syncfusion WinForms PDF Viewer.
Steps to Print the PDF document in Windows service:
The following steps illustrate the printing of a PDF document in a windows service using WinForms PDFViewer.
- Create a Windows Service application and add the necessary features to the application as instructed in the below link.
- Define the printing process to print the PDF document silently using PdfViewerControl in a separate method.
C#
void PrintPDF() { //Initialize the PdfViewer PdfViewerControl pdfViewerControl = new PdfViewerControl(); // Load the PDF document to be printed pdfViewerControl.Load(@"../../Data/Barcode.pdf"); // Print the PDF document silently using the printer’s name. pdfViewerControl.Print("Pass your printer’s name here..."); }
Note:
We should specify the printer’s name by passing it as a parameter in the Print method as mentioned in the above code example. Otherwise, the service is unable to detect the printer even if it is a default printer.
- Perform the printing process as a single threaded operation (STA), when the service is started.
C#
protected override void OnStart(string[] args) { // Create and Initialize the thread Thread thread = new Thread(PrintPDF); //Set the threading model to STA thread.SetApartmentState(ApartmentState.STA); thread.Start(); }