使用iTextSharp和Window Forms创建pdf

本文关键字:创建 pdf Forms Window iTextSharp 使用 | 更新日期: 2023-09-27 18:24:55

我正试图在我的windows窗体应用程序中创建一个pdf文档,在该应用程序中,我有窗体和用户控件,其中有几个控件,如文本框、richtexbox、标签、按钮、单选按钮等。

我想用这些表单和用户控件中的数据创建pdf,pdf应该看起来像那些表单和用户控制器中的内容、按钮、文本框、标签等,类似于那些表单和使用者控件中的图片。

很抱歉,如果我不清楚,我想添加用户在表单中看到的内容,就像它是一张图片,但不做屏幕截图,就像做这样的事情:

pdfDocument.Add(Button);
pdfDocument.Add(textBox);

也许这个问题是不对的,但我是生成PDF的新手

使用iTextSharp和Window Forms创建pdf

要执行屏幕截图方法,可以执行以下操作:

using (Bitmap b = new Bitmap(this.Width, this.Height))
{
    using (Graphics g = Graphics.FromImage(b))
    {
        g.CopyFromScreen(this.Location, new Point(0, 0), this.Size);
    }
    Document doc = new Document();
    iTextSharp.text.Image i = iTextSharp.text.Image.GetInstance(b, System.Drawing.Imaging.ImageFormat.Bmp);
    PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(@"C:'Temp'output.pdf", FileMode.Create));
    doc.SetPageSize(new iTextSharp.text.Rectangle(this.Size.Width + doc.LeftMargin + doc.RightMargin, this.Size.Height + doc.TopMargin + doc.BottomMargin));
    doc.Open();
    doc.Add(i);
    doc.Close();
}

简单的方法-点击按钮即可截图您的应用程序,并使用itextsharp将该图像嵌入PDF中。

很难(但可能更好的方法)-使用i-extsharp编写一些代码来生成PDF,传递您的数据。。。