we set a resize of the images before the transformation from Word to PDF with a fixed width of 600. In some cases, however, when the PDF is generated, the images do not maintain this resize and are enormous. Example attached: page 4 and page 6 should have two images respectively 600x451 and 600x400 but as you can see on page 4 the photo is huge and not really 600x451. If you export the photos from the PDF but in reality the photos are the correct size, so it would look like a rendering error in generating the PDF.
Below is the code used
- Code: Select all
Paragraph p_image = new Paragraph(document);
p_image.Format.HorizontalAlignment = HorizontalAlignment.Center;
Image imageArt = Image.FromFile(physicalPath + @"numeri\" + num.num_id + "\\" + numa.numa_id + "\\" + numa.numa_immagine);
//calculate the ratio
double dbl = (double)imageArt.Width / (double)imageArt.Height;
int newWidth = imageArt.Width;
double newHeight = imageArt.Height;
if (newWidth > 600)
{
newWidth = 600;
double diff = (double)(imageArt.Width - 600) / dbl;
newHeight = (double)(imageArt.Height - diff);
}
Bitmap objBitmap = ResizeImage(imageArt, newWidth, Convert.ToInt32(newHeight));
using (var memoryStream = new MemoryStream())
{
objBitmap.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Jpeg);
p_image.AppendPicture(memoryStream.ToArray());
}
tempSection.Paragraphs.Add(p_image);