-1
I have an image file that I need to pass to PDF and for this I am using the iTextSharp.
So I’m trying this 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 are the images placed side by side for comparison
The stackoverflow editor resizes the images and so here it looks bad. But by clicking on both images it appears in full size and with the correct quality.
How to generate PDF with good image quality?
EDITION
Making use of Graphics
Using Graphics to try to improve the output image also had no improvements.
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:
Resizing by the object iTextSharp.text.Image
When using the original image, without resizing it, and resizing the object Image of iTextSharp I managed to get a little improvement.
In various attempts of resizing the image seeking the best conversion I found that still there was loss while passing the image to the Image of iTextSharp and then to the Document
But still presents a low-quality image!
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();
}
}
}
Upshot
Thanks for the answer! I even got a resized image with more quality. However, I still get poor quality when I go to PDF. I got a small improvement by resizing the original image inside Image of iTextSharp.
– JamesTK