c# winform如何设置打印纸张大小

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c# winform如何设置打印纸张大小相关的知识,希望对你有一定的参考价值。

Graphics g = e.Graphics;
int frmw = this.Width;
int frmh = this.Height;
this.printDocument1.DefaultPageSettings.PaperSize = new System.Drawing.Printing.PaperSize("Custom", frmw, frmh);
this.printDocument1.DefaultPageSettings.Margins = new System.Drawing.Printing.Margins(0, 0, 0, 0);
foreach (Control crl in this.Controls)

if (crl is TextBox)

int Left = crl.Location.X;
int Top = crl.Location.Y;
int width = crl.Width;
int height = crl.Height;
Font prnFont = new Font(crl.Font.Name, crl.Font.Size, crl.Font.Style);
Rectangle r = new Rectangle(Left, Top, width, height);
g.DrawRectangle(Pens.White, r);
SolidBrush prnBrush = new SolidBrush(crl.ForeColor);
g.DrawString(crl.Text, prnFont, prnBrush, r);//在长方形画字符串


为何我这样设置却没有用????他还是按照默认A4的打印出来

参考技术A 首先你要到指定印表机下,新增加你要列印的纸张大小。
//然后在代码中,指定尺寸
foreach (PaperSize paperSize in printDocument1.PrinterSettings.PaperSizes)

if (paperSize.Width == frmw && paperSize.Height == frmh)

this.printDocument1.DefaultPageSettings.PaperSize = paperSize;
break;


这样列印出来,就是你要的纸张大小
参考技术B 修改属性不可以吗?

c#实现打印功能,可以设置纸张大小,字体和颜色等

 /// <summary>
        /// 打印的按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnPrint_Click(object sender, EventArgs e)
        {
            // printDocument1 为 打印控件
            //设置打印用的纸张 当设置为Custom的时候,可以自定义纸张的大小,还可以选择A4,A5等常用纸型
            this.printDocument1.DefaultPageSettings.PaperSize = new PaperSize("Custom",500,300);
           
            this.printDocument1.PrintPage += new PrintPageEventHandler(this.MyPrintDocument_PrintPage);
            //将写好的格式给打印预览控件以便预览
            printPreviewDialog1.Document = printDocument1;
            //显示打印预览
            DialogResult result = printPreviewDialog1.ShowDialog();
        }

        /// <summary>
        /// 打印的格式
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MyPrintDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            /*如果需要改变自己 可以在new Font(new FontFamily("黑体"),11)中的“黑体”改成自己要的字体就行了,黑体 后面的数字代表字体的大小
             System.Drawing.Brushes.Blue , 170, 10 中的 System.Drawing.Brushes.Blue 为颜色,后面的为输出的位置 */
            e.Graphics.DrawString("新乡市三月软件公司入库单", new Font(new FontFamily("黑体"), 11), System.Drawing.Brushes.Black, 170, 10);
            e.Graphics.DrawString("供货商:河南科技学院", new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Blue, 10, 12);
            //信息的名称
            e.Graphics.DrawLine(Pens.Black, 8, 30, 480, 30);
            e.Graphics.DrawString("入库单编号", new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Black, 9, 35);
            e.Graphics.DrawString("商品名称", new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Black, 160, 35);
            e.Graphics.DrawString("数量", new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Black, 260, 35);
            e.Graphics.DrawString("单价", new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Black, 330, 35);
            e.Graphics.DrawString("总金额", new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Black, 400, 35);
            e.Graphics.DrawLine(Pens.Black, 8, 50, 480, 50);
            //产品信息
            e.Graphics.DrawString("R2011-01-2016:06:35", new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Black, 9, 55);
            e.Graphics.DrawString("联想A460", new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Black, 160, 55);
            e.Graphics.DrawString("100", new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Black, 260, 55);
            e.Graphics.DrawString("200.00", new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Black, 330, 55);
            e.Graphics.DrawString("20000.00", new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Black, 400, 55);


            e.Graphics.DrawLine(Pens.Black, 8, 200, 480, 200);
            e.Graphics.DrawString("地址:新乡市河南科技学院信息工程学院", new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Black, 9, 210);
            e.Graphics.DrawString("经办人:任忌", new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Black, 220, 210);
            e.Graphics.DrawString("服务热线:15083128577", new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Black, 320, 210);
            e.Graphics.DrawString("入库时间:" + DateTime.Now.ToString(), new Font(new FontFamily("黑体"), 8), System.Drawing.Brushes.Black, 9, 230);
        }

 

以上是关于c# winform如何设置打印纸张大小的主要内容,如果未能解决你的问题,请参考以下文章

怎么在C#打印自定义纸张

c#实现打印功能,可以设置纸张大小,字体和颜色等

C#修改打印机默认纸张A4到A3

EXCEL中纸张尺寸如何让设置自定义大小?

C#打印的时候如何设置A4纸张为横向

如何使用 PrintDocument 在热敏打印机上打印文本文件?