简体   繁体   中英

How to set up and print with custom paper size in C# printdocument?

I am trying to use PrintDocument and set up the paper size to print or barcode thermal printer. Because I don't have the printer nearby I am using Microsoft Print To PDF option which appeared in Win10.

During initialization I have such code: 设置自定义纸张尺寸时出现异常

As you see, here I am trying to set up custom paper size for default paper size. But, I cannot specify Kind property, because it's readonly. RawKind property not helps.

As alternative I have such event. It does not help either. It correctly displays the page layout on preview, but in PDF document I observe pages printed in A4, as by default.

private void PrintDoc_QueryPageSettings(object sender, QueryPageSettingsEventArgs e)
        {

            PageSettings nSettings = new PageSettings();
            int properWidthInHundretsOfInches = (int)(handlingClassRef.newconfig.labelParameters.barcodeLabelWidthMM * (1.0 / 25.4) * 100.0);
            int properHeightInHundretsOfInches = (int)(handlingClassRef.newconfig.labelParameters.barcodeLabelHeightMM * (1.0 / 25.4) * 100.0);
            nSettings.PaperSize = new PaperSize("label", (int)properWidthInHundretsOfInches, (int)properHeightInHundretsOfInches);            
            e.PageSettings = nSettings;

        }

I am aware of question How to print with custom paper size in winforms , but I don't actually understand the answer. Should I reconfigure printer by using printer properties OS dialog? I would like rather not to require user to modify settings of of printer in one way or another. Also, I'd like to achieve appropriate result during printing to pdf exploration phase.

How to set up and print with custom paper size in C# printdocument?


Edit: using the line:

printDoc.DefaultPageSettings.PaperSize = new PaperSize("label", properWidthInHundretsOfInches, properHeightInHundretsOfInches);

did not resolve question.

Here is a result:

预览很好而且很小,但打印的文档很大并且页面大小不合适

preview is nice and small but printed document is large and has not proper page size

您可以尝试在 System.Drawing.Printing 下初始化 PaperSize 类,然后您可以指定自定义大小

printDoc.DefaultPageSettings.PaperSize = new PaperSize("MyPaper", 600, 800);

I found the solution for this!

The Short Answer is:

printDocument1.DefaultPageSettings.PaperSize = new PaperSize("MyPaper", 700, 900);

Why it's printing A4 Paper Size, Not Full Report?

Because The Default Virtual pdf printer in WindowsMicrosoft Print To Pdf uses A4 Paper Size, you can try to change it to A5 from the Control Panel And try to print it again. You will notice that it has included more lines on the pdf output , So don't worry, the code I have mentioned is Correct. but it depends on the printer you use. Because printers Use only Some Formatted Paper Sizes and it will not accept more pages out of the frame.

See This picture for more explanation

..

First, I was furious Because of this problem, I thought that printpreviewDialog1 had another Printable area, and I tried to make it as exact as printdocument1, and then I noticed it's just a viewer. After hours of research and many tries, I noticed that the printer wouldn't accept Any more lines; I was working on a Cashier report. I needed to make a long paper for the thermal printer, but when I was testing on the "print to pdf" printer, it didn't Print all the lines on preview control because it just prints to A4 size, mo more and no less!

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