I am attempting to reduce the size of my PDF files by replacing images with "shrunken" images.
To shrink the images, I am attempting to use XImage.Save() with a resolution of 144 DPI.
The size of the image saved is not reduced in any way; and calling Resources.Images.Replace() seems to be resulting in a larger file than the original!
What can I do to make this work correctly?
The attached .zip file contains three files:
Original: (S)TEST Color 600 highest.pdf
Exported Image: (S)TEST Color 600 highest.pdf.Page 1.1.Bmp
Final: (S)TEST Color 600 highest.pdf.final.pdf
Here is the conversion function:
private static void ReduceImageSizes(string inFile, string outFile, int pixelsPerInch) {
pdf.Document pdfDocument = new pdf.Document(inFile);
foreach (pdf.Page page in pdfDocument.Pages) {
int idx = 1;
foreach (pdf.XImage image in page.Resources.Images) {
try {
System.Drawing.Imaging.ImageFormat fmt = System.Drawing.Imaging.ImageFormat.Bmp;
string filename = IBSCompression.MakeTempFilename(inFile, "Page " + idx, fmt.ToString());
using (var imageStream = new FileStream(filename, FileMode.CreateNew)) {
image.Save(imageStream, fmt, pixelsPerInch);
// todo: resize image to (image.Width * pixelsPerInch) * (image.Height * pixelsPerInch)
imageStream.Seek(0, SeekOrigin.Begin);
page.Resources.Images.Replace(idx, imageStream);
}
} catch (Exception) {
// ignore the exception
}
idx = idx + 1;
}
}
pdfDocument.Save(outFile);
}
To shrink the images, I am attempting to use XImage.Save() with a resolution of 144 DPI.
The size of the image saved is not reduced in any way; and calling Resources.Images.Replace() seems to be resulting in a larger file than the original!
What can I do to make this work correctly?
The attached .zip file contains three files:
Original: (S)TEST Color 600 highest.pdf
Exported Image: (S)TEST Color 600 highest.pdf.Page 1.1.Bmp
Final: (S)TEST Color 600 highest.pdf.final.pdf
Here is the conversion function:
private static void ReduceImageSizes(string inFile, string outFile, int pixelsPerInch) {
pdf.Document pdfDocument = new pdf.Document(inFile);
foreach (pdf.Page page in pdfDocument.Pages) {
int idx = 1;
foreach (pdf.XImage image in page.Resources.Images) {
try {
System.Drawing.Imaging.ImageFormat fmt = System.Drawing.Imaging.ImageFormat.Bmp;
string filename = IBSCompression.MakeTempFilename(inFile, "Page " + idx, fmt.ToString());
using (var imageStream = new FileStream(filename, FileMode.CreateNew)) {
image.Save(imageStream, fmt, pixelsPerInch);
// todo: resize image to (image.Width * pixelsPerInch) * (image.Height * pixelsPerInch)
imageStream.Seek(0, SeekOrigin.Begin);
page.Resources.Images.Replace(idx, imageStream);
}
} catch (Exception) {
// ignore the exception
}
idx = idx + 1;
}
}
pdfDocument.Save(outFile);
}