如何在特定纸张上打印面板内容? [英] how can I print a panel content on particular papers?

查看:88
本文介绍了如何在特定纸张上打印面板内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好

我有一些纸质表格有一些未填充的部分,我想通过我的点阵式打印机以编程方式填写它们。所以我制作了一个有一些标签的面板。这些标签在面板上有指定的位置。但是我不能在没有面板背景的情况下打印它们。

这是我的代码的一部分,它为标签提供文本并打印出来:

hi everyone
I have some paper forms that have some unfilled parts and i want to fill them programmatically via my dot matrix printer on my ready paper forms. so i made a panel which has some labels. and these labels have specified locations on the panel. but i can't print them without panel's background.
this is a part of my code that gives labels their text and print them:

private void button5_Click(object sender, EventArgs e)
        {
            int k=0;
            for (int i = 0; i < dataGridView1.RowCount && k < 3878; i++)
            {
                k = Convert.ToInt32(dataGridView1.Rows[i].Cells[0].Value);
                ctot25 = Convert.ToInt32(dataGridView1.Rows[i].Cells[13].Value);
                ctot50 = Convert.ToInt32(dataGridView1.Rows[i].Cells[14].Value);
                ctot100 = Convert.ToInt32(dataGridView1.Rows[i].Cells[15].Value);

                if (i > 0 && ctot25 != 0 || ctot50 != 0 || ctot100 != 0)
                {
                    label33.Text = (dataGridView1.Rows[i].Cells[1].Value).ToString();
                    label34.Text = (dataGridView1.Rows[i].Cells[0].Value).ToString();
                    System.Drawing.Printing.PrintDocument doc = new                System.Drawing.Printing.PrintDocument();
            doc.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(printDocument1_PrintPage);
            PrintDialog PrintSettings = new PrintDialog();
            PrintSettings.Document = doc;
            PageSettings pgsetting = new PageSettings();

            if (PrintSettings.ShowDialog() == DialogResult.OK)
                doc.Print();
                }
            }

private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            Bitmap bmp = new Bitmap(panel1.Width, panel1.Height, panel1.CreateGraphics());
            panel1.DrawToBitmap(bmp, new Rectangle(0, 0, panel1.Width, panel1.Height));

            RectangleF bounds = e.PageSettings.PrintableArea;
            e.Graphics.DrawImage(bmp, bounds.Left, bounds.Top, panel1.Width, panel1.Height);
        }






每个结尾的
我应该打印一张表格。




at the end of each for I should have one of the forms printed.

推荐答案

问题是你只是将面板绘制成一个位图 - 所以你会得到完全相同的东西,就像你把它画到屏幕上一样 - 小组不会知道它在哪里画(并且正确地说,它不应该知道)。



如果你想失去背景,你必须自己绘制面板内容 - 或者让内容自己绘制,具体取决于面板包含的内容 - 通过查看面板控件并确定你想用它们做什么。



很抱歉,如果这听起来很模糊,但是根据您提供的信息,我们无法准确。



顺便说一句:不要像这样使用CreateGraphics:你负责自己清理,如果你只是把它传递给Bitmap构造函数就不能这样做。图形上下文是一种稀缺资源,如果你保持这样的编码,你将耗尽它们!您需要保留一个引用Graphics对象并在完成后将其公开。
The problem is that you are just drawing the panel into a bitmap - so you will get exactly teh same thing as if you had drawn it onto the screen - the panel doesn't "know" where it is drawing to (and rightly, it shouldn't need to know).

If you want to lose the background, you will have to draw the panel content yourself - or get the content to draw it itself, depending on what the panel contains - by looking at the panel Controls and working out what you want to do with them.

Sorry if that sounds vague, but from the info you give, we can't be precise.

BTW: don't use CreateGraphics like that: you are responsible for cleaning up after yourself, and you can't do that if you just pass it to a Bitmap constructor. Graphics contexts are a scarce resource, and you will run out of them if you keep coding like that! You need to keep a reference the the Graphics object and Dispose it when you are finished with it.


这篇关于如何在特定纸张上打印面板内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆