我正在尝试在我的应用程序中打印文档。但在不同的打印机上我得到不同的结果。这是我的代码:
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}
您可以看到三星打印机具有良好的可打印区域,而惠普则没有。无论我将改变代码(设置originatmargins等),HP总是A4大小。
如果我在打印属性中更改了纸张设置(抱歉波兰语对话框):
并且在显示对话框后不更改纸张尺寸,然后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 :(得分:15)
PrintDialog关闭后,不要只设置
pd.DefaultPageSettings.PaperSize = paperSize;
尝试设置
pd.PrinterSettings.DefaultPageSettings.PaperSize = paperSize;
我认为会照顾它。
答案 1 :(得分:4)
设置
pd.DefaultPageSettings.PaperSize = paperSize;
和
pd.PrinterSettings.DefaultPageSettings.PaperSize = paperSize;
有时可能无效。
最合适的是选择自定义纸张大小 安装在打印机驱动程序或计算机,然后设置 以下属性
pd.DefaultPageSettings.PaperSize = ExistingPaperSize;
pd.PrinterSettings.PaperSize = ExistingPaperSize;
喜欢这段代码
PrintDocument pd = new PrintDocument();
pd.PrinterSettings = printdg.PrinterSettings;
PaperSize RequiredPaperSize = CalculatePaperSize(WIDTH,HEIGHT);
bool FoundMatchingPaperSize = false;
for (int index = 0; index < pd.PrinterSettings.PaperSizes.Count; index++)
{
if (pd.PrinterSettings.PaperSizes[index].Height == RequiredPaperSize.Height && pd.PrinterSettings.PaperSizes[index].Width == RequiredPaperSize.Width)
{
pd.PrinterSettings.DefaultPageSettings.PaperSize = pd.PrinterSettings.PaperSizes[index];
pd.DefaultPageSettings.PaperSize = pd.PrinterSettings.PaperSizes[index];
FoundMatchingPaperSize = true;
break;
}
}
//Method to calculate PaperSize from Centimeter to 1/100 of an inch
/// Caclulates the paper size
/// </summary>
/// <param name="WidthInCentimeters"></param>
/// <param name="HeightInCentimetres"></param>
/// <returns></returns>
public static System.Drawing.Printing.PaperSize CalculatePaperSize(double WidthInCentimeters,
double HeightInCentimetres)
{
int Width = int.Parse( ( Math.Round ((WidthInCentimeters*0.393701) * 100, 0, MidpointRounding.AwayFromZero) ).ToString() );
int Height = int.Parse( ( Math.Round ((HeightInCentimetres*0.393701) * 100, 0, MidpointRounding.AwayFromZero) ).ToString() );
PaperSize NewSize = new PaperSize();
NewSize.RawKind = (int)PaperKind.Custom;
NewSize.Width = Width;
NewSize.Height = Height;
NewSize.PaperName = "Letter";
return NewSize;
}