6

This question compress pdf with large images via java gives the code to compress a PDF with iText in Java. Since in Android there are no Graphics2D, AffineTransform, BufferedImage and ImageIO, I thought of adapting this code by using the Bitmap class.

UPDATE: Thanks to @TilmanHausherr, I modified the compression to JPEG and got the following working code.

public static void manipulatePdf(String src, String dest) throws IOException, DocumentException {
    // Read the file
    PdfReader reader = new PdfReader(src);
    int n = reader.getXrefSize();
    PdfObject object;
    PRStream stream;
    // Look for image and manipulate image stream
    for (int i = 0; i < n; i++) {
        object = reader.getPdfObject(i);
        if (object == null || !object.isStream())
            continue;
        stream = (PRStream)object;
        // if (value.equals(stream.get(key))) {
        PdfObject pdfsubtype = stream.get(PdfName.SUBTYPE);
        System.out.println(stream.type());
        if (pdfsubtype != null && pdfsubtype.toString().equals(PdfName.IMAGE.toString())) {
            PdfImageObject image = new PdfImageObject(stream);
            byte[] imageBytes= image.getImageAsBytes();

            Bitmap bmp;
            bmp = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length);
            if (bmp == null) continue;

            int width=bmp.getWidth();
            int height=bmp.getHeight();

            Bitmap outBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
            Canvas outCanvas=new Canvas(outBitmap);
            outCanvas.drawBitmap(bmp, 0f, 0f, null);

            ByteArrayOutputStream imgBytes = new ByteArrayOutputStream();

            outBitmap.compress(Bitmap.CompressFormat.JPEG, 50, imgBytes);

            stream.setData(imgBytes.toByteArray(), false, PdfStream.BEST_COMPRESSION);
            stream.put(PdfName.FILTER, PdfName.DCTDECODE);
            imgBytes.close();
        }
    }
    // Save altered PDF
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
    stamper.close();
    reader.close();
}

Unfortunately JPEG is not a good option in my case since I have images with transparencies. Using PNG produces blank images even if I set the filter to FLATEDECODE, which to my understanding should be the PNG filter.

The only working code that I have for PNG is this:

   public static void manipulatePdf(String src, String dest) throws IOException, DocumentException {
    // Read the file
    PdfReader reader = new PdfReader(src);
    int n = reader.getXrefSize();
    PdfObject object;
    PRStream stream;
    // Look for image and manipulate image stream
    for (int i = 0; i < n; i++) {
        object = reader.getPdfObject(i);
        if (object == null || !object.isStream())
            continue;
        stream = (PRStream)object;
        // if (value.equals(stream.get(key))) {
        PdfObject pdfsubtype = stream.get(PdfName.SUBTYPE);
        System.out.println(stream.type());
        if (pdfsubtype != null && pdfsubtype.toString().equals(PdfName.IMAGE.toString())) {
            byte[] streamBytes= PdfReader.getStreamBytes(stream);
            stream.setData(streamBytes, true, PdfStream.BEST_COMPRESSION);
        }
    }
    // Save altered PDF
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
    stamper.close();
    reader.close();
}

The code above delegates the compression to iText, which may be or may be not enough.

Community
  • 1
  • 1
vitil
  • 256
  • 1
  • 3
  • 13
  • why did you use the pdfbox label? – Tilman Hausherr Jul 07 '15 at 06:13
  • 1
    I didn't understand why the question was down voted, so I up voted it because it is a very good question (and I my Android knowledge is too limited to answer it). However, I agree with @TilmanHausherr: the pdfbox label shouldn't be there. I won't remove it, or I risk getting hate more down votes and false accusations like I received the last couple of days. – Bruno Lowagie Jul 07 '15 at 07:18
  • Sorry, I just copied the labels from the original question and substituted Java with Android. My apologies – vitil Jul 07 '15 at 07:21
  • @BrunoLowagie I didn't downvote it. The question looks legit. – Tilman Hausherr Jul 07 '15 at 07:25
  • @TilmanHausherr OK, no problem; sorry for assuming that. I see a close vote to, indicating that the question isn't clear. That's not true. The question is very clear. It contains a code sample and the question is very clear: *how do you apply the compression of images that works in Java to an Android context where one can't use AWT classes?* At first sight, I don't see anything wrong with the code sample, but I am not familiar with the `BitMap` class on Android and that's probably where the problem arises. – Bruno Lowagie Jul 07 '15 at 08:20
  • 2
    @user2358649 I see Bitmap.CompressFormat.PNG, and then DCTDECODE - are you sure about that? DCT is for Jpeg. – Tilman Hausherr Jul 07 '15 at 08:24
  • @TilmanHausherr Good catch. Replace `CompressFormat.PNG` with `CompressFormat.JPEG` and let us know what happens. – Bruno Lowagie Jul 07 '15 at 12:50
  • Please see edited question – vitil Jul 07 '15 at 21:52
  • 1
    @user2358649 "I set the filter to FLATEDECODE, which to my understanding should be the PNG filter" - no it is not, Flate is a different compression format than PNG. I guess my "help" ends here, as I don't know iText, and I don't know how it handles transparencies on the API level (in PDF, there are images and masks, which bring the transparency effect). – Tilman Hausherr Jul 07 '15 at 23:28

0 Answers0