简体   繁体   中英

different print results from exactly the same code

I have two programs, one is a windows service and the other a windows forms application. They have exactly the same code for printing one A4 page. They both print to the same network printer and they start drawing at position 0,0.

private void pd_PrintCustomsDocument(object sender, PrintPageEventArgs ev)
{
  Graphics g = ev.Graphics;

  g.PageUnit = GraphicsUnit.Millimeter;
  using (Font courierBig = new Font("Courier", 15))
  {
    g.DrawString("Shipping Invoice", courierBig, Brushes.Black, new Point(0, 0));
    // etc
  }
}

The windows forms app prints the document correctly, a page margin is used. But the service starts printing exactly at the edge of the paper. Is there a difference between printing with gdi+ from a service and a windows forms application?

The code for the actual printing is divided into a base and subclass for overriding default printer settings like selecting page from a different tray:

public class PrintBehaviour : IDisposable
{
    private string mPrinterName;
    private PrintPageEventHandler mHandler;
    private PrintDocument mDocument = new PrintDocument();

    public PrintBehaviour(string name, PrintPageEventHandler handler)
    {
        mPrinterName = name;
        mHandler = handler;
        mDocument.PrintController = new StandardPrintController();
    }

    public virtual void SettingsOverride(PrintDocument doc) {}

    public void Print()
    {
        SettingsOverride(mDocument);
        mDocument.PrinterSettings.PrinterName = mPrinterName;
        mDocument.PrintPage += new PrintPageEventHandler(mHandler);
        mDocument.Print();
    }

    public void Dispose()
    {
        mDocument.Dispose();
    }
}

public sealed class CustomsPrintBehaviour : PrintBehaviour
{
    private string mPaperTray;

    public CustomsPrintBehaviour(string name, PrintPageEventHandler handler, string paperTray)
        : base(name, handler)
    {
        mPaperTray = paperTray;
    }

    public override void SettingsOverride(PrintDocument doc)
    {
        base.SettingsOverride(doc);
        doc.DefaultPageSettings.Landscape = true;

        foreach (PaperSource source in doc.PrinterSettings.PaperSources)
        {
            if (source.SourceName.Trim().ToUpper() == mPaperTray)
            {
                doc.DefaultPageSettings.PaperSource = source;

                PaperSize size = new PaperSize { RawKind = (int)PaperKind.A4 };

                doc.DefaultPageSettings.PaperSize = size;
                break;
            }
        }
    }
}

and called like this:

using (var pb = new CustomsPrintBehaviour(_customsPrinter, pd_PrintCustomsDocument, kv["PaperTray"].ToUpper()))
{
  pb.Print();
}

From MSDN :

GDI+ functions and classes are not supported for use within a Windows service. Attempting to use these functions and classes from a Windows service may produce unexpected problems, such as diminished service performance and run-time exceptions or errors.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM