如何将Windows窗体转换为包括文本框值的PDF [英] How to convert Windows Form to PDF including values of textboxes

查看:87
本文介绍了如何将Windows窗体转换为包括文本框值的PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我已经在Visual Studio 2005中创建了Windows应用程序.在此应用程序中,用户可以将当前活动表单导出到.pdf文件.下一个问题,如何将其与文本框的值一起保存?当我离开他们的epmty时,没有问题,并且保存了pdf图片.然后,在文本框中提供一些输入并获取输出,单击导出",然后显示消息"A generic error occurred in GDI".并在Visual Studio输出窗口中:

Hi,

I have created a Windows Application in Visual Studio 2005. In this application, the user is able to export the current active form to a .pdf-file. Next problem, how do I save it with the values of textboxes? When I leave them epmty, there''s no problem and image en pdf are saved. Then I give some input and get output in textboxes, I click export, and I get a message "A generic error occurred in GDI". And in visual studio output window:

A first chance exception of type ''System.FormatException'' occurred in mscorlib.dll. <br />
A first chance exception of type ''System.Runtime.InteropServices.ExternalException'' occurred in System.Drawing.dll



这是我的代码:

对于捕获活动屏幕:



Here''s my code:

For capturing active screen:

try
            {
                Rectangle bounds = this.Bounds;
                using (Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height))
                {
                    using (Graphics g = Graphics.FromImage(bitmap))
                    {
                        g.CopyFromScreen(new Point(bounds.Left, bounds.Top), Point.Empty, bounds.Size);
                    }
                    bitmap.Save("C://Rectangle.bmp";, ImageFormat.Bmp);
                }
            }
            catch(Exception e)
            {
                MessageBox.Show(e.Message.ToString());
            }



导出为pdf:



For exporting to pdf:

captureScreen();

            PdfDocument doc = new PdfDocument();

            PdfPage oPage = new PdfPage();

            doc.Pages.Add(oPage);
            oPage.Rotate = 90;
            XGraphics xgr = XGraphics.FromPdfPage(oPage);
            XImage img = XImage.FromFile(@"C://Rectangle.bmp");

            xgr.DrawImage(img, 0, 0);

            doc.Save("C://RectangleDocument.pdf");
            doc.Close();



就像我说的那样,使用空文本框进行导出可以很好,但是当具有值时,我会收到错误消息.



As I said, the exporting with empty textboxes goes fine, but when the have a value, I get the error messages.

推荐答案

我自己还没有尝试过,但我有一个问题.如果我没记错的话,默认情况下,将Windows.Form打印到打印机将导致窗体打印也没有值,因此解决方案可能是也使用一些用于实现该目的的技术.

我找到了这篇文章(我认为这是我前一段时间为表格打印所引用的文章,但我现在不太确定了):http://msdn.microsoft.com/en-us/magazine/cc188767.aspx [在C#.Net中打印Windows表单 [
I haven''t tried this myself, but I have a question. If I recall correctly, printing a Windows.Form to a printer will by default result in the form printing without values as well, so the solution may be to use some of the techniques used to achieve that as well.

I found this article (I think this is one I referenced a while back for forms printing, but I''m not entirely sure anymore): http://msdn.microsoft.com/en-us/magazine/cc188767.aspx[^]

The best answer I could find that may help you is this one See Solution 1 from Sergey for the concept: Print Windows form in C#.Net[^]


大家好,

我答应回到这个问题,然后我找到了答案:
Hi all,

I promised to get back to this question, and I found myself an answer:
g.CopyFromScreen(new Point(bounds.Left, bounds.Top), Point.Empty, bounds.Size, CopyPixelOperation.SourceCopy);



我只需要将CopyPixelOperation.SourceCopy作为最后一个参数添加到CopyFromScreen方法.

因此,工作代码如下所示:



I just had to add the CopyPixelOperation.SourceCopy as last parameter to the CopyFromScreen method.

So the working code looks like this:

private void captureScreen()
        {
            try
            {
                Rectangle bounds = this.Bounds;
                using (Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height))
                {
                    using (Graphics g = Graphics.FromImage(bitmap))
                    {
                        g.CopyFromScreen(new Point(bounds.Left, bounds.Top), Point.Empty, bounds.Size, CopyPixelOperation.SourceCopy);
                    }
                    bitmap.Save("C://Rectangle.bmp", ImageFormat.Bmp);
                }
            }
            catch(Exception e)
            {
                MessageBox.Show(e.Message.ToString());
            }


        }

private void btnMenuExport_Click(object sender, EventArgs e)
        {
            captureScreen();

            PdfDocument doc = new PdfDocument();

            PdfPage oPage = new PdfPage();

            doc.Pages.Add(oPage);
            oPage.Rotate = 90;
            XGraphics xgr = XGraphics.FromPdfPage(oPage);
            XImage img = XImage.FromFile(@"C://Rectangle.bmp");

            xgr.DrawImage(img, 0, 0);

            doc.Save("C://RectangleDocument.pdf");
            doc.Close();
        }


我找到了一个更好的解决方案.请检查我的文章:
将Windows表单转换为PDF [
I found a better solution to this. Please check my article:
Convert a Windows Form to PDF[^]


这篇关于如何将Windows窗体转换为包括文本框值的PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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