如何将PDF页面的内容旋转到任意角度? [英] How do I rotate the contents of a PDF page to an arbitrary angle?

查看:1422
本文介绍了如何将PDF页面的内容旋转到任意角度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将PDF页面的内容旋转任意角度,并且PDPage.setRotation(int)命令限制为90度的倍数. 页面的内容是矢量和文本,以后我需要能够放大页面的内容,这意味着由于分辨率降低,我无法将页面转换为图像.

I need to rotate the contents of a PDF page by an arbitrary angle and the PDPage.setRotation(int) command is restricted to multiples of 90 degrees. The contents of the page are vector and text and I need to be able to zoom in on the contents later, which means that I cannot convert the page to an image because of the loss of resolution.

推荐答案

在注释中,已经表明可以绘制一些内容,例如现有的常规纵向或横向页面,以任意角度与新的常规纵向或横向页面,可以使用在此答案中 .

In comments it already has been indicated that to draw some content, e.g. an existing regular portrait or landscape page, at an arbitrary angle onto a new regular portrait or landscape page, one can use the mechanism presented in this answer.

此处显示的代码

  1. 需要PDFBox开发2.0.0-SNAPSHOT版本和
  2. 使用形式xobjects,在当前上下文中这是不必要的,

尽管如此,这是一种适用于当前常规发行版1.8.8的快速而肮脏的解决方案,无需引入表格xobjects.

though, here a quick&dirty solution working for the current regular release 1.8.8 without introducing form xobjects.

此方法

void transformPage(PDDocument document, PDPage page, AffineTransform at) throws IOException, COSVisitorException
{
    PDRectangle cropBox = page.findCropBox();
    float xOffset = (cropBox.getUpperRightX() + cropBox.getLowerLeftX()) / 2f;
    float yOffset = (cropBox.getUpperRightY() + cropBox.getLowerLeftY()) / 2f;
    AffineTransform transform = AffineTransform.getTranslateInstance(xOffset, yOffset);
    transform.concatenate(at);
    transform.concatenate(AffineTransform.getTranslateInstance(-xOffset, -yOffset));

    PDPageContentStream stream = new PDPageContentStream(document, page, true, false);
    stream.concatenate2CTM(transform);
    stream.close();

    COSBase contents = page.getCOSDictionary().getDictionaryObject(COSName.CONTENTS);
    if (contents instanceof COSStreamArray)
    {
        COSStreamArray contentsArray = (COSStreamArray) contents;
        COSArray newArray = new COSArray();
        newArray.add(contentsArray.get(contentsArray.getStreamCount() - 1));

        for (int i = 0; i < contentsArray.getStreamCount() - 1; i++)
        {
            newArray.add(contentsArray.get(i));
        }

        COSStreamArray newStreamArray = new COSStreamArray(newArray);
        page.getCOSDictionary().setItem(COSName.CONTENTS, newStreamArray);
    }
}

将给定的转换应用于给定的页面.为了简化使用案例(旋转PDF页面的内容),将变换封装在平移中,将坐标系的原点移动到页面中心以进行变换.

applies the given transformation to the given page. To make the use case at hands (to rotate the contents of a PDF page) easier, the transformation is enveloped in translations moving the origin of the coordinate system to the center of the page for the transformation.

可以像这样使用方法

try ( InputStream sourceStream = getClass().getResourceAsStream("13.pdf") )
{
    final PDDocument document = PDDocument.load(sourceStream);
    final AffineTransform transform = AffineTransform.getRotateInstance(Math.PI / 4);

    List<PDPage> pages = document.getDocumentCatalog().getAllPages();

    for (PDPage page: pages)
    {
        transformPage(document, page, transform);
    }

    document.save("13-transformedPages.pdf");
}

将文档页面逆时针旋转45°(PI/4,数学上为正旋转方向).

to rotate the pages of a document counterclockwise by 45° (PI/4, mathematically positive rotation direction).

这篇关于如何将PDF页面的内容旋转到任意角度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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