Preeti Joshi
Preeti Joshi

Reputation: 11

Reduce Pdf File size using java

I am trying to create a pdf file using PDDocument but output pdf size is more than 15 mb. i want to reduce so that max pdf size is max 10 mb

Creating pdf file : (templateList is list of all PDDocuments)

for (PDDocument template : templateList) {
    for (PDPage page : template.getPages()) {
        if (status != 0) {

            PDImageXObject pdImage = PDImageXObject.createFromByteArray(template, signImg,
                    "sign.png");

            PDPageContentStream contents = new PDPageContentStream(template, page, true, true);
            AffineTransform at = new AffineTransform(pdImage.getHeight() * 1.5, 0, 0,
                    pdImage.getWidth() * 1.5, 4000, 4300);
            at.rotate(Math.toRadians(90));
            contents.drawXObject(pdImage, at);
            contents.close();
        }
        pdfTemplate.addPage(page);

    }
}
ByteArrayOutputStream outputStream1 = new ByteArrayOutputStream();
pdfTemplate.save(outputStream1);

this results in huge output file.

i have tried to reduce file size by using:

PdfReader reader;
try {
    System.out.println("Reading file");
    reader = new PdfReader(new FileInputStream(
            "input.pdf"));
    System.out.println("Reading file completed");
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream("output.pdf"));
    int total = reader.getNumberOfPages() + 1;
    System.out.println("Total pages :" + total);
    for (int i = 1; i < total; i++) {
        reader.setPageContent(i + 1, reader.getPageContent(i + 1));
    }
    stamper.setFullCompression();
    stamper.close();
    System.out.println("Compression Done");
} catch (IOException | DocumentException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

but this does not seem to be working for me as output file is also of same size as original

what can I do to achieve this?

Upvotes: 1

Views: 45

Answers (1)

Glenner003
Glenner003

Reputation: 1637

By executing this: PDImageXObject.createFromByteArray(template, signImg, "sign.png"); for every iteration, a new image is created for each new iteration.

Place the creation of the object before the loop and reuse it.

PDImageXObject pdImage = PDImageXObject.createFromByteArray(template, signImg, "sign.png");

// The transformation is always the same, so it can also be reused
AffineTransform at = new AffineTransform(
    pdImage.getHeight() * 1.5, 0, 0, pdImage.getWidth() * 1.5, 4000, 4300
);
at.rotate(Math.toRadians(90));

for (PDDocument template : templateList) {
    for (PDPage page : template.getPages()) {
        if (status != 0) {
            PDPageContentStream contents = new PDPageContentStream(template, page, true, true);
            contents.drawXObject(pdImage, at);
            contents.close();
        }
        pdfTemplate.addPage(page);
    }
}

ByteArrayOutputStream outputStream1 = new ByteArrayOutputStream();
pdfTemplate.save(outputStream1);

Upvotes: 2

Related Questions