2

The following code creates a bitmap from a control on the form, and then shows a save dialog to save as a JPEG. Can anyone help with the code to save the Bitmap bm as a PDF with iTextSharp?

 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);
 }
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Glen
  • 655
  • 3
  • 16
  • 31

2 Answers2

9

You can try this

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

Referenced from here

Chris Stillwell
  • 10,266
  • 10
  • 67
  • 77
Jibran Khan
  • 3,236
  • 4
  • 37
  • 50
  • the idea is not to save the image to a file first. i want to save the "bm" object to the pdf without saving to a file first – Glen Jun 17 '13 at 11:07
  • You can directly pass the bitmap to the image. – Jibran Khan Jun 17 '13 at 11:08
  • your using PdfWriter there, im using iTextSharp ? – Glen Jun 17 '13 at 11:15
  • OK i got this working. But, the image doesn't fit inside the pdf document. Any idea how i can make it fit? – Glen Jun 17 '13 at 11:26
  • You have to play with formatting of the document. if you get this answer helpful then mark as accepted for future help to community. – Jibran Khan Jun 17 '13 at 11:39
  • You can go through this link to check the display formatting http://stackoverflow.com/questions/4325151/adding-an-image-to-a-pdf-using-itextsharp-and-scale-it-properly – Jibran Khan Jun 17 '13 at 11:41
-1
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();
}
decates
  • 3,406
  • 1
  • 22
  • 25
Luis Fajardo
  • 19
  • 1
  • 2
  • 3
    Could you please [edit] in an explanation (in English; the Spanish comments here will be difficult for most users to make any use of) of why this code answers the question? Code-only answers are [discouraged](http://meta.stackexchange.com/q/148272/274165), because they don't teach the solution. – Nathan Tuggy Jul 06 '15 at 01:18
  • @NathanTuggy Here's the comment in English - //here I passed a bitmapped image – JB06 Sep 09 '15 at 16:19