For the past week or so I've been trying to print PDF files to our Zebra ZT220 printer from C# using the FREE Spire.PDF NuGet component. Everything went pretty smoothly when testing the functionality from a WinForms GUI, i.e. when clicking the 'Print' button I had put up there.
This all changed when I deployed the same code to run from a Windows Service. The printing still got done, i.e. the PDF file was sent to the Zebra ZT220 printer, but it seemed to mess up where on the label to start printing. Now only the upper left corner of our 10x15 cm label has something printed on it.
I'm guessing for some reason the printer settings that are picked up when printing from a GUI aren't honored/used when doing the same from a Windows Service.
Any idea's on how to fix this? Do I have to specify what the bounds / margins of the document are before printing or can I somehow make C# use the printer settings from a Windows Service, same as it does from a GUI?
Note: Up until now I've pretty much only used 'PdfPrintPageScaling.ActualSize' as page scaling.
I hope you can help. Thanks in advance.
Below is the code I'm using at the moment:
- Code: Select all
PdfDocument document = new PdfDocument();
int scalingPerc = 0;
if (scaling == "Aangepaste schaal")
{
if (scalingPercentage != -1)
{
document.PageScaling = PdfPrintPageScaling.CustomSacle;
document.CustomScaling = scalingPerc;
}
else
{
return;
}
}
else if (scaling == "Passend")
{
document.PageScaling = PdfPrintPageScaling.FitSize;
}
else if (scaling == "Ware grootte")
{
document.PageScaling = PdfPrintPageScaling.ActualSize;
}
else if (scaling == "Te grote pagina's verkleinen")
{
document.PageScaling = PdfPrintPageScaling.ShrinkOversized;
}
string extension = Path.GetExtension(filePath);
if (extension.ToLower() == ".pdf")
{
try
{
document.LoadFromFile(filePath);
PrintDocument printDoc = document.PrintDocument;
printDoc.PrinterSettings.PrinterName = printerName;
if (!String.IsNullOrEmpty(printerLadeSourceName)) // printerlade gekozen
{
for (int i = 0; i < printDoc.PrinterSettings.PaperSources.Count; i++)
{
string ladeSourceName = printDoc.PrinterSettings.PaperSources[i].SourceName;
if (printerLadeSourceName.ToUpper() == ladeSourceName.ToUpper()) // lade komt overeen
{
printDoc.DefaultPageSettings.PaperSource = printDoc.PrinterSettings.PaperSources[i];
break;
}
}
}
printDoc.Print();
printDoc.Dispose();
}
catch (Exception ex)
{
// log error
}
}
Kind regards,
Anton