C# Save Bitmap as PDF With iTextSharp
16,270
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
Author by
Glen
Updated on June 05, 2022Comments
-
Glen almost 2 years
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); }
-
Glen almost 11 yearsthe 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
-
Jibran Khan almost 11 yearsYou can directly pass the bitmap to the image.
-
Glen almost 11 yearsyour using PdfWriter there, im using iTextSharp ?
-
Glen almost 11 yearsOK i got this working. But, the image doesn't fit inside the pdf document. Any idea how i can make it fit?
-
Jibran Khan almost 11 yearsYou 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 almost 11 yearsYou can go through this link to check the display formatting stackoverflow.com/questions/4325151/…
-
Nathan Tuggy almost 9 yearsCould 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, because they don't teach the solution.
-
JB06 over 8 years@NathanTuggy Here's the comment in English - //here I passed a bitmapped image