【问题标题】:C# Save Bitmap as PDF With iTextSharpC# 使用 iTextSharp 将位图保存为 PDF
【发布时间】:2013-06-17 10:57:01
【问题描述】:

以下代码从窗体上的控件创建位图,然后显示保存对话框以保存为 JPEG。任何人都可以帮助使用 iTextSharp 将位图 bm 保存为 PDF 的代码吗?

 Bitmap bm = null;
 bm = new Bitmap(this.RCofactorTBS.SelectedTab.Width, this.RCofactorTBS.SelectedTab.Height);
 this.RCofactorTBS.SelectedTab.DrawToBitmap(bm, this.RCofactorTBS.SelectedTab.ClientRectangle);

 SaveFileDialog dialog = new SaveFileDialog();
 dialog.Filter = "JPEG|*.jpeg";
 dialog.Title = "Save Test As Jpeg";
 dialog.ShowDialog();

 if (dialog.FileName != "" && bm != null)
 {
    bm.Save(dialog.FileName);
 }

【问题讨论】:

    标签: c# pdf bitmap


    【解决方案1】:

    你可以试试这个

    System.Drawing.Image image = System.Drawing.Image.FromFile("Your image file path");
    Document doc = new Document(PageSize.A4);
    PdfWriter.GetInstance(doc, new FileStream("image.pdf", FileMode.Create));
    doc.Open();
    iTextSharp.text.Image pdfImage = iTextSharp.text.Image.GetInstance(image, System.Drawing.Imaging.ImageFormat.Jpeg);
    doc.Add(pdfImage);
    doc.Close();
    

    引用自here

    【讨论】:

    • 这个想法不是先将图像保存到文件中。我想将“bm”对象保存到pdf而不先保存到文件中
    • 可以直接将位图传给图片。
    • 你在那里使用 PdfWriter,我使用 iTextSharp 吗?
    • 好的,我得到了这个工作。但是,图像不适合 pdf 文档。知道如何使它适合吗?
    • 您必须使用文档的格式。如果您认为此答案有帮助,请标记为已接受,以便将来对社区提供帮助。
    【解决方案2】:
    public void exportarPDF(Bitmap img){           
        // System.Drawing.Image image = System.Drawing.Image.FromFile("C://snippetsource.jpg"); // Here it saves with a physical file
        System.Drawing.Image image = img;  //Here I passed a bitmap
        Document doc = new Document(PageSize.A4);
        PdfWriter.GetInstance(doc, new FileStream("C://image.pdf", FileMode.Create));
        doc.Open();
        iTextSharp.text.Image pdfImage = iTextSharp.text.Image.GetInstance(image,
                System.Drawing.Imaging.ImageFormat.Jpeg);
        doc.Add(pdfImage);
        doc.Close();
    }
    

    【讨论】:

    • 您能否请edit 解释一下为什么这段代码回答了这个问题?纯代码答案是 discouraged,因为它们不教授解决方案。
    • @NathanTuggy 这是英文评论 - //这里我传递了一个位图图像
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-10
    • 2016-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多