http://expert.csdn.net/Expert/topic/1276/1276427.xml?temp=.8006403
解决方案 »
- datagridview1显示SQL查询出来的100行2列数据,一列保存为一个数组。数组经过简单计算重新保存到SQL,,怎么处理?
- 问个关于socket的问题
- 请各位高手帮帮忙!!!winform下用treeview绑定xml动态生成特定的树
- 动态创建的控件怎么销毁
- 请教VS2003操作水晶报表问题
- 用vs.net 2005写的C#连接oracle的程序,怎么才能运行在vs.net 2003上
- 一个简单的xml问题,在线等待,马上结贴!
- foreach问题,简单,但是我不会,呵呵
- c#问题请教
- 哪里有介绍C#的内置函数?比如时间格式化函数之类的.
- 关于Web页面上的DataGrid问题
- 请教StreamReader的超时问题
具体没有看的太明白
能否把你的代码压缩一下发给我,我调试看看 [email protected]
另外,楼上 hongshun(好好) 的连接也不错
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Drawing.Printing;
using System.IO;namespace Test
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.TextBox textBox3;
private System.Windows.Forms.TextBox textBox4;
private System.Windows.Forms.TextBox textBox5;
private PrintDocument pd = new PrintDocument();
private PageSettings storePageSetting = new PageSettings();
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Button button4;
private System.Windows.Forms.TextBox textBox6;
private System.Windows.Forms.Button button5;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
this.pd.DocumentName = "HAHAHA";
this.pd.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.pd_PrintPage);
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.textBox2 = new System.Windows.Forms.TextBox();
this.textBox3 = new System.Windows.Forms.TextBox();
this.textBox4 = new System.Windows.Forms.TextBox();
this.textBox5 = new System.Windows.Forms.TextBox();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.button4 = new System.Windows.Forms.Button();
this.textBox6 = new System.Windows.Forms.TextBox();
this.button5 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(40, 16);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(224, 21);
this.textBox1.TabIndex = 0;
this.textBox1.Text = "textBox1";
//
// button1
//
this.button1.Location = new System.Drawing.Point(8, 208);
this.button1.Name = "button1";
this.button1.TabIndex = 1;
this.button1.Text = "打印预览";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(40, 48);
this.textBox2.Name = "textBox2";
this.textBox2.TabIndex = 0;
this.textBox2.Text = "textBox1";
//
// textBox3
//
this.textBox3.Location = new System.Drawing.Point(40, 88);
this.textBox3.Name = "textBox3";
this.textBox3.TabIndex = 0;
this.textBox3.Text = "textBox1";
//
// textBox4
//
this.textBox4.Location = new System.Drawing.Point(160, 48);
this.textBox4.Name = "textBox4";
this.textBox4.TabIndex = 0;
this.textBox4.Text = "textBox1";
//
// textBox5
//
this.textBox5.Location = new System.Drawing.Point(16, 128);
this.textBox5.Name = "textBox5";
this.textBox5.Size = new System.Drawing.Size(264, 21);
this.textBox5.TabIndex = 0;
this.textBox5.Text = "textBox1";
//
// button2
//
this.button2.Location = new System.Drawing.Point(96, 208);
this.button2.Name = "button2";
this.button2.TabIndex = 1;
this.button2.Text = "页面设置";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(192, 208);
this.button3.Name = "button3";
this.button3.TabIndex = 1;
this.button3.Text = "打印";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// button4
//
this.button4.Location = new System.Drawing.Point(192, 208);
this.button4.Name = "button4";
this.button4.TabIndex = 1;
this.button4.Text = "打印";
//
// textBox6
//
this.textBox6.Location = new System.Drawing.Point(40, 88);
this.textBox6.Name = "textBox6";
this.textBox6.TabIndex = 0;
this.textBox6.Text = "textBox1";
//
// button5
//
this.button5.Location = new System.Drawing.Point(192, 88);
this.button5.Name = "button5";
this.button5.TabIndex = 1;
this.button5.Text = "重设";
this.button5.Click += new System.EventHandler(this.button5_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.button1,
this.textBox1,
this.textBox2,
this.textBox3,
this.textBox4,
this.textBox5,
this.button2,
this.button3,
this.button4,
this.textBox6,
this.button5});
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
} private void Form1_Load(object sender, System.EventArgs e)
{
string paperName = "B5";
foreach(PaperSize ps in this.pd.PrinterSettings.PaperSizes)
if(paperName == ps.PaperName)
{
//ps.Width = 800;
//ps.Height = 800;
//this.pd.DefaultPageSettings.PaperSize = ps;
this.storePageSetting.PaperSize = ps;
}
// this.storePageSetting = this.pd.DefaultPageSettings;
this.storePageSetting.PaperSize = new PaperSize("Custom",800,400);
this.storePageSetting.Margins = new Margins(40,40,40,40);
this.WriteTextBox(); } private void button1_Click(object sender, System.EventArgs e)
{
pd.DefaultPageSettings = this.storePageSetting;
PrintPreviewDialog ppDlg = new PrintPreviewDialog();
ppDlg.Document = pd;
ppDlg.ShowDialog();
}
{
this.textBox1.Text = "纸张种类:"+this.storePageSetting.PaperSize.Kind.ToString();
this.textBox2.Text = "纸张名称:"+this.storePageSetting.PaperSize.PaperName.ToString();
this.textBox3.Text = "纸张宽度:"+this.storePageSetting.PaperSize.Width.ToString();
this.textBox4.Text = "纸张高度:"+this.storePageSetting.PaperSize.Height.ToString();
this.textBox5.Text = "左:"+this.storePageSetting.Margins.Left.ToString()
+"右"+this.storePageSetting.Margins.Right.ToString()
+"上"+this.storePageSetting.Margins.Top.ToString()
+"下"+this.storePageSetting.Margins.Bottom.ToString();
}
private void pd_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
e.Graphics.FillRectangle(Brushes.Blue,new Rectangle(e.PageSettings.Margins.Left+(e.MarginBounds.Width-200)/2,
e.PageSettings.Margins.Top+(e.MarginBounds.Height-200)/2,200,200));
} private void button2_Click(object sender, System.EventArgs e)
{
PageSetupDialog psDlg = new PageSetupDialog();
psDlg.PageSettings = this.storePageSetting;
if(psDlg.ShowDialog()==DialogResult.OK)
this.WriteTextBox();
} private void button3_Click(object sender, System.EventArgs e)
{
this.pd.DefaultPageSettings = this.storePageSetting;
PrintDialog pDlg = new PrintDialog();
pDlg.Document = pd;
if(pDlg.ShowDialog()==DialogResult.OK)
pd.Print();
} private void button5_Click(object sender, System.EventArgs e)
{
if(this.storePageSetting.PaperSize.Kind == PaperKind.Custom)
{
try
{
this.storePageSetting.PaperSize.PaperName = this.textBox2.Text.Trim();
this.storePageSetting.PaperSize.Width = int.Parse(this.textBox3.Text.Trim());
this.storePageSetting.PaperSize.Height = int.Parse(this.textBox4.Text.Trim());
this.WriteTextBox();
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
else
MessageBox.Show("不能改变纸张");
}
}
}
我试了你的代码,没有问题呀
你的textBox3 and textBox4应该输入数字而不是
“纸张宽度:800”具体去除WriteTextBox()就可以了我页面预览根据输入宽度、高度调节都没问题
但是现在小弟还有一个问题,如果我在Form_laod事件中加入:
this.pd.DefaultPageSettings = this.storePageSetting;
打印事件和预览事件中都不加这句,则重设纸张大小后预览正常,但是打印到PDF格式文档时纸张就不正确。而通过在页面设置对话框中选择的纸张打印出来则是正确的。
另外,我把这句加到打印事件中时,如果改变自定义纸张的大小,则在预览时会出现未处理的异常情况。我把我最新的代码发给你们,请hongshun(好好) 把你的邮箱告诉我,或者上MSN也可以的。
我的机器上装了一个“FinePrint pdfFactory Pro”打印机(把打印结果显示为PDF文档)和Adobe Acrobat的Acrobat Distiller打印机,如果我把这个设置为默认打印机,则在预览时会出错,如果把其它打印机设为默认值,则不会出此错误。但是现在还是有一个问题,就是我通过页面设置选取的纸张(包括在打印机选项中设置的自定义纸张),都能够在打印机中正确的打印出来。而我在程序中自定义的纸张则打印到PDF文档时显示的纸张大小则不正确,始终显示打印机选项中的默认值。不过现在问题基本已经解决了,衷心地感谢你们两位了。希望能够多向你们学习,有一天也挣上5颗星。我的QQ:2636829:)