How to print PDF files from Windows Service?

Hello,

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 \"\" \"\"";
               
                [DllImport("pdfprintsdk.dll")]
                public static extern int VeryPDF_PDFPrint(string CommandLine);
               
                public Service1()
                {
                        InitializeComponent();
                        if (!System.Diagnostics.EventLog.SourceExists("MySource"))
                        {
                                System.Diagnostics.EventLog.CreateEventSource
                                    ("MySource", "MyNewLog");
                        }
                        eventLog1.Source = "MySource";
                        eventLog1.Log = "MyNewLog";
                }
               
                protected override void OnStart(string[] args)
                {
                        string strPDFFile = (@"D:\ASCIW\EXENET\LIB\
                              pdfprint_sdk\bin\readme.pdf");
                        eventLog1.WriteEntry("Printing "+strPDFFile+" ...");
                        long nRet = PrintDoc(strPDFFile, "Brother HL-5450DN");
                        eventLog1.WriteEntry(nRet.ToString());
                }
               
                protected override void OnStop()
                {
                }
               
                public static long PrintDoc(string FullDocumentName, string PrinterName)
                {
                        string PrintCommand = PrintCommandTemplate;
                       
                        try
                        {
                                if (LicenseKey != null)
                                        PrintCommand = PrintCommand.Replace
                                        ("[LicenseKey]", LicenseKey);
                                else
                                        PrintCommand = PrintCommand.Replace
                                        ("[LicenseKey]", "XXXXXXXXXXXXXXXX");
                                PrintCommand = PrintCommand.Replace("", PrinterName).Replace
                                        ("", FullDocumentName);
                                return VeryPDF_PDFPrint(PrintCommand);
                        }
                        catch (SyntaxErrorException)
                        {
                                return -999;
                        }
                }
        }
}

Customer
--------------------------------------------------

image
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,

https://www.verypdf.com/wordpress/201109/how-do-i-call-pdfprint-from-windows-service-or-local-system-to-print-the-pdf-file-to-network-printer-2310.html

https://www.verypdf.com/wordpress/201109/pdf-printer-cant-print-pdf-file-to-network-printer-from-windows-service-2309.html

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,

http://stackoverflow.com/questions/510170/the-difference-between-the-local-system-account-and-the-network-service-acco

http://social.technet.microsoft.com/Forums/windowsserver/en-US/e50abc9a-2141-42ba-a6ee-b228a01acfe8/how-to-grant-network-access-to-local-user?forum=winservergen

http://serverfault.com/questions/135867/how-to-grant-network-access-to-localsystem-account

http://stackoverflow.com/questions/3439760/localsystem-vs-system-vs-local-system-windows-system-accounts

http://kb.ucla.edu/articles/local-system-accountnt-authoritysystem-and-network-resource-access

Please also refer to following web pages for more information,

https://www.verypdf.com/wordpress/201311/how-to-print-a-pdf-file-to-network-printer-from-windows-service-39509.html

https://www.verypdf.com/wordpress/201109/pdf-printer-cant-print-pdf-file-to-network-printer-from-windows-service-2309.html

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

http://www.verydoc.com/exeshell.html#VeryPDFComRunCmd_COM_Component

http://www.verydoc.com/blog/verydoc-release-notes-verydoc-releases-an-exe-com-of-verypdfcomruncmd-exe-today-verypdf-exe-com-does-allow-you-to-call-ms-office-and-any-exe-application-from-asp-php-c-net-etc-program-languag.html

VeryPDF

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!