We are trying to optimize our pdf compression.
According to this link:
Different type of compression can be applied over images to reduce their size. The type of compression being applied over image depends upon the ColorSpace of source image i.e. if image is Color (RGB), then apply JPEG2000 compression, and if it is Black & White, then JBIG2/JBIG2000 compression should be applied. Therefore identifying each image type and using an appropriate type of compression will create best/optimized output.
Does Aspose provide all functionality to apply these compressions?
In the following code snippet I tried to replace the jpeg of the pdf by the same image with JBIG2 compression generated by another library. However when looking at the result the original pdf (601 KB) is now 1525 KB after adding the JBIG2 jpeg file (84 KB). I extracted the JPEG image again after the replacement and the result JPEG is 435 KB, 150 dpi instead of 300, full color instead of B/W and also unreadable. Is there a way to replace an image without these alterations?
I also added the result pdf we are trying to achieve, which was generated by another application, which is B/W, 300 dpi and only 32 KB.
Below is the code I used for this test. I have been playing around with the optimization settings but without a satisfying result.
Am I missing something?
private void CompressPdf()
Aspose.Pdf.Document doc = new Aspose.Pdf.Document("Original.pdf");
foreach (Page page in doc.Pages)
int idx = 1;
foreach (XImage image in page.Resources.Images)
using (var fileStream = new FileStream("JBIG2.jpeg", FileMode.Open, FileAccess.ReadWrite))
//using (var imageStream = new MemoryStream())
// // To compress images change the image type and resolution
// image.Save(imageStream, System.Drawing.Imaging.ImageFormat.Png, 72);
// imageStream.Seek(0, System.IO.SeekOrigin.Begin);
// // Control image quality for better compression
// page.Resources.Images.Replace(idx, imageStream, 80);
idx = idx + 1;
//Output JBIG file after replace in pdf
using (var fileStream = new FileStream("JBIG2 After replace.jpeg", FileMode.CreateNew, FileAccess.ReadWrite))
// Load source PDF file
Aspose.Pdf.Document document = new Aspose.Pdf.Document(sFile);
// Optimzie the file size by removing unused objects
LinkDuplcateStreams = true,
RemoveUnusedObjects = true,
RemoveUnusedStreams = true,
CompressImages = false
// Save the updated file