Table disappears when drawn before contentStream - PDFBox with Boxable

谁说我不能喝 提交于 2019-11-29 17:39:03

You create an additional content stream for the page in question using

PDPageContentStream contentStream = new PDPageContentStream(doc, page);

This constructor is documented as:

/**
 * 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);

which is documented as

/**
 * 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

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);

instead.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!