如何缩小PDF格式的内容? [英] How to down scale content of a pdf?
问题描述
我有一个pdf,我需要缩小规模。 pdf处于A4纵向模式,我需要的是将pdf的内容缩小到5%,并将其放入A4 A4和纵向模式的新PDF中。它不是将pdf转换为图像,缩放它们并将其放回pdf的选项。我正在寻找一种方法来解决这个问题。有没有办法用pdfbox或itext解决这个问题?
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?
推荐答案
如果您使用 iText 7 ,这是一个选项:
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();
}
请注意5%( 0.05
writeLiteral()
方法中的值非常小。如果有文字,将很难阅读它所说的内容。也许你的意思是95%。在这种情况下,使用: writeLiteral(\ nq 0.95 0 0 0.95 0 0 cm\\\
。
q\\\
)
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")
.
注意:iText 5已停产,但iText 5答案已经确定发布于2014年的Stack Overflow:收缩轮换PDF页面在现有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
这篇关于如何缩小PDF格式的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!