-1

i have a pdf which I need to down scale. The pdf is in A4 portrait mode, what I need is to shrink the content of the pdf to 5 % and put this into a new PDF also in size A4 and portrait mode. Its not an option to convert the pdf to images, scale them and put it back to a pdf. I am looking for a way to solve this in java. Is there a way to solve this with pdfbox or itext?

Al Phaba
  • 6,545
  • 12
  • 51
  • 83

2 Answers2

2

If you use iText 7, then this is an option:

public void manipulatePdf(String src, String dest) throws IOException {
    PdfDocument pdfDoc = new PdfDocument(new PdfReader(src), new PdfWriter(dest));
    int n = pdfDoc.getNumberOfPages();
    PdfPage page;
    for (int p = 1; p <= n; p++) {
        new PdfCanvas(pdfDoc.getPage(p).newContentStreamBefore(),
                new PdfResources(), pdfDoc).writeLiteral("\nq 0.05 0 0 0.05 0 0 cm\nq\n");
        new PdfCanvas(pdfDoc.getPage(p).newContentStreamAfter(),
                new PdfResources(), pdfDoc).writeLiteral("\nQ\nQ\n");
    }
    pdfDoc.close();
}

Note that 5% (the 0.05 values in the writeLiteral() method) is really small. If there's text, it will be very hard to read what it says. Maybe you meant 95%. In that case use: writeLiteral("\nq 0.95 0 0 0.95 0 0 cm\nq\n").

Source: How to shrink pages in an existing PDF?

Note: iText 5 is being discontinued, but the iText 5 answer was already posted on Stack Overflow in 2014: Shrink PDF pages with rotation using Rectangle in existing PDF

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • Thanks for the answer, unfortunately, I can not afford this version, probably you will not show a solution for the older version 2.1.7? – Al Phaba Apr 05 '18 at 12:38
  • When you say *I can not afford this version*, what do you mean? If you avoid creating closed source software and obey the AGPL, then you can use iText under the AGPL. If you are working for an employer, *you* don't have to pay for a license, that's up to your employer. If you are self-employed, consider making money by creating an add-on on the [iText 7 platform](https://itextpdf.com/itext-developers-platform). – Bruno Lowagie Apr 05 '18 at 12:48
  • By the way: you can no longer use iText 2.1.7 in a commercial context either: see the [FAQ](https://developers.itextpdf.com/question/versions-older-than-5). – Bruno Lowagie Apr 05 '18 at 12:49
  • @AlPhaba if you are restricted to ancient versions only of the libraries you mention, you should have indicated that in your question from the start. – mkl Apr 05 '18 at 13:52
2

Answer for PDFBox 2.0.*:

try (PDDocument doc = PDDocument.load(new File("...")))
{
    for (int p = 0; p < doc.getNumberOfPages(); ++p)
    {
        PDPage page = doc.getPage(p);
        try (PDPageContentStream cs = new PDPageContentStream(doc, page, AppendMode.PREPEND, true))
        {
            cs.saveGraphicsState();
            cs.transform(Matrix.getScaleInstance(0.05f, 0.05f));
            cs.saveGraphicsState();
        }
        try (PDPageContentStream cs = new PDPageContentStream(doc, page, AppendMode.APPEND, true))
        {
            cs.restoreGraphicsState();
            cs.restoreGraphicsState();
        }
    }
    doc.save(new File("...."));
}

You'll see something tiny in the bottom left.

Tilman Hausherr
  • 17,731
  • 7
  • 58
  • 97