I need to check the resolution of images in a word document. The requirement is to have a minimum DPI of 290 for all images.
I am using the below code for the same. (assuming for now that all shapes are images)
foreach (Shape image in doc.GetChildNodes(NodeType.Shape, true))
{
double imageResolution = image.ImageData.ImageSize.HorizontalResolution;
}
I have attached the document that I am validating.
For both the images the resolution is 220DPI.
After validation I need to upsample the resolution. I am using the below code:
foreach (Shape image in doc.GetChildNodes(NodeType.Shape, true))
{
if (image.ImageData.ImageSize.HorizontalResolution < expectedDPI && image.HasImage)
{
using (Bitmap bitmapImage = new Bitmap(image.ImageData.ToImage()))
{
bitmapImage.SetResolution(290, 290);
MemoryStream stream = new MemoryStream();
bitmapImage.Save(stream, GetImageType(image.ImageData.ImageType.ToString()));
image.ImageData.SetImage(stream);
}
}
}
After upsampling I convert the word to pdf using the below code.
PdfSaveOptions saveOptions = new PdfSaveOptions();
saveOptions.EmbedFullFonts = true;
saveOptions.DownsampleOptions.DownsampleImages = false;
saveOptions.UseHighQualityRendering = true;
saveOptions.ColorMode = ColorMode.Grayscale;
saveOptions.ImageCompression = PdfImageCompression.Jpeg;
saveOptions.JpegQuality = 100;
saveOptions.ImageColorSpaceExportMode = bPdfImageColorSpaceExportMode.SimpleCmyk;
saveOptions.SaveFormat = SaveFormat.Pdf;
MemoryStream stream=new MemoryStream();
_sourceDocument.Save(stream, saveOptions);
In the pdf I am getting different resolutions (used acrobat pro)
Image 1 – 220 DPI
Image 2 – 134 DPI
We can see that the upsample code didn’t work. But then both the images should show a DPI of 220.
I verified that acrobat pro is giving correct results using the formula – DPI = width in pixels/width in inches.
I came to a conclusion that image.ImageData.ImageSize.HorizontalResolution is giving wrong resolution. I think it is not considering the fact that the image 2 has been resized. (On resize the resolution should change right.)
Also is there a way to up sample images and then convert to pdf?
I saved the image in the up sampled document to local folder and checked the properties to find that the resolution is 290 DPI.