在Android中使用大图像压缩pdf

时间:2015-07-06 22:33:35

标签: android image pdf compression itext

此问题compress pdf with large images via java提供了使用Java中的iText压缩PDF的代码。 因为在Android中没有Graphics2D,AffineTransform,BufferedImage和ImageIO,所以我想通过使用Bitmap类来调整这段代码。

更新 感谢@TilmanHausherr,我将压缩修改为JPEG并获得了以下工作代码。

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();
}

不幸的是,在我的情况下,JPEG不是一个好选择,因为我有透明胶片的图像。即使我将滤镜设置为FLATEDECODE,使用PNG也会产生空白图像,据我所知,这应该是PNG滤镜。

我对PNG唯一有效的代码是:

   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();
}

上面的代码将压缩委托给iText,这可能是也可能不够。

0 个答案:

没有答案