在c# Windows窗体项目中,无法让打印机打印自定义大小的纸张
本文关键字:自定义 打印 打印机 窗体 Windows 项目 | 更新日期: 2023-09-27 18:08:17
我最近一直在尝试在c#应用程序中打印表单。拍摄表格照片很容易,但我在打印部分卡住了。我读了很多文章和很多解决方案来打印自定义尺寸的纸张(我的意思是我的表格要打印在我为打印机指定的纸张上),但遗憾的是没有一个成功。
到目前为止,我在我的项目中有:
//my method for taking a picture form the form , and then printing it .
void PrintImage(object o, PrintPageEventArgs e)
{
int x = SystemInformation.WorkingArea.X;
int y = SystemInformation.WorkingArea.Y;
int width = this.Width;
int height = this.Height;
Rectangle bounds = new Rectangle(x, y, width, height);
Bitmap img = new Bitmap(width, height);
// e.PageSettings.PaperSize = new PaperSize("62mm", 244, (int)Size.Width) { RawKind = 259 };
this.DrawToBitmap(img, bounds);
Point p = new Point(0, 0);
e.Graphics.DrawImage(img, p);
}
//getting the list of installed printers .
private void comboBox1_Click(object sender, EventArgs e)
{
comboBox1.Items.Clear();
foreach (string printer in PrinterSettings.InstalledPrinters)
{
comboBox1.Items.Add(printer);
}
}
//Print Button Click event declaration
private void btnPrint_Click(object sender, EventArgs e)
{
PrintDocument pd = new PrintDocument();
if (comboBox1.SelectedIndex !=-1)
{
pd.PrinterSettings.PrinterName = comboBox1.SelectedItem.ToString();
}
pd.DefaultPageSettings.PaperSize = new PaperSize("CardSize", 50, 50);
pd.PrintPage += new PrintPageEventHandler(PrintImage);
pd.Print();
}
我发现了:要自定义纸张大小,只需执行以下命令:
PrintDocument pd = new PrintDocument();
PaperSize paperSize = new PaperSize("MyCustomSize", 200, 200 ); //numbers are optional
paperSize.RawKind = (int)PaperKind.Custom;
pd.DefaultPageSettings.PaperSize = paperSize;