【问题标题】:Table disappears when drawn before contentStream - PDFBox with Boxable在 contentStream 之前绘制表格时表格消失 - 带有 Boxable 的 PDFBox
【发布时间】:2018-02-15 23:13:28
【问题描述】:

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

float yPosition = table.draw()

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

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

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

【问题讨论】:

    标签: java pdfbox boxable


    【解决方案1】:

    您使用

    为相关页面创建额外的内容流
    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
    

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

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

    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),则需要

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

    改为。

    【讨论】:

      猜你喜欢
      • 2017-09-08
      • 2021-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-31
      • 2017-09-12
      相关资源
      最近更新 更多