How to print PDF files from Windows Service?


Is it possible to call pdfprintsdk.dll from a Windows-service ?
I tried it but I keep getting nRet=-4 ...

My test-code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Runtime.InteropServices;
using System.IO;

namespace Service
        public partial class Service1 : ServiceBase
                private static string LicenseKey = null;
                private static string PrintCommandTemplate =
                              "pdfprint -$ \"[LicenseKey]\" -printer \"\" \"\"";
                public static extern int VeryPDF_PDFPrint(string CommandLine);
                public Service1()
                        if (!System.Diagnostics.EventLog.SourceExists("MySource"))
                                    ("MySource", "MyNewLog");
                        eventLog1.Source = "MySource";
                        eventLog1.Log = "MyNewLog";
                protected override void OnStart(string[] args)
                        string strPDFFile = (@"D:\ASCIW\EXENET\LIB\
                        eventLog1.WriteEntry("Printing "+strPDFFile+" ...");
                        long nRet = PrintDoc(strPDFFile, "Brother HL-5450DN");
                protected override void OnStop()
                public static long PrintDoc(string FullDocumentName, string PrinterName)
                        string PrintCommand = PrintCommandTemplate;
                                if (LicenseKey != null)
                                        PrintCommand = PrintCommand.Replace
                                        ("[LicenseKey]", LicenseKey);
                                        PrintCommand = PrintCommand.Replace
                                        ("[LicenseKey]", "XXXXXXXXXXXXXXXX");
                                PrintCommand = PrintCommand.Replace("", PrinterName).Replace
                                        ("", FullDocumentName);
                                return VeryPDF_PDFPrint(PrintCommand);
                        catch (SyntaxErrorException)
                                return -999;


Because Windows System user account hasn't network ability, so when you call pdfprint.exe from Windows Service, you will not able to use network printer, because network is prohibited in System user account.

We have a solution to allow you to print PDF files from Windows Service, please look at following web pages for more information,

The general solution is switch user account from System to Administrator or other interactive user accounts, you can run the service from Administrator user account directly or use CmdAsUser.exe to call pdfprint.exe from Administrator user account, this will overcome the limitation of System user account.

You can also grant Network Access ability to System user account, if you like to do, please refer to following web pages,

Please also refer to following web pages for more information,

You can also use VeryPDFComRunCmd COM to call pdfprint.exe from Windows Service,


VN:F [1.9.20_1166]
Rating: 10.0/10 (1 vote cast)
VN:F [1.9.20_1166]
Rating: 0 (from 0 votes)
How to print PDF files from Windows Service?, 10.0 out of 10 based on 1 rating

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

Verify Code   If you cannot see the CheckCode image,please refresh the page again!