Ivan P.
Ivan P.

Reputation: 946

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: exception during setting custom paper size

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

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

Upvotes: 4

Views: 20647

Answers (2)

Yousef10
Yousef10

Reputation: 31

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 Windows Microsoft 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!

Upvotes: 3

Prany
Prany

Reputation: 2143

You can try by initialising PaperSize class under System.Drawing.Printing and then you can specify the custom size

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

Upvotes: 3

Related Questions