C# 使用iTextSharp和窗口窗体创建pdf

C# 使用iTextSharp和窗口窗体创建pdf,c#,winforms,.net-2.0,itextsharp,C#,Winforms,.net 2.0,Itextsharp,我正在尝试在我的windows窗体应用程序中创建一个pdf文档,在该应用程序中,我有一些窗体和用户控件,其中包括文本框、richtexbox、标签、按钮、单选按钮等控件。 我想用这些表单和用户控件中的数据创建pdf,pdf应该看起来像那些表单和用户控件中的内容、按钮、文本框、标签等,类似于那些表单和用户控件中的图片 抱歉,如果我不清楚,我想添加用户在表单中看到的内容,就像它是一张图片,但不做屏幕截图,就像这样做: pdfDocument.Add(Button); pdfDocument.Add(

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

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

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

pdfDocument.Add(Button);
pdfDocument.Add(textBox);
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();
}

也许这个问题不正确,但我是新来生成PDF的

简易方法-点击一个按钮即可截屏应用程序,并使用itextsharp将该图像嵌入PDF


硬(但可能更好的方法)-使用i-textsharp编写一些代码来生成PDF,并传递数据…

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


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

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

pdfDocument.Add(Button);
pdfDocument.Add(textBox);
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();
}

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

pdfDocument.Add(Button);
pdfDocument.Add(textBox);
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();
}

这里也有人问过类似的问题,听起来。。所以大错特错。如果这是企业给我的真正要求,我会一直反对,直到脸色发青。在构建报告或其他方面,这样做必须有一些非常有力的理由。我对这个问题做了一些编辑,添加了一些注释行。这里有人问了一个类似的问题。听起来。。所以大错特错。如果这是企业给我的真正要求,我会一直反对,直到脸色发青。在构建报告或其他方面,这样做必须有一些非常有力的理由。我对这个问题做了一些编辑,添加了一些aclarations行