1

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

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

rac3b3nn0n
  • 861
  • 2
  • 12
  • 26

1 Answers1

2

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.

mkl
  • 90,588
  • 15
  • 125
  • 265