Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing custom paper size to an impact printer in WPF

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.

like image 364
epalm Avatar asked Mar 15 '13 15:03

epalm


People also ask

How do I set custom paper size when printing?

To resolve this issue, change the default paper size of your printer: Click Start, point to Settings, and the click Printers. Right-click the appropriate printer, and then click Properties. Click the Paper tab, and then click the paper size you want to use in the Paper Size box.


1 Answers

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;
        }
like image 90
Palak.Maheria Avatar answered Nov 03 '22 14:11

Palak.Maheria