【问题标题】:convert windows form to pdf file将 windows 窗体转换为 pdf 文件
【发布时间】:2011-06-16 08:20:47
【问题描述】:

如何将 Windows 表单转换为与表单相同设计的 pdf(pdf 文档)格式

【问题讨论】:

    标签: c# winforms pdf-generation


    【解决方案1】:

    我对此进行了测试,它可以工作。我不喜欢它创建一个临时文件的事实。

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Drawing.Imaging;
    using System.Drawing.Printing;
    using System.IO;
    
    namespace WindowsFormsApplication4
    {
        public partial class Form1 : Form
        {
            private System.IO.Stream streamToPrint;
    
            string streamType;
    
            [System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
            private static extern bool BitBlt
            (
                IntPtr hdcDest, // handle to destination DC
                int nXDest, // x-coord of destination upper-left corner
                int nYDest, // y-coord of destination upper-left corner
                int nWidth, // width of destination rectangle
                int nHeight, // height of destination rectangle
                IntPtr hdcSrc, // handle to source DC
                int nXSrc, // x-coordinate of source upper-left corner
                int nYSrc, // y-coordinate of source upper-left corner
                System.Int32 dwRop // raster operation code
            );
    
    
            private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
            {
                System.Drawing.Image image = System.Drawing.Image.FromStream(this.streamToPrint);
    
                int x = e.MarginBounds.X;
                int y = e.MarginBounds.Y;
    
                int width = image.Width;
                int height = image.Height;
                if ((width / e.MarginBounds.Width) > (height / e.MarginBounds.Height))
                {
                    width = e.MarginBounds.Width;
                    height = image.Height * e.MarginBounds.Width / image.Width;
                }
                else
                {
                    height = e.MarginBounds.Height;
                    width = image.Width * e.MarginBounds.Height / image.Height;
                }
                System.Drawing.Rectangle destRect = new System.Drawing.Rectangle(x, y, width, height);
                e.Graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, System.Drawing.GraphicsUnit.Pixel);
            }
    
    
            private void button1_Click(object sender, EventArgs e)
            {
                String filename = System.IO.Path.GetTempFileName();
    
                Graphics g1 = this.CreateGraphics();
                Image MyImage = new Bitmap(this.ClientRectangle.Width, this.ClientRectangle.Height, g1);
                Graphics g2 = Graphics.FromImage(MyImage);
                IntPtr dc1 = g1.GetHdc();
                IntPtr dc2 = g2.GetHdc();
                BitBlt(dc2, 0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height, dc1, 0, 0, 13369376);
                g1.ReleaseHdc(dc1);
                g2.ReleaseHdc(dc2);
                MyImage.Save(filename, ImageFormat.Png);
                FileStream fileStream = new FileStream(filename, FileMode.Open, FileAccess.Read);
                StartPrint(fileStream, "Image");
                fileStream.Close();
                if (System.IO.File.Exists(filename))
                {
                    System.IO.File.Delete(filename);
                }
            }
    
            public void StartPrint(Stream streamToPrint, string streamType)
            {
    
                this.printDocument1.PrintPage += new PrintPageEventHandler(printDocument1_PrintPage);
    
                this.streamToPrint = streamToPrint;
    
                this.streamType = streamType;
    
                System.Windows.Forms.PrintDialog PrintDialog1 = new PrintDialog();
    
                PrintDialog1.AllowSomePages = true;
                PrintDialog1.ShowHelp = true;
                PrintDialog1.Document = printDocument1;
                DialogResult result = PrintDialog1.ShowDialog();
                if (result == DialogResult.OK)
                {
                    printDocument1.Print();
                }
            }
    
            public Form1()
            {
                InitializeComponent();
            }
    
        }
    

    }

    【讨论】:

    • 如果安装了 pdf 打印机,那么它会给我空白的 pdf 文件
    • 如果表单很长,这将不起作用,我的意思是如果它有滚动条,因为它会捕获屏幕
    【解决方案2】:

    这可能是之前提出的相同问题,并且对此问题有一些有用的建议:Save WinForm to PDF & print multipage WinForm

    【讨论】:

      【解决方案3】:

      看看这个问题(我已经回答过),因为你的问题看起来很相似:

      How Can i Print Array of Images in Winform App in c#

      希望这会有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-04-18
        • 2012-02-05
        • 2021-12-05
        • 2011-05-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多