【问题标题】:How to set up and print with custom paper size in C# printdocument?如何在 C# printdocument 中设置和打印自定义纸张大小?
【发布时间】:2020-01-15 14:31:59
【问题描述】:

我正在尝试使用 PrintDocument 并设置要打印的纸张尺寸或条形码热敏打印机。因为我附近没有打印机,所以我使用的是 Win10 中出现的 Microsoft Print To PDF 选项。

在初始化期间,我有这样的代码:

如您所见,我在这里尝试为默认纸张尺寸设置自定义纸张尺寸。但是,我不能指定 Kind 属性,因为它是只读的! RawKind 属性没有帮助。

作为替代,我有这样的活动。它也无济于事。它在预览时正确显示页面布局,但在 PDF 文档中,我观察到默认情况下以 A4 打印的页面。

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;

        }

我知道问题 How to print with custom paper size in winforms ,但我实际上并不理解答案。我应该使用打印机属性操作系统对话框重新配置打印机吗?我宁愿不要求用户以一种或另一种方式修改打印机的设置。另外,我想在打印到 pdf 探索阶段取得适当的结果。

如何在 C# printdocument 中设置和打印自定义纸张尺寸?


编辑:使用行:

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

没有解决问题。

这是一个结果:

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

【问题讨论】:

    标签: c# winforms printdocument


    【解决方案1】:

    您可以尝试初始化 System.Drawing.Printing 下的 PaperSize 类,然后您可以指定自定义尺寸

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

    【讨论】:

    • 它没有多大帮助。 pdf 文档仍然没有正确的页面大小,但预览控件显示正确的大小。我会用截图更新问题,因为我不能在这里添加它。
    • 好的,我也看到你没有指定PrinterName = "Microsoft Print to PDF",你也可以试试
    • 感谢您的洞察力。但我已经在 onClick 按钮处理程序中指定了它: printDoc.PrinterSettings.PrinterName = comboInstalledPrinters.Text; printDoc.Print();
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-29
    • 1970-01-01
    • 2020-02-17
    • 1970-01-01
    • 2012-08-31
    • 2011-01-09
    相关资源
    最近更新 更多