You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@pdfbox.apache.org by Wolfgang Ditz <wo...@uibk.ac.at> on 2013/03/10 20:27:44 UTC

swapping images no longer working

Hello!

I've been using PDFbox 1.5.0 to compress (huge, scanned) images in pdf 
files to get them down to practical sizes.
After updating the library to PDFbox 1.7.1 my method of doing so stopped 
working.

Extracting the image still works flawlessly, but assigning the 
compressed image seems to have no effect.
The "modified" file is equivalent to a file that's just been parsed and 
saved again.
It seemed to me that the getImages() returns a readOnly view now.
So i tried using the COS* datastructures but only managed to trash my files.

compressPDF(final File input, final File output, final int quality) {
     PDFParser parser = new PDFParser(new FileInputStream(input));
     parser.parse();
     PDDocument doc = parser.getPDDocument();
     for (final PDPage p : doc.getDocumentCatalog().getAllPages()) {
         Map<String, PDXObjectImage> images = p.getResources().getImages();
         for (final String s : images.keySet()) {
             PDXObjectImage i = images.get(s);
             File ovImg = new File(tmpDir, s + "." + i.getSuffix());
             i.write2file(ovImg);
             File newImage = ImgUtil.compress(ovImg, quality);
             /*omitting code to handle different image formats, using 
jpg as example*/
             PDJpeg jpg = new PDJpeg(doc, new FileInputStream(newImg));
             images.put(s, jpg);
             }
         }
         doc.save(output.getPath());
         doc.close();
     }

Any hints how to get back on track ?

Thanks in advance!
  wolfgang