java 计算打印纸大小,如何使用winforms中的自定义纸张尺寸进行打印

我正在尝试在我的应用程序中打印文档 . 但在不同的打印机上我得到不同的结果 . 这是我的代码:

PaperSize paperSize = new PaperSize("My Envelope", 440, 630);

paperSize.RawKind = (int)PaperKind.Custom;

PrintDocument pd = new PrintDocument();

pd.PrintPage += (sender, args) => Console.Out.WriteLine("Printable Area for printer {0} = {1}", args.PageSettings.PrinterSettings.PrinterName, args.PageSettings.PrintableArea);

pd.DefaultPageSettings.PaperSize = paperSize;

pd.DefaultPageSettings.Landscape = true;

pd.DefaultPageSettings.Margins = new Margins(60, 40, 20, 20);

Console.Out.WriteLine("My paper size: " + pd.DefaultPageSettings.PaperSize);

PrintDialog printDialog = new PrintDialog(); // to choose printer

printDialog.Document = pd;

if (printDialog.ShowDialog(this) == DialogResult.OK)

{

// pd.DefaultPageSettings.PaperSize = paperSize; // uncomment to override size from dialog

Console.Out.WriteLine("Paper size for printer {0} = {1}", printDialog.PrinterSettings.PrinterName, pd.DefaultPageSettings.PaperSize);

_sptTxtControl.Print(pd);

}

对话框显示我有两台打印机 - 三星和惠普 . 这是这两个的控制台输出:

My paper size: [PaperSize My Envelope Kind=Custom Height=630 Width=440]

Paper size for printer HP LaserJet 1022n = [PaperSize A4 Kind=A4 Height=1169 Width=827]

Printable Area for printer HP LaserJet 1022n = {X=21,83333,Y=15,66667,Width=789,3333,Height=1137,333}

My paper size: [PaperSize My Envelope Kind=Custom Height=630 Width=440]

Paper size for printer Samsung SCX-4x28 Series PCL6 = [PaperSize A4 Kind=A4 Height=1169 Width=827]

Printable Area for printer Samsung SCX-4x28 Series PCL6 = {X=17,33333,Y=17,16667,Width=792,3333,Height=1135,167}

您可以看到对话框正在将大小更改为A4 . 因此,如果你在showdialog之后取消注释,我正在执行paperize . 打印时的输出如下所示:

My paper size: [PaperSize My Envelope Kind=Custom Height=630 Width=440]

Paper size for printer HP LaserJet 1022n = [PaperSize My Envelope Kind=Custom Height=630 Width=440]

Printable Area for printer HP LaserJet 1022n = {X=21,83333,Y=15,66667,Width=789,3333,Height=1137,333}

My paper size: [PaperSize My Envelope Kind=Custom Height=630 Width=440]

Paper size for printer Samsung SCX-4x28 Series PCL6 = [PaperSize My Envelope Kind=Custom Height=630 Width=440]

Printable Area for printer Samsung SCX-4x28 Series PCL6 = {X=16,66667,Y=20,Width=400,1667,Height=589,8333}

您可以看到三星打印机具有良好的可打印区域,而惠普则没有 . 无论我在代码中改变什么,HP总是A4大小(设置originatmargins等)

如果我在打印属性中更改了纸张设置(抱歉波兰语对话框):

5d937553-d067-480e-917c-7350652108a5.png

显示对话框后不更改纸张尺寸,然后HP正在打印所有内容 . 输出看起来像这样:

My paper size: [PaperSize My Envelope Kind=Custom Height=630 Width=440]

Paper size for printer HP LaserJet 1022n = [PaperSize My Envelop Format Kind=Custom Height=630 Width=440]

Printable Area for printer HP LaserJet 1022n = {X=18,66667,Y=16,Width=405,3333,Height=597,3333}

但我不想强迫用户为他的打印机保存自定义大小 . 我也试过用Kyocera打印机 - 它可以工作,但对于其他两台惠普打印机却没有 .

最糟糕的是,Word 2010在两台打印机上都打印出相同尺寸的RTF文档,我不能责怪HP驱动程序 .

有任何想法吗?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值