2
votes

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

1

1 Answers

2
votes

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);