12
Document document = new Document(reader.getPageSizeWithRotation(1));
PdfCopy writer = new PdfCopy(document, new FileOutputStream(outFile));
document.open();
PdfImportedPage page = writer.getImportedPage(reader, ++i);
writer.setFullCompression();
writer.addPage(page);
document.close();
writer.close();

I am using iText to split and merger the PDF, I need your help to reduce (compress) the output PDF size programmatically. Please let me know the steps to achieve the same.

1

5 Answers 5

7

With writer.setFullCompression() you already compressed file as much as possible. With iText you can't do anything more.

Sign up to request clarification or add additional context in comments.

3 Comments

oh thanks partlov.. so u suggest me to go with some other techniques to achieve the same.
This is not true in general. You can always extract raster images from the file, reduce their resolution, and put them back on the file.
You can also change the compression type for lossless-compressed images and use a lossy algorithm instead.
7

use iText

PdfReader reader = new PdfReader(new FileInputStream("input.pdf"));
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream("output.pdf"));
int total = reader.getNumberOfPages() + 1;
for ( int i=1; i<total; i++) {
   reader.setPageContent(i + 1, reader.getPageContent(i + 1));
}
stamper.setFullCompression();
stamper.close();

1 Comment

+1 for the reordering. A similar case: osdir.com/ml/java.lib.itext.general/2007-09/msg00128.html
4

Also change the PdfCopy to PdfSmartCopy. It will eliminate duplicate streams which have the same hash (md5).

1 Comment

i used this iText pdf compression but its not working
0

You can use ghostscript, invoking the exe with specific parameters for print your pdf with the ghostscript's pdfwriter (example: sDEVICE=pdfwrite -sOutputFile=myfile.pdf). There are several accepted parameters, for compression or quality levels, etc. It may result and optimized and smaller file.

Comments

0

Multiple Bitmap Image to pdf converter --> Compressed Pdf

public static String  createPDFWithMultipleImage(Bitmap[] bitmaps, String pdf_name){
    String directoryPath = Environment.getExternalStorageDirectory() + "/OpPath/";

    File file = new File(directoryPath,pdf_name);
    try {
        FileOutputStream fileOutputStream = new FileOutputStream(file);
        PdfDocument pdfDocument = new PdfDocument();
        for (int i = 0; i < bitmaps.length; i++){
            Bitmap original = bitmaps[i];
            int nh = (int) ( original.getHeight() * (512.0 / original.getWidth()) );
            Bitmap bitmap = Bitmap.createScaledBitmap(original, 512, nh, true);

            PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(bitmap.getWidth(), bitmap.getHeight(), (i + 1)).create();
            PdfDocument.Page page = pdfDocument.startPage(pageInfo);
            Canvas canvas = page.getCanvas();
            Paint paint = new Paint();
            paint.setColor(Color.BLUE);
            canvas.drawPaint(paint);
            canvas.drawBitmap(bitmap, 0f, 0f, null);
            pdfDocument.finishPage(page);
            bitmap.recycle();
        }
        pdfDocument.writeTo(fileOutputStream);
        pdfDocument.close();
        return file.toString();

    } catch (IOException e) {
        e.printStackTrace();
        return file.toString();
    }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.