C#使用iTextSharp将位图保存为PDF

时间:2013-06-17 10:57:02

标签: c# pdf bitmap

以下代码从表单上的控件创建一个位图,然后显示一个保存对话框以保存为JPEG。任何人都可以帮助代码将Bitmap bm保存为带有iTextSharp的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);
 }

2 个答案:

答案 0 :(得分:7)

你可以试试这个

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

答案 1 :(得分:0)

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();
}