iText - 在创建PDF时旋转页面内容 [英] iText - Rotate page content while creating PDF

查看:600
本文介绍了iText - 在创建PDF时旋转页面内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想制作一个包含横向页面的PDF。虽然可以使用以下方法将页面大小设置为横向:

I would like to produce a PDF that has pages in landscape. While it is possible to set the size of the page to landscape using:

document.setPageSize(PageSize.LETTER.rotate());

这实现了我想要的效果,因为我添加的任何内容仍然是左 - >右我希望它是底部 - >顶部。

this does not achieve what I want because any content that I add is still oriented left->right while I would like it to be bottom->top.

IE这就是我得到的:

I.E. this is what I am getting:

当我想要的是:

我已经能够实现所需的输出通过使用iText创建并打开PDF后打开PDF,但我想要一个解决方案,让我在添加内容后立即用iText旋转它。

I have been able to achieve the desired output by opening the PDF after it has been created and rotating it using iText, but I would like a solution that lets me rotate it immediately with iText after adding content to it.

推荐答案

优秀的问题。如果我能够两次投票,我会!

Excellent question. If I was able to upvote it twice, I would!

你可以用 PdfPageEvent 来达到你想要的效果:

You can achieve what you want with a PdfPageEvent:

public class RotateEvent extends PdfPageEventHelper {
    public void onStartPage(PdfWriter writer, Document document) {
        writer.addPageDictEntry(PdfName.ROTATE, PdfPage.SEASCAPE);
    }
}

你应该使用这个 RotateEvent 在您定义编写器之后:

You should use this RotateEvent right after you've defined the writer:

    PdfWriter writer = PdfWriter.getInstance(document, os);
    writer.setPageEvent(new RotateEvent());

请注意,我使用 SEASCAPE 来获取图像中显示的方向。如果您希望页面朝向另一个方向,也可以使用 LANDSCAPE

Note that I used SEASCAPE to get the orientation shown in your image. You can also use LANDSCAPE if you want the page to be oriented in the other direction.

我需要记住这个问题一旦我开始写第三版iText in Action。这是何时使用 onStartPage()事件的一个很好的例子。

I need to remember this question once I start writing a third edition of "iText in Action". It's a nice example of when to use the onStartPage() event.

这篇关于iText - 在创建PDF时旋转页面内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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