【问题标题】:Can't get the printer to print a custom sized paper in C# Windows Form project无法让打印机在 C# Windows 窗体项目中打印自定义尺寸的纸张
【发布时间】:2011-11-14 09:29:55
【问题描述】:

我最近一直在尝试在 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();

    }

【问题讨论】:

    标签: c# printing


    【解决方案1】:

    好的,我发现了: 要制作自定义纸张尺寸,只需简单地做:

              PrintDocument pd = new PrintDocument();
              PaperSize paperSize = new PaperSize("MyCustomSize", 200, 200 ); //numbers are optional
    
               paperSize.RawKind = (int)PaperKind.Custom;
    
               pd.DefaultPageSettings.PaperSize = paperSize;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-04
      • 1970-01-01
      • 1970-01-01
      • 2020-02-17
      • 1970-01-01
      • 1970-01-01
      • 2022-02-01
      • 1970-01-01
      相关资源
      最近更新 更多