Articles in this section
Category / Section

How to create PDF from a Windows service?

1 min read

To create PDF from a Windows service:

  1. In the Visual Studio, choose File->New->Project.
  2. In the list of Visual C# project templates, choose Windows Service and name the Project, for example, MyPdfService, and then click ok.
  3. In the solution explorer, rename the service1.cs to PdfService.cs.
  4. PdfService.cs contains two default functions, OnStart() and OnStop(). Write the appropriate codes for these two functions. The following code example shows how to merge a PDF document in the Windows Services.

C#

protected override void OnStart(string[] args)
        {
           //Creates a PDF document.
            PdfDocument doc = new PdfDocument();
           //Adds a page.
            PdfPage page = doc.Pages.Add();
            //Creates PDF graphics for the page.
            PdfGraphics g = page.Graphics;
            //Creates a solid brush.
            PdfBrush brush = new PdfSolidBrush(System.Drawing.Color.Black);
           //Sets the font.
            PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 36);
            //Draws the text.
            g.DrawString("Hello world!", font, brush, new PointF(20, 20));
            //Saves the document.
            doc.Save("HelloWorld.pdf");
            //Closes the document.
            doc.Close(true);
        }
  1. In the solution explorer, right click the PdfService.cs and choose View Designer. Now, the designer window is opened.
  2. Right Click the Designer window and choose AddInstaller. By default, a component class that contains two installers is added to your project. The component’s name is ProjectInstaller.
  3. In the design view of the ProjectInstaller, choose serviceInstaller1 in the property window, and set the ServiceName as PdfService.
  4. In the design view of the ProjectInstaller, choose ServiceProcessInstaller1. In the property window, set the Account property as LocalSystem.

 

Installing the service:

  1. Run the Visual Studio Developer Command Prompt as the Administrator.
  2. In the command Prompt window, navigate the folder that contains your project bin file. For example, visual studio 2012\Projects\MyPdfService\bin\Debug.
  3. Then enter the following command: installutil.exe MyPdfService.exe

Now, the service is installed successfully.

 

Start the Service:

  1. Open administrative tools and choose services. You should see PdfService in the services window.
  2. In the services window, right click the PdfService and select start. Now, the Windows Service is started.

 

Uninstalling the service:

Enter the following command for the Visual Studio Developer Command Prompt: installutil.exe /u MyPdfService.exe

Now, the service is uninstalled.

 

Sample Link:

https://www.syncfusion.com/downloads/support/directtrac/general/MyPdfService-644014439.zip


Conclusion
I hope you enjoyed learning on how to create PDF from a Windows service.

You can refer to our WinForms PDF feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our WinForms PDF example to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!
Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied