简体   繁体   中英

How to reduce PDF file size programmatically in Java?

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.

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

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

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM