1

I'm aware similar questions have been asked in the past, but they seem to be from circa five years ago, and was wondering if there is any more information on this.

I have the following code, which works well when ran as a desktop application:

var printServer = new PrintServer();
var printerQueues = printServer.GetPrintQueues();
var printer = printerQueues.FirstOrDefault(x => x.Name.Equals(sPrinter));

if (printer != null)
{
    try
    {
       printer.AddJob("Printing test file", sLoc, false);
       printJobSuccess = true;
    }
    catch (Exception)
    {
        // Log print fail 
    }
}

However, when I build it into a windows service it doesn't run. Similar to this question (Xps printing from windows service) I have also tried Aspose, but that doesn't work either.

I've also tried printing using Aspose.Words and the XpsPrint API (see: http://www.aspose.com/docs/display/wordsnet/How+to++Print+a+Document+on+a+Server+via+the+XpsPrint+API) - updated Nov 14, 2015 and suggests a way around printing XPS files for Windows Service applications. See below:

using (Stream stream = new FileStream(sLoc, FileMode.Open, FileAccess.Read))
{
    XpsPrintHelper.Print(stream, sPrinter, "Printing test file", true);
}

I've seen a lot of people comment saying they're still looking for a solution to this. Has anyone achieved it or know of a workaround / way of doing so? Or am I doing something wrong?

Community
  • 1
  • 1
  • Please note that the main goal of [this](http://www.aspose.com/docs/display/wordsnet/How+to++print+a+document+on+a+server+via+the+xpsprint+api) article is to show how to print a word processing document from an ASP.NET or Windows Service application using Aspose.Words and the XpsPrint API. Please make sure that your printer is properly configured. You may try following code snippet for testing at your end in the Windows Service. This code does not use Aspose.Words at all. So if it will not work, the problem is somewhere on your side. This code just prints an image. – Tahir Manzoor Feb 10 '16 at 06:16
  • PrintDocument doc = new PrintDocument(); doc.PrinterSettings.PrinterName = @"\\192.168.0.2\hp LaserJet 1010 Series Driver"; doc.PrintPage += Doc_PrintPage; doc.Print(); //------------------------------------- private void Doc_PrintPage(object sender, PrintPageEventArgs e) { e.Graphics.DrawImage(Image.FromFile(@"Common\test.jpg"), e.MarginBounds.Left, e.MarginBounds.Top); } – Tahir Manzoor Feb 10 '16 at 06:36
  • Did you find any solution for your problem? – bahramzy Mar 22 '21 at 10:20

0 Answers0