如何在WinForms的自定义纸张尺寸打印 [英] How to print with custom paper size in winforms

查看:1873
本文介绍了如何在WinForms的自定义纸张尺寸打印的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的应用打印文档。但是,在不同的打印机,我得到不同的结果。这是我的代码:

I'm trying to print a document in my application. But on different printers i get different results. This is my code:

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

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

When dialog shows I have two printers - Samsung and HP. This is the console output for these two:

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。所以,如果你uncommemt线的ShowDialog后我执行纸张大小。输出打印时看起来是这样的:

You can see that the dialog is changing the size to A4. So if you uncommemt line after showdialog I'm enforcing papersize. The output when printing looks like this:

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}

您可以看到,三星打印机具有良好的可打印区域,而惠普也没有。惠普始终A4尺寸无论我将在更改代码(套originatmargins等)

You can see that Samsung printer has good Printable Area while HP has not. HP has always A4 size whatever I will change in code (set originatmargins etc.)

如果我更改打印属性我的纸张设置(波兰对话框不好意思):

If I change my paper settings in print properties (sorry for Polish dialog):

和不显示则HP在打印一切OK对话框后,更改纸张尺寸。输出看起来是这样的:

and not change paper size after showing dialog then HP is printing everything ok. Output looks like this:

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}

但我并不想强迫用户自定义保存尺寸为他的打印机。我也试过这与京瓷打印机 - 它的工作原理,但对其他两台HP打印机它没有

But I don't want to force user to save custom size for his printer. I have tried also this with a Kyocera printer - it works, but for two other HP printers it doesn't.

而最糟糕的是,Word 2010的打印ok了这种规模上的两台打印机相同的RTF文档,共同我cant't怪HP驱动程序。

And the worst part is that Word 2010 prints ok the same RTF document with this size on both printers, co I cant't blame the HP driver.

任何想法?

推荐答案

PrintDialog类关闭后,不要只设置

After the PrintDialog closes, don't just set

pd.DefaultPageSettings.PaperSize = paperSize;



也尝试设置

Try also setting

pd.PrinterSettings.DefaultPageSettings.PaperSize = paperSize;



我认为这会照顾它。

I think that will take care of it.

这篇关于如何在WinForms的自定义纸张尺寸打印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆