I have an image file that I need to pass to PDF and for this I am using iTextSharp .
I'm trying to do this in the following way:
private static void Main(string[] args)
{
using (var stream = new FileStream("document.pdf", FileMode.Create))
{
using (var document = new Document(PageSize.A4))
{
PdfWriter.GetInstance(document, stream);
document.SetMargins(20, 20, 20, 20);
document.AddCreationDate();
document.Open();
using (var picture = new Bitmap("model256.bmp"))
{
double percentage;
if (picture.Height > picture.Width)
percentage = document.PageSize.Height / picture.Height;
else
percentage = document.PageSize.Width / picture.Width;
using (var newPicture = Util.ResizeImage(picture,
Convert.ToInt32(picture.Width * percentage),
Convert.ToInt32(picture.Height * percentage)))
{
var image = iTextSharp.text.Image.GetInstance(newPicture,
BaseColor.WHITE);
image.Alignment = Element.ALIGN_CENTER;
document.Add(image);
document.Close();
}
}
}
}
}
With this help to resize:
public static class Util
{
public static Bitmap ResizeImage(Bitmap bitmap, int newWidth, int newHeight)
{
var width = bitmap.Width;
var heigth = bitmap.Height;
if (width > newWidth || heigth > newHeight)
{
if (width > heigth)
{
heigth = (heigth * newWidth) / width;
width = 500;
}
else
{
width = (width * newHeight) / heigth;
heigth = 500;
}
}
var image = bitmap.GetThumbnailImage(width, heigth, null, new System.IntPtr(0));
return new Bitmap(image);
}
}
The format of the images is bmp .
Here you have the images placed side by side for comparison
The stackoverflow editor resizes the images so it looks bad here. But by clicking on both images it appears in real size and with the right quality.
HowtogeneratePDFwithgoodimagequality?
EDITION
MakinguseofGraphics
UsingGraphicstotrytoimprovetheoutputimagealsohasnotbeenimproved.
private static void Main(string[] args)
{
using (var stream = new FileStream("document.pdf", FileMode.Create))
{
using (var document = new Document(PageSize.A4))
{
PdfWriter.GetInstance(document, stream);
document.SetMargins(20, 20, 20, 20);
document.AddCreationDate();
document.Open();
using (var picture = new Bitmap("original.bmp"))
{
var width = Convert.ToInt32(Math.Round(
document.PageSize.Width - 40));
var height = Convert.ToInt32(Math.Round(
((float)picture.Height / (float)picture.Width) * (width)));
using (var newPicture = Util.RedimensionarImagem(
picture, width, height, PixelFormat.Format48bppRgb))
{
var image = Image.GetInstance(newPicture, BaseColor.WHITE);
image.Alignment = Element.ALIGN_CENTER;
document.Add(image);
document.Close();
}
}
}
}
}
And the resize routine:
public static Image RedimensionarImagem(Image image, int width, int height, PixelFormat pf)
{
if (image != null)
{
var newImage = new Bitmap(width, height, pf);
using (var gr = Graphics.FromImage(newImage))
{
//gr.SmoothingMode = SmoothingMode.HighQuality;
gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
//gr.PixelOffsetMode = PixelOffsetMode.HighQuality;
gr.DrawImage(image, 0, 0, width, height);
gr.Dispose();
return newImage;
}
}
return null;
}
Sample image:
ResizingbyobjectiTextSharp.text.Image
Whenusingtheoriginalimagewithoutresizingit,andresizingtheiTextSharpobject,Iwasabletogetalittleimprovement.
Inseveralattemptstoresizetheimageforthebestconversion,InoticedthattherewasstillalosswhenpassingtheimagetoiTextSharpImageandthentoDocument
Butstillitpresentsanimagewithlowquality!
Code:
using (var document = new Document(PageSize.A4))
{
document.SetMargins(20, 20, 20, 20);
document.AddCreationDate();
document.Open();
using (var picture = new Bitmap("original.bmp"))
{
var width = Convert.ToInt32(Math.Round(document.PageSize.Width - 40));
var height = Convert.ToInt32(Math.Round(((float)picture.Height / (float)picture.Width) * (width)));
using (var writer = PdfWriter.GetInstance(document, stream))
{
writer.SetPdfVersion(PdfWriter.PDF_VERSION_1_7);
writer.CompressionLevel = PdfStream.NO_COMPRESSION;
document.Open();
var image = iTextSharp.text.Image.GetInstance(bitmap, BaseColor.WHITE);
image.Alignment = Element.ALIGN_CENTER;
image.SetDpi(600, 600);
image.ScaleToFit(width, heigth);
document.Add(image);
document.Close();
}
}
}
Result