Search code examples
c#.netprintingzebra-printersprintdocument

PrintDocument adds an extra blank page upon a print job


When ever I print a image label to a zebra printer, the label prints normally and then an extra blank label is fed and this subsequently misaligns the top margin of each printed label afterwards.

I have looked online and at a lot of questions that have been already posted (i.e. printdocument adds blank page), with no solution.

I have tested printing the image file directly from Windows Photo Viewer as well as from the label program itself with success. The issue only occurs when running the program using PrintDocument. The following is my code

    var printDoc = new PrintDocument {PrinterSettings = {PrinterName = printerName}};
    printDoc.PrintPage += (sender, args) =>
    {
        using(Image img = Image.FromFile(filePath))
        {   //file is 900x300, DPI 300, and print page is 3x1 inches
            args.Graphics.PageUnit = GraphicsUnit.Document;
            args.Graphics.DrawImage(img, 0, 0, img.Width, img.Height);
            args.HasMorePages = false;
        }
    };

    printDoc.Print();

I have tried to set the width and height to a much smaller value when I draw the image but it will still print a blank label! Any help would be greatly appreciated.


Solution

  • I realized the issue. This only occurs running the program as a windows service. It will override any settings you have to the locally mapped printer and use the default network printer settings. Running the program as a console app worked as expected. The solution was to configure the default printer from its network location!

    Many thanks to everyone who left comments and helped :)