// creation of the document with a certain size and certain margins
iTextSharp.text.Document document = new iTextSharp.text.Document(iTextSharp.text.PageSize.A4, 0, 0, 0, 0);
// creation of the different writers
iTextSharp.text.pdf.PdfWriter writer = iTextSharp.text.pdf.PdfWriter.GetInstance(document, new System.IO.FileStream(Server.MapPath("~/App_Data/result.pdf"), System.IO.FileMode.Create));
// load the tiff image and count the total pages
System.Drawing.Bitmap bm = new System.Drawing.Bitmap(Server.MapPath("~/App_Data/source.tif")); int total = bm.GetFrameCount(System.Drawing.Imaging.FrameDimension.Page);
document.Open();
iTextSharp.text.pdf.PdfContentByte cb = writer.DirectContent; for (int k = 0; k < total; ++k)
{
bm.SelectActiveFrame(System.Drawing.Imaging.FrameDimension.Page, k);
iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(bm, System.Drawing.Imaging.ImageFormat.Bmp); // scale the image to fit in the page
img.ScalePercent(72f / img.DpiX * 100);
img.SetAbsolutePosition(0, 0);
cb.AddImage(img);
document.NewPage();
}
document.Close();
but for this you have to use thied party dll itextsharp.
justshahzad
Member
21 Points
34 Posts
how can I Covert an image to pdf document using C#
Mar 30, 2012 11:35 AM|LINK
Hi,
Can anyone tell, how can I Covert an image to pdf document using c#
Regards,
shahzad
steve.watson...
Member
244 Points
52 Posts
Re: how can I Covert an image to pdf document using C#
Mar 30, 2012 11:42 AM|LINK
// creation of the document with a certain size and certain margins
iTextSharp.text.Document document = new iTextSharp.text.Document(iTextSharp.text.PageSize.A4, 0, 0, 0, 0);
// creation of the different writers
iTextSharp.text.pdf.PdfWriter writer = iTextSharp.text.pdf.PdfWriter.GetInstance(document, new System.IO.FileStream(Server.MapPath("~/App_Data/result.pdf"), System.IO.FileMode.Create));
// load the tiff image and count the total pages
System.Drawing.Bitmap bm = new System.Drawing.Bitmap(Server.MapPath("~/App_Data/source.tif"));
int total = bm.GetFrameCount(System.Drawing.Imaging.FrameDimension.Page);
document.Open();
iTextSharp.text.pdf.PdfContentByte cb = writer.DirectContent;
for (int k = 0; k < total; ++k)
{
bm.SelectActiveFrame(System.Drawing.Imaging.FrameDimension.Page, k);
iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(bm, System.Drawing.Imaging.ImageFormat.Bmp);
// scale the image to fit in the page
img.ScalePercent(72f / img.DpiX * 100);
img.SetAbsolutePosition(0, 0);
cb.AddImage(img);
document.NewPage();
}
document.Close();
but for this you have to use thied party dll itextsharp.
download it from:
http://sourceforge.net/projects/itextsharp/
justshahzad
Member
21 Points
34 Posts
Re: how can I Covert an image to pdf document using C#
Mar 30, 2012 11:45 AM|LINK
will you please elaborate "img.ScalePercent(72f / img.DpiX * 100);"
this code always convert a half image.
Richey
Contributor
3816 Points
431 Posts
Re: how can I Covert an image to pdf document using C#
Apr 05, 2012 12:10 PM|LINK
HI,
You can refer to the below link about the sample.
http://www.codeproject.com/Articles/28609/Converting-Image-Files-to-PDF
justshahzad
Member
21 Points
34 Posts
Re: how can I Covert an image to pdf document using C#
Apr 05, 2012 02:23 PM|LINK
above code create the pdf, but if the image is large, created PDF contain incomplete image.
Richey
Contributor
3816 Points
431 Posts
Re: how can I Covert an image to pdf document using C#
Apr 11, 2012 03:04 AM|LINK
Hi,
If so, you can resize the image first when it is very large.
http://forums.asp.net/t/1530762.aspx
http://www.codeproject.com/Articles/31180/Image-resizing-with-asp-net
mou_inn
Participant
778 Points
952 Posts
Re: how can I Covert an image to pdf document using C#
Apr 09, 2013 02:33 PM|LINK
which dlli need to include