C# winfom 水晶报表(For VS.NET2010)设置纵向打印,打印出的却是横向的,编程实现,勿手动设置

hiwcn 2012-08-08 10:06:32
C# winfom 水晶报表(For VS.NET2010)设置纵向打印,打印出的却是横向的,请朋友们指点下.找出问题原因.

以编程方式实现,勿提供手动设置的方案.

代码如下:
private void 打印DToolStripMenuItem_Click(object sender, EventArgs e)
{
ReportDocument crReportDocument = new ReportDocument();
//Create an instance of a report
crReportDocument = new JcApp.Report.RptStock();// RptStock.rpt为水晶报表文件
//Use error handling in case an error occurs
try
{
//Set the printer name to print the report to. By default the sample
//report does not have a defult printer specified. This will tell the
//engine to use the specified printer to print the report. Print out
//a test page (from Printer properties) to get the correct value.
System.Drawing.Printing.PrintDocument printDocument = new System.Drawing.Printing.PrintDocument();
string strPrintName = printDocument.PrinterSettings.PrinterName;
crReportDocument.PrintOptions.PrinterName = strPrintName;

//根据指定纸张格式名称设置纸张格式
Microsoft.Win32.RegistryKey rk;
if (!strPrintName.StartsWith(@"\\")) //本地打印机
rk = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Print\\Printers\\" + strPrintName + "\\DsDriver");
else //网络打印机
{

string[] p = strPrintName.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"));
//纸张格式名称与纸张格式id的键值对
System.Collections.Hashtable ht = new System.Collections.Hashtable();
for (int i = 0; i < papers.Length; i++)
{
ht.Add(papers[i], i);
}
//根据纸型名称在其名称列表中的索引,获取该索引在调用Get_PaperSizes后的size数组中的对应值
string strPageSizeName = "241二等分";//自定义纸张 宽度:2100mm 高度:1390mm
int[] sizes = PaperSizeGetter.Get_PaperSizes(strPrintName);
int paperSizeid = sizes[Convert.ToInt32(ht[strPageSizeName])];//sizes[this.listBoxPapers.SelectedIndex];

//将该size赋值给报表对象
crReportDocument.PrintOptions.PaperSize = (CrystalDecisions.Shared.PaperSize)(paperSizeid);

crReportDocument.PrintToPrinter(1, true, 1, 1);

MessageBox.Show("打印完成!");
}
catch (Exception err)
{
MessageBox.Show(err.ToString());
}
}



//根据纸张名称获取其所在本地机上的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;
}
}

...全文
80 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧