如何缩小pdf的内容? [英] How to down scale content of a pdf?

查看:37
本文介绍了如何缩小pdf的内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需要缩小比例的pdf.pdf 处于 A4 纵向模式,我需要的是将 pdf 的内容缩小到 5%,并将其放入一个新的 PDF 中,同样采用 A4 大小和纵向模式.它不是将 pdf 转换为图像、缩放它们并将其放回 pdf 的选项.我正在寻找一种在java中解决这个问题的方法.有没有办法用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?

推荐答案

PDFBox 2.0.* 的答案:

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.

这篇关于如何缩小pdf的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆