Pdf image replace JBIG2

Hi,

We are trying to optimize our pdf compression.

According to this link:

http://www.aspose.com/docs/display/pdfnet/Identify+if+image+inside+PDF+is+Colored+or+Black+&+White

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))
{
page.Resources.Images.Replace(idx, fileStream);
}

//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))
{
page.Resources.Images[1].Save(fileStream, System.Drawing.Imaging.ImageFormat.Jpeg);
}
}

// Load source PDF file
Aspose.Pdf.Document document = new Aspose.Pdf.Document(sFile);
// Optimzie the file size by removing unused objects
doc.OptimizeResources(new Aspose.Pdf.Document.OptimizationOptions()
{
LinkDuplcateStreams = true,
RemoveUnusedObjects = true,
RemoveUnusedStreams = true,
CompressImages = false
});
// Save the updated file
doc.Save("result.pdf");
}

Best Regards

Hi Johan,


Thanks for your inquiry. Please use following code optimize the PDF document. However Aspose.Pdf is optimizing document to 236 kb instead 32 kb, so we have logged a ticket PDFNEWNET-39066 to investigate it further. We will keep you updated about the issue resolution progress.

// Load source PDF file<o:p></o:p>

Aspose.Pdf.Document document = new Document(myDir+"Original.pdf");

// Optimzie the file size by removing unused objects

document.OptimizeResources(new Document.OptimizationOptions()

{

LinkDuplcateStreams = true,

RemoveUnusedObjects = true,

RemoveUnusedStreams = true,

CompressImages = true,

ImageQuality = 10

});

// Save the updated file

document.Save(myDir+"Optimized.pdf");

We are sorry for the inconvenience caused.

Best Regards,

Hi,


I had tried your optimization settings but my result pdf was also a lot bigger then the expected 32 KB. It seems the ImageReplace functionality is alterning the image when performing the replace. If I reextract the image after the replace it is stored as full color instead of B/W with JBIG2 compression.

Is it possible to perform a replace that doesn’t affect the image presented?

Best Regards

Hi Johan,


Thanks for your inquiry. We have logged a ticket PDFNEWNET-39096 to investigate the Images.Replace() method functionality and for its rectification accordingly. We will keep you updated about the issue resolution progress within this forum thread.

We are sorry for the inconvenience caused.

Best Regards,