每个iText文档有多个渲染器:更新了更多详细信息 [英] Multiple Renderers per iText document: Updated with more details

查看:75
本文介绍了每个iText文档有多个渲染器:更新了更多详细信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用iText7创建包含多个部分的文档.有些部分应按常规格式设置,但有些则应格式化为列.我可以使用ColumnDocumentRenderer对象将其正确格式化为列,但是当我这样做时,整个文档设置为使用列.有什么办法让iText即时交换要使用的渲染器?

I am using iText7 to create a document which has multiple sections. Some of the sections are to be formatted normally, but some are to be formatted into columns. I can get it to format properly into columns by using the ColumnDocumentRenderer object, but when I do so, the entire document is set to use columns. Is there any way to have iText swap which renderer to use on the fly?

当我尝试快速交换渲染器时,出现空指针异常(com.itextpdf.kernel.pdf.PdfDictionary.get(PdfDictionary.java:482)).

When I try to swap out renderers on the fly, I get a null pointer exception (com.itextpdf.kernel.pdf.PdfDictionary.get(PdfDictionary.java:482)).

PdfDocument pdf = new PdfDocument(new PdfWriter(targetFile));
Document document = new Document(pdf);
DocumentRenderer defRender = new DocumentRenderer(document);
document.setRenderer(defRender);
ColumnDocumentRenderer dictRender = getColumnRender();

while (<CONDITION>) {
    document.setRenderer(dictRender);
    document.add(new Paragraph("THIS IS NORMAL TEXT"));
    document.add(new Paragraph("THIS IS NORMAL TEXT"));
    document.add(new Paragraph("THIS IS NORMAL TEXT"));
    <...> 
    document.setRenderer(defRender);
    document.add(new Paragraph("THIS IS COLUMN TEXT"));
    document.add(new Paragraph("THIS IS COLUMN TEXT"));
    document.add(new Paragraph("THIS IS COLUMN TEXT"));
    <...>
}

将渲染器设置为dictRender之后,第一个document.add()语句在com.itextpdf.kernel.pdf.PdfDictionary.get(PdfDictionary.java:482)处引发空指针错误.

After I set the renderer to dictRender, the first document.add() statement throws a null pointer error at com.itextpdf.kernel.pdf.PdfDictionary.get(PdfDictionary.java:482).

我不想创建多个不同的PDF文件,但是我想这可能是我最终要做的事情.感谢您的帮助.

I don't want to have to create multiple different PDF files, but am thinking that might be what I end up having to do. Thanks for any help here.

推荐答案

之所以会发生异常,是因为默认情况下会尽快保存页面内容,从而刷新页面内容.

The exception occurs because page content is flushed as soon as it is possible by default to save memory.

为避免立即清除内容,有一个DocumentRendererColumnDocumentRenderer构造函数的参数.

To avoid immediate flushing of content, there is a parameter of DocumentRenderer and ColumnDocumentRenderer constructors.

因此,例如,要创建一个关闭立即冲洗功能的DocumentRenderer,则必须按以下方式创建它:

So, for instance, to create a DocumentRenderer with immediate flushing switched off, you would have to create it as follows:

DocumentRenderer defRender = new DocumentRenderer(document, false);

ColumnDocumentRenderer非常相似.

接下来,由于您已关闭自动刷新功能并自行更改了渲染器,因此在代码末尾,在document.close();之前,您将必须手动刷新渲染器:

Next, as you have switched off automatic flushing and change renderers by yourself, in the end of your code, just before document.close();, you would have to manually flush your renderers:

defRender.flush();
dictRender.flush();

现在出现了内容,但是由于内容重叠,结果看起来仍然很难看.这取决于开发人员,因为两个渲染器是独立的实例,并且它们相互独立维护currentArea.为了适当地处理此问题,您将必须使用刚刚完成处理的上一个渲染器的currentArea更新要切换到的渲染器的currentArea.您可以通过扩展提供的标准渲染器或调用renderer.getCurrentArea()并修改bBox来实现.

Now the content appears, though still the result may look ugly because of content overlapping. This is up to the developer to resolve, because two renderers are independent instances and they maintain currentArea independent of each other. To handle this appropriately, you would have to update currentArea of the renderer you are going to switch to with the currentArea of the previous renderer you have just finished working with. You can do that by extending the standard provided renderers, or calling renderer.getCurrentArea() and modifying the bBox.

这篇关于每个iText文档有多个渲染器:更新了更多详细信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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