在 contentStream 之前绘制时表格消失 - PDFBox with Boxable [英] Table disappears when drawn before contentStream - PDFBox with Boxable

查看:58
本文介绍了在 contentStream 之前绘制时表格消失 - PDFBox with Boxable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 PDFBox 和 Boxable 的新手,希望有人能帮我解决这个问题!这个问题是参考这里提出的问题(参考:https://github.com/dhorions/boxable/issues/89 )在此,flurinBoonea 提供了一个小示例代码,将文本、图像和表格都放在同一页面中.我的问题是,如果我想创建一个表格(它具有基于内部内容的动态高度),然后我需要在表格后面放一些文本.我怎么能做到这一点?!?我在某处读到在绘制表格时我使用类似的东西来获取下一个元素的 YPosition,

I am new to PDFBox and Boxable and I'm hoping if someone could help me with this! This question is in reference to a question asked here (Ref: https://github.com/dhorions/boxable/issues/89 ) In this, flurinBoonea presented a small sample code to put Text, Image and Table all in the same page. My question is, if I want to create a Table (which has dynamic height based on the content inside) and then I need to put some text after the table. How am I able to do that ?!? Somewhere I read that while drawing the table I use something similar to get the YPosition for next element,

float yPosition = table.draw()

然后将此位置用于下一个元素,但是每当我在以下代码之前使用 table.draw 时,

And then use this position for the next element but whenever I use table.draw before the following piece of code,

PDPageContentStream contentStream = new PDPageContentStream(doc, page);
contentStream.beginText();
contentStream.setFont(font, 18);
contentStream.moveTextPositionByAmount(0, yPosition - 20);
contentStream.drawString("This is a test message");
contentStream.endText();
contentStream.close();

表格消失,只显示文本.不知道如何解决这个问题.有人可以帮我解决这个问题.我有点被这个问题困住了很长一段时间.提前致谢

The table disappears and only the text is displayed. Not sure how to work around this. Can someone please help me with this. I'm sort of stuck with this problem for quite a while now. Thank you in advance

推荐答案

您可以使用

PDPageContentStream contentStream = new PDPageContentStream(doc, page);

此构造函数记录为:

/**
 * Create a new PDPage content stream. This constructor overwrites all existing content streams
 * of this page.
 *
 * @param document The document the page is part of.
 * @param sourcePage The page to write the contents to.
 * @throws IOException If there is an error writing to the page contents.
 */
public PDPageContentStream(PDDocument document, PDPage sourcePage) throws IOException

因此,在创建此内容流时,您会丢弃该页面上的所有内容.

When creating this content stream, therefore, you throw away all content you had on that page.

每当您想添加到现有内容时,请使用不同的构造函数,例如

Whenever you want to add to the existing content, use a different constructor, e.g.

PDPageContentStream contentStream = new PDPageContentStream(doc, page, AppendMode.APPEND, true, true);

记录为

/**
 * Create a new PDPage content stream.
 *
 * @param document The document the page is part of.
 * @param sourcePage The page to write the contents to.
 * @param appendContent Indicates whether content will be overwritten, appended or prepended.
 * @param compress Tell if the content stream should compress the page contents.
 * @param resetContext Tell if the graphic context should be reset. This is only relevant when
 * the appendContent parameter is set to {@link AppendMode#APPEND}. You should use this when
 * appending to an existing stream, because the existing stream may have changed graphic
 * properties (e.g. scaling, rotation).
 * @throws IOException If there is an error writing to the page contents.
 */
public PDPageContentStream(PDDocument document, PDPage sourcePage, AppendMode appendContent,
                           boolean compress, boolean resetContext) throws IOException

<小时>

顺便说一句:您提到您对 PDFBox 和 Boxable 不熟悉,所以我假设您使用的是当前版本,尤其是 PDFBox 2.0.x.如果由于某种原因您选择使用旧版本(例如 1.8.x),则需要


As an aside: You mentioned that you are new to PDFBox and Boxable, so I assumed you use a current version, in particular a PDFBox 2.0.x. If for some reason you chose to use an old version (e.g. 1.8.x), you need

PDPageContentStream contentStream = new PDPageContentStream(doc, page, true, true, true);

相反.

这篇关于在 contentStream 之前绘制时表格消失 - PDFBox with Boxable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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