Articles in this section
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.

  1. Create a Windows Service application and add the necessary features to the application as instructed in the below link.

https://learn.microsoft.com/en-us/dotnet/framework/windows-services/walkthrough-creating-a-windows-service-application-in-the-component-designer

  1. 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.

  1. 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();
}

 

View Sample in GitHub

See Also:

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied