Printing custom paper size to an impact printer in WPF

点点圈 提交于 2019-12-12 11:07:18

问题


I'm printing to an impact printer, loaded with 8.5 x 8.5 inch paper. When I print, the printer ejects 11 inches instead of 8.5.

PageMediaSize pageSize = new PageMediaSize(PageMediaSizeName.Unknown, element.Width, element.Height);

PrintDialog dialog = new PrintDialog();
dialog.PrintTicket.PageMediaSize = pageSize;
Console.WriteLine(dialog.PrintableAreaHeight); // 816, good!
dialog.PrintQueue = myQueue;                   // selected from a combobox
Console.WriteLine(dialog.PrintableAreaHeight); // 1056 :(

dialog.PrintVisual(element, description);

Using "How do I convert Twips to Pixels in .NET?" I've determined that 8.5 inches is 816 pixels, which is the size of my element.Width and element.Height. I'm setting a new PageMediaSize, but this seems to have no effect, dialog.PrintableAreaHeight is still ends up at 1056 when I set the queue on the dialog.

If I do dialog.ShowDialog(), manually pick my printer, and manually find and change Paper Size in my printer's advanced settings, then dialog.PrintableAreaHeight properly reflects the change.

This page http://go4answers.webhost4life.com/Example/set-printdialogs-default-page-size-168976.aspx suggests that I can only set a PageMediaSize supported by my printer. Using the GetPrintCapabilities function on my PrintQueue, I see a list of 10 or so page sizes, none of which are 8.5 x 8.5. This is the same list I see when I go to my printer's advanced settings in windows.


回答1:


Please find the code below, it sets paper size as required

        var printerSettings = new PrinterSettings();
        var labelPaperSize = new PaperSize { RawKind = (int)PaperKind.A6, Height = 148, Width = 105 };
        printerSettings.DefaultPageSettings.PaperSize = labelPaperSize;
        var labelPaperSource = new PaperSource { RawKind = (int)PaperSourceKind.Manual };
        printerSettings.DefaultPageSettings.PaperSource = labelPaperSource;
        if (printerSettings.CanDuplex)
        {
            printerSettings.Duplex = Duplex.Default;
        }


来源:https://stackoverflow.com/questions/15436334/printing-custom-paper-size-to-an-impact-printer-in-wpf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!