1个回答
展开全部
因为水晶报表的PaperSize是enum类型,但有时客户用的纸张不在其定义范围内,虽然在设计报表时可以选择目的打印机和纸型,但如果重新配置过该打印机则报表默认纸型不再有效,报表将按A4进行预览且会失真。
打印的基本代码:
CrystalReport1 report = new CrystalReport1(); //Report为你自己的报表名
PageMargins margins;
margins = Report.PrintOptions.PageMargins;
margins.bottomMargin = 250;
margins.leftMargin = 350;
margins.rightMargin = 350;
margins.topMargin = 350;
// Apply the page margins.
Report.PrintOptions.ApplyPageMargins(margins);
// Select the printer.
string printerName = "\\\\局域网机器名\\打印机名(例如HP 2100)"; //本地打印机直接指定名称
Report.PrintOptions.PrinterName = printerName; //指定打印机名称
Report.PrintOptions.PaperSize = PaperSize.PaperA4; //指定纸张尺寸
report.PrintToPrinter(1, true, 1, 4);
本文由xwdd129编写,转载请注明出处,谢谢!
下面简单就打印中的参数进行说明:
PrintOptions类,提供用于设置报表打印选项的属性和方法。
PrintOptions成员:
PageContentHeight---Int32,获取页面内容的高度
PageContentWidth---Int32,获取页面内容的宽度
PageMargins---获取报表的边距
PageOrientation---获取或设置打印机纸张方向
Pagesize---获取或设置当前打印机纸张的大小
PrinterName---字符串,获取或设置报表所使用的打印机名称
ReportDocument.PrintToPrinter方法
public virtual void PrintToPrinter( int nCopies, bool collated, int startPageN, int endPageN )
nCopies 指明要打印的分数
collated 指明是否逐份打印
startPageN 指明要打印的第一页
endPageN 指明要打印的最后一页
发表于 @ 2006年06月01日 16:00:00|评论(3)|编辑
新一篇: 正在换工作…… | 旧一篇: 关于推模式水晶报表数据源的设置问题评论
#DaChu 发表于2007-09-24 12:04:54 IP: 121.28.161.*
margins = Report.PrintOptions.PageMargins
其中Report需要添加哪个引用?
我的是11.5版本,使用CRAXDRT.Report
系统告之:不包含PrintOptions定义
谢谢
we_are_friend2008@163.com#pshy 发表于2007-12-18 09:48:17 IP: 59.36.90.*
Papersize里面就只有那些纸张格式,请问还能增加自定义的纸张???#pshy 发表于2007-12-18 09:49:46 IP: 59.36.90.*
或者能自定义一个类似Papersize的类吗?
因为水晶报表的PaperSize是enum类型,但有时客户用的纸张不在其定义范围内,虽然在设计报表时可以选择目的打印机和纸型,但如果重新配置过该打印机则报表默认纸型不再有效,报表将按A4进行预览且会失真。
//获取本机所有打印机将其名称填充到comboBoxPrinters中:包含本地和网络打印机
foreach(string printer in System.Drawing.Printing.PrinterSettings.InstalledPrinters)
{
this.comboBoxPrinters.Items.Add(printer);
}
//当从comboBoxPrinters中选择打印机时获取其支持的纸张大小将其填充到listBoxPapers
this.listBoxPapers.Items.Clear();
string printer = this.comboBoxPrinters.Text;
this.m_printer = printer;
Microsoft.Win32.RegistryKey rk;
if(!this.comboBoxPrinters.Text.StartsWith(@"\\")) //本地打印机
rk = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Print\\Printers\\" + this.comboBoxPrinters.Text + "\\DsDriver");
else //网络打印机
{
string[] p = printer.Remove(0,2).Split(new char[] { '\\' });
string path = "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Print\\Providers\\LanMan Print Services\\Servers\\" + p[0] + "\\Printers\\" + p[1] + "\\DsDriver";
rk = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(path);
}
string[] papers = (string[])(rk.GetValue("printMediaSupported"));
for(int i=0;i<papers.Length;i++)
{
this.listBoxPapers.Items.Add(papers[i]);
}
//根据纸张名称获取其所在本地机上的PaperSize:调用的是PaperSizeGetter.Get_PaperSizes静态方法(是从水晶报表中reflect精简出来的,版权归原作者所有)
public class PaperSizeGetter
{
public static string OutputPort = String.Empty;
[DllImport("winspool.drv", CharSet=CharSet.Auto, SetLastError=true)]
public static extern int DeviceCapabilities(string pDevice, string pPort, short fwCapabilities, IntPtr pOutput, IntPtr pDevMode);
public static int[] Get_PaperSizes(string printer)
{
string text1 = printer;
int num1 = FastDeviceCapabilities(0x10, IntPtr.Zero, -1, text1);
if (num1 == -1)
{
return new int[0];
}
int num2 = Marshal.SystemDefaultCharSize * 0x40;
IntPtr ptr1 = Marshal.AllocCoTaskMem(num2 * num1);
FastDeviceCapabilities(0x10, ptr1, -1, text1);
IntPtr ptr2 = Marshal.AllocCoTaskMem(2 * num1);
FastDeviceCapabilities(2, ptr2, -1, text1);
IntPtr ptr3 = Marshal.AllocCoTaskMem(8 * num1);
FastDeviceCapabilities(3, ptr3, -1, text1);
int[] sizeArray1 = new int[num1];
for (int num3 = 0; num3 < num1; num3++)
{
string text2 = Marshal.PtrToStringAuto((IntPtr) (((long) ptr1) + (num2 * num3)), 0x40);
int num4 = text2.IndexOf('\0');
if (num4 > -1)
{
text2 = text2.Substring(0, num4);
}
short num5 = Marshal.ReadInt16((IntPtr) (((long) ptr2) + (num3 * 2)));
int num6 = Marshal.ReadInt32((IntPtr) (((long) ptr3) + (num3 * 8)));
int num7 = Marshal.ReadInt32((IntPtr) ((((long) ptr3) + (num3 * 8)) + 4));
sizeArray1[num3] = System.Convert.ToInt32(num5);
}
Marshal.FreeCoTaskMem(ptr1);
Marshal.FreeCoTaskMem(ptr2);
Marshal.FreeCoTaskMem(ptr3);
return sizeArray1;
}
private static int FastDeviceCapabilities(short capability, IntPtr pointerToBuffer, int defaultValue, string printerName)
{
int num1 = DeviceCapabilities(printerName, OutputPort, capability, pointerToBuffer, IntPtr.Zero);
if (num1 == -1)
{
return defaultValue;
}
return num1;
}
}
//根据纸型名称在其名称列表中的索引,获取该索引在调用Get_PaperSizes后的size数组中的对应值
int[] sizes = PaperSizeGetter.Get_PaperSizes(printerName);
int paperSizeid = sizes[this.listBoxPapers.SelectedIndex];
//将该size赋值给报表对象
report.PrintOptions.PaperSize = (CrystalDecisions.Shared.PaperSize)(paperSizeid);
打印的基本代码:
CrystalReport1 report = new CrystalReport1(); //Report为你自己的报表名
PageMargins margins;
margins = Report.PrintOptions.PageMargins;
margins.bottomMargin = 250;
margins.leftMargin = 350;
margins.rightMargin = 350;
margins.topMargin = 350;
// Apply the page margins.
Report.PrintOptions.ApplyPageMargins(margins);
// Select the printer.
string printerName = "\\\\局域网机器名\\打印机名(例如HP 2100)"; //本地打印机直接指定名称
Report.PrintOptions.PrinterName = printerName; //指定打印机名称
Report.PrintOptions.PaperSize = PaperSize.PaperA4; //指定纸张尺寸
report.PrintToPrinter(1, true, 1, 4);
本文由xwdd129编写,转载请注明出处,谢谢!
下面简单就打印中的参数进行说明:
PrintOptions类,提供用于设置报表打印选项的属性和方法。
PrintOptions成员:
PageContentHeight---Int32,获取页面内容的高度
PageContentWidth---Int32,获取页面内容的宽度
PageMargins---获取报表的边距
PageOrientation---获取或设置打印机纸张方向
Pagesize---获取或设置当前打印机纸张的大小
PrinterName---字符串,获取或设置报表所使用的打印机名称
ReportDocument.PrintToPrinter方法
public virtual void PrintToPrinter( int nCopies, bool collated, int startPageN, int endPageN )
nCopies 指明要打印的分数
collated 指明是否逐份打印
startPageN 指明要打印的第一页
endPageN 指明要打印的最后一页
发表于 @ 2006年06月01日 16:00:00|评论(3)|编辑
新一篇: 正在换工作…… | 旧一篇: 关于推模式水晶报表数据源的设置问题评论
#DaChu 发表于2007-09-24 12:04:54 IP: 121.28.161.*
margins = Report.PrintOptions.PageMargins
其中Report需要添加哪个引用?
我的是11.5版本,使用CRAXDRT.Report
系统告之:不包含PrintOptions定义
谢谢
we_are_friend2008@163.com#pshy 发表于2007-12-18 09:48:17 IP: 59.36.90.*
Papersize里面就只有那些纸张格式,请问还能增加自定义的纸张???#pshy 发表于2007-12-18 09:49:46 IP: 59.36.90.*
或者能自定义一个类似Papersize的类吗?
因为水晶报表的PaperSize是enum类型,但有时客户用的纸张不在其定义范围内,虽然在设计报表时可以选择目的打印机和纸型,但如果重新配置过该打印机则报表默认纸型不再有效,报表将按A4进行预览且会失真。
//获取本机所有打印机将其名称填充到comboBoxPrinters中:包含本地和网络打印机
foreach(string printer in System.Drawing.Printing.PrinterSettings.InstalledPrinters)
{
this.comboBoxPrinters.Items.Add(printer);
}
//当从comboBoxPrinters中选择打印机时获取其支持的纸张大小将其填充到listBoxPapers
this.listBoxPapers.Items.Clear();
string printer = this.comboBoxPrinters.Text;
this.m_printer = printer;
Microsoft.Win32.RegistryKey rk;
if(!this.comboBoxPrinters.Text.StartsWith(@"\\")) //本地打印机
rk = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Print\\Printers\\" + this.comboBoxPrinters.Text + "\\DsDriver");
else //网络打印机
{
string[] p = printer.Remove(0,2).Split(new char[] { '\\' });
string path = "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Print\\Providers\\LanMan Print Services\\Servers\\" + p[0] + "\\Printers\\" + p[1] + "\\DsDriver";
rk = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(path);
}
string[] papers = (string[])(rk.GetValue("printMediaSupported"));
for(int i=0;i<papers.Length;i++)
{
this.listBoxPapers.Items.Add(papers[i]);
}
//根据纸张名称获取其所在本地机上的PaperSize:调用的是PaperSizeGetter.Get_PaperSizes静态方法(是从水晶报表中reflect精简出来的,版权归原作者所有)
public class PaperSizeGetter
{
public static string OutputPort = String.Empty;
[DllImport("winspool.drv", CharSet=CharSet.Auto, SetLastError=true)]
public static extern int DeviceCapabilities(string pDevice, string pPort, short fwCapabilities, IntPtr pOutput, IntPtr pDevMode);
public static int[] Get_PaperSizes(string printer)
{
string text1 = printer;
int num1 = FastDeviceCapabilities(0x10, IntPtr.Zero, -1, text1);
if (num1 == -1)
{
return new int[0];
}
int num2 = Marshal.SystemDefaultCharSize * 0x40;
IntPtr ptr1 = Marshal.AllocCoTaskMem(num2 * num1);
FastDeviceCapabilities(0x10, ptr1, -1, text1);
IntPtr ptr2 = Marshal.AllocCoTaskMem(2 * num1);
FastDeviceCapabilities(2, ptr2, -1, text1);
IntPtr ptr3 = Marshal.AllocCoTaskMem(8 * num1);
FastDeviceCapabilities(3, ptr3, -1, text1);
int[] sizeArray1 = new int[num1];
for (int num3 = 0; num3 < num1; num3++)
{
string text2 = Marshal.PtrToStringAuto((IntPtr) (((long) ptr1) + (num2 * num3)), 0x40);
int num4 = text2.IndexOf('\0');
if (num4 > -1)
{
text2 = text2.Substring(0, num4);
}
short num5 = Marshal.ReadInt16((IntPtr) (((long) ptr2) + (num3 * 2)));
int num6 = Marshal.ReadInt32((IntPtr) (((long) ptr3) + (num3 * 8)));
int num7 = Marshal.ReadInt32((IntPtr) ((((long) ptr3) + (num3 * 8)) + 4));
sizeArray1[num3] = System.Convert.ToInt32(num5);
}
Marshal.FreeCoTaskMem(ptr1);
Marshal.FreeCoTaskMem(ptr2);
Marshal.FreeCoTaskMem(ptr3);
return sizeArray1;
}
private static int FastDeviceCapabilities(short capability, IntPtr pointerToBuffer, int defaultValue, string printerName)
{
int num1 = DeviceCapabilities(printerName, OutputPort, capability, pointerToBuffer, IntPtr.Zero);
if (num1 == -1)
{
return defaultValue;
}
return num1;
}
}
//根据纸型名称在其名称列表中的索引,获取该索引在调用Get_PaperSizes后的size数组中的对应值
int[] sizes = PaperSizeGetter.Get_PaperSizes(printerName);
int paperSizeid = sizes[this.listBoxPapers.SelectedIndex];
//将该size赋值给报表对象
report.PrintOptions.PaperSize = (CrystalDecisions.Shared.PaperSize)(paperSizeid);
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询