2 Replies Latest reply on Dec 4, 2018 12:41 AM by Anton Engels

    FREE Spire.PDF - Zebra printing PDF out of bounds

    Anton Engels

      Hi,

       

      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:

       

      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
           }
      }
      
        • Re: FREE Spire.PDF - Zebra printing PDF out of bounds
          Robin West

          Hi Anton, When you said that only the upper left corner was printed, does that mean the entire label was scaled to fit in that corner, or that the image was scaled right but just cut off and only showing that corner?

          Are you printing to a driver or do you have a PDF virtual machine running on the printer?  Which driver version?

          Thanks

            • Re: FREE Spire.PDF - Zebra printing PDF out of bounds
              Anton Engels

              Hi Robin,

               

              I meant that the scaling seemed to be correct, but that it only printed a portion of the actual PDF on the label (upper left corner).

               

              Since posting this message I have fixed this issue by setting up the Zebra printer with the correct dimensions of the label it is using on the print server itself. I assumed even though the service I was using didn't honor the correct settings on the device it was running on it would pick up the default settings of the printer used on the print server. This seemed to be correct.

               

              I also built in functionality to specify the page size of the label when running the service, so even if the printer does not have the correct dimensions of the label set up on the print server itself, it will still print the PDF using the page size provided to the service.

               

              I know PDF printing is not what a Zebra is meant for per se, so on top of this I have made it possible to print ZPL files directly to our printer, which, if I am correct, should not give me any issues, right?

               

              But, still, this is my first time printing programmatically, so any further advice would be much appreciated.

               

              Thanks for taking the time to answer my issue.