Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
137 views
in Technique[技术] by (71.8m points)

java - How to down scale content of a pdf?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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("
q 0.05 0 0 0.05 0 0 cm
q
");
        new PdfCanvas(pdfDoc.getPage(p).newContentStreamAfter(),
                new PdfResources(), pdfDoc).writeLiteral("
Q
Q
");
    }
    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(" q 0.95 0 0 0.95 0 0 cm q ").

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...