Printing from a .Net Service

Printing from a .NET Service

Trust me, you will spend more money trying to search/develop a solution for this as compared to buying a third party component. Do not reinvent the wheel and go for the paid solution.

Printing is a complex problem and I would love to see the day when better framework support is added for this.

Printing in a Windows Service

Printing in Windows Services is not recommended.

you need to use a different account for your service,( domain account) so that you can access network resources.

You can find more info at: Network printing with window service

Is it possible to print documents from a .net core 3.1 Windows Service?

Incredibly, we did manage to achieve the impossible - printing PDFs from a .net Core 3.1 Windows Service.

We use the FreeSpire.PDF v5.4.0 nuget package and the following code to print pre-generated pdf data, to a Zebra Label printer.

bool printedOK = true;
string printErrorMessage = "";
try
{
PdfDocument pdf = new PdfDocument(printJobResult.printJob.PrintData);
pdf.PrintSettings.PrinterName = jobInfo.PrinterAddress;
pdf.PrintSettings.DocumentName = jobInfo.Type == PrintJobType.Label ? $"Label {jobInfo.OrderNumber}" : $"DeliveryNote {jobInfo.OrderNumber}";
if(jobInfo.Type == PrintJobType.Label)
{
pdf.PrintSettings.PaperSize = new System.Drawing.Printing.PaperSize("Custom", _labelWidth, _labelHeight);
pdf.PrintSettings.SetPaperMargins(2, 2, 2, 2);
}
pdf.PrintSettings.SelectSinglePageLayout(Spire.Pdf.Print.PdfSinglePageScalingMode.FitSize, true);
_logger.LogDebug($"Paper Size - Width:{pdf.PrintSettings.PaperSize.Width} Height:{pdf.PrintSettings.PaperSize.Height} Name:{pdf.PrintSettings.PaperSize.PaperName} Kind:{pdf.PrintSettings.PaperSize.Kind} RawKind:{pdf.PrintSettings.PaperSize.RawKind}");

pdf.Print();
}
catch (Exception ex)
{
printErrorMessage = "Printing Error: " + ex.ToString();
printedOK = false;
}

Note to self - Do go and check the details of these following points...

Newer versions of the FreeSpire.PDF plugin don't allow printing, and I believe there are limits even with the 5.4.0 version (10 pages of printing I think), but for our purposes, the 5.4.0 version of the plugin has allowed us to create a tidy little delivery label print spooler, running as a Windows Service on a warehouse PC.

Printing from a Windows Service

The point is not how to print from a windows service or from an application, if you don't want any user interaction to be required you have to specify all print parameters without any need to show a print dialog ( which you can't because a windows service has no access to the UI ).

see here: http://msdn.microsoft.com/en-us/library/system.drawing.printing.printdocument.aspx



Related Topics



Leave a reply



Submit