How to down scale content of a pdf?

前端 未结 2 410
傲寒
傲寒 2020-12-04 04:29

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 a

2条回答
  •  孤城傲影
    2020-12-04 05:01

    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

提交回复
热议问题