itext - Reduce Pdf File size using java - Stack Overflow

I am trying to create a pdf file using PDDocument but output pdf size is more than 15 mb. i want to red

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?

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?

Share edited Mar 4 at 15:24 Preeti Joshi asked Mar 4 at 6:33 Preeti JoshiPreeti Joshi 114 bronze badges 3
  • 1 Your first piece of code appears to use PDFBox. Which PDFBox version do you use? Effective compression (using object streams) is only possible in newer versions. That been said, how many pages does your template have? And how large is it and the signature image? If you add 14 1MB image files to a single 1MB PDF file, you are likely to end up with a 15MB file... – mkl Commented Mar 4 at 11:34
  • 1 Post a link to output PDF file so we can see why it is so large. Then we can offer advice on how its size can be reduced (if it can be reduced). – iPDFdev Commented Mar 5 at 6:38
  • I am using PDFBox version 2.0.32. I am trying to merge multiple pdf files (count is dynamic)...if I make pdf file manually by merging 10 pdf files size is around 8 mb , same file i am trying to generate using java , it goes around 19 mb – Preeti Joshi Commented Mar 5 at 6:40
Add a comment  | 

1 Answer 1

Reset to default 2

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745059051a4608850.html

相关推荐

  • itext - Reduce Pdf File size using java - Stack Overflow

    I am trying to create a pdf file using PDDocument but output pdf size is more than 15 mb. i want to red

    6小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信