public static void Run()
{
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_WorkingWithImages();
string fileName = "Test.docx";
string srcFileName = dataDir + fileName;
Console.WriteLine("Loading {0}. Size {1}.", srcFileName, GetFileSize(srcFileName));
Document doc = new Document(srcFileName);
// 220ppi Print - said to be excellent on most printers and screens.
// 150ppi Screen - said to be good for web pages and projectors.
// 96ppi Email - said to be good for minimal document size and sharing.
const int desiredPpi = 150;
// In .NET this seems to be a good compression / quality setting.
const int jpegQuality = 90;
// Resample images to desired ppi and save.
int count = Resampler.Resample(doc, desiredPpi, jpegQuality);
Console.WriteLine("Resampled {0} images.", count);
if (count != 1)
Console.WriteLine("We expected to have only 1 image resampled in this test document!");
string dstFileName = dataDir + RunExamples.GetOutputFilePath(fileName);
doc.Save(dstFileName);
Console.WriteLine("Saving {0}. Size {1}.", dstFileName, GetFileSize(dstFileName));
// Verify that the first image was compressed by checking the new Ppi.
doc = new Document(dstFileName);
Shape shape = (Shape)doc.GetChild(NodeType.Shape, 0, true);
double imagePpi = shape.ImageData.ImageSize.WidthPixels / ConvertUtil.PointToInch(shape.SizeInPoints.Width);
Debug.Assert(imagePpi < 150, "Image was not resampled successfully.");
Console.WriteLine("\nCompressed images successfully.\nFile saved at " + dstFileName);
}
public static int GetFileSize(string fileName)