如何在WinForms应用中为爱普生打印机设置介质名称为Custom?
WinForms中设置爱普生打印机介质名称为“Custom”的解决方法
直接创建PaperSize对象指定名称为"Custom"不生效,核心原因是爱普生打印机的自定义介质需要关联到目标打印机的驱动设置,且正确绑定到PrintDocument的配置中,具体解决步骤如下:
第一步:定位目标打印机
先获取你要操作的爱普生打印机的PrinterSettings实例,确保指定正确的打印机名称:var printerSettings = new PrinterSettings(); printerSettings.PrinterName = "你的爱普生打印机全名"; // 替换为实际打印机名称第二步:创建/获取自定义纸张
计算好自定义尺寸(单位为百分之一英寸)后,先检查打印机是否已存在名为"Custom"的自定义纸张,不存在则创建并添加到打印机的纸张集合中,必须将PaperKind设为Custom:// 转换尺寸为百分之一英寸(WinForms打印系统的标准单位) int widthInHundredths = Convert.ToInt32(width * 0.3937 * 100); int heightInHundredths = Convert.ToInt32(height * 0.3937 * 100); PaperSize customPaper = null; // 遍历打印机支持的纸张,查找已存在的Custom介质 foreach (PaperSize paper in printerSettings.PaperSizes) { if (paper.PaperName.Equals("Custom", StringComparison.OrdinalIgnoreCase)) { customPaper = paper; break; } } if (customPaper == null) { // 创建新的自定义纸张 customPaper = new PaperSize("Custom", widthInHundredths, heightInHundredths); customPaper.PaperKind = PaperKind.Custom; // 添加到打印机的纸张列表中 printerSettings.PaperSizes.Add(customPaper); }第三步:绑定到PrintDocument
将配置好的自定义纸张设置为PrintDocument的默认页面纸张:var printDocument = new PrintDocument(); printDocument.PrinterSettings = printerSettings; printDocument.DefaultPageSettings.PaperSize = customPaper;关键注意事项
部分爱普生打印机要求先在系统层面的打印机属性中添加自定义纸张尺寸,驱动才能识别代码中的配置。如果代码仍不生效,可打开控制面板→设备和打印机→右键目标爱普生打印机→打印机属性→设备设置,添加对应尺寸的自定义介质并命名为"Custom",再重新运行代码。
内容的提问来源于stack exchange,提问作者Sam
相关产品推荐
相关产品推荐

