pdfbox-layout icon indicating copy to clipboard operation
pdfbox-layout copied to clipboard

Draw on existing PDDocument?

Open ronzeidman opened this issue 8 years ago • 5 comments

Hi, I'm trying to create a document that have a table in it: /-----------------------
| text text text |

text text text
------------------
text text text
-----------------------/

I'm using pdfbox-layout for the first text paragraph than https://github.com/dhorions/boxable for the table, when I want to use pdfbox-layout again I'm encountering an issue. I cannot use pdfbox on an existing pddocument, there is no api for setting one, just creating one. so if I do doc.getPDDocument() I receive a document without pages, adding a page manually and drawing the table and continuing adding paragraphs before the actual render gives me a table on the first page and all the text in the next pages. if I do doc.render() I can put the table on the same page as the first text but I can't draw after the table since rendering twice removes the table I drew on the previously rendered document...

My workaround was to render the text, then the table, then the text after the table end position, then render the table again on the PDDocument...

ronzeidman avatar Aug 23 '17 13:08 ronzeidman

Hello ronzeidman, I face a similar problem. Can you please show me the piece of code for adding text after inserting the table for the first time? Thanks and best regards Carsten

kernOTH avatar Feb 09 '18 14:02 kernOTH

Something like this:

Document doc = new Document(...);
float yPos = topMargin;
doc.add(paragraph1);
yPos += paragraph1.getHeight()
doc.add(paragraph2);
yPos += paragraph2.getHeight()
doc.add(paragraph3);
yPos += paragraph3.getHeight()
int pageNum = 0;
PDDocument noTableDoc = doc.render();
PDDocument tableDoc = null;

while (yPos > PDRectangle.A4.getHeight()) {
    yPos -= PDRectangle.A4.getHeight();
    pageNum++;
}
//The drawTable does returns "table.draw();" (new BaseTable(PDRectangle.A4.getHeight() - yPos,... ,... ,... ,... ,document.getPage(pageNum), ...))
float tableYPos = PDRectangle.A4.getHeight() - drawTable(noTableDoc, pageNum, yPos, boldFont, font, info);

int numberOfPages = noTableDoc.getNumberOfPages() - 1 - pageNum;
float newTopMargin = numberOfPages * PDRectangle.A4.getHeight() + tableYPos - yPos;

doc.add(...);
doc.add(...);
doc.add(...);
doc.add(...);
tableDoc = doc.render();
drawTable(tableDoc, pageNum, yPos, boldFont, font, info);

ronzeidman avatar Feb 11 '18 08:02 ronzeidman

Hi, I'm trying to create a document that have a table in it: /----------------------- | text text text |

text text text

text text text -----------------------/ I'm using pdfbox-layout for the first text paragraph than https://github.com/dhorions/boxable for the table, when I want to use pdfbox-layout again I'm encountering an issue. I cannot use pdfbox on an existing pddocument, there is no api for setting one, just creating one. so if I do doc.getPDDocument() I receive a document without pages, adding a page manually and drawing the table and continuing adding paragraphs before the actual render gives me a table on the first page and all the text in the next pages. if I do doc.render() I can put the table on the same page as the first text but I can't draw after the table since rendering twice removes the table I drew on the previously rendered document...

My workaround was to render the text, then the table, then the text after the table end position, then render the table again on the PDDocument...

What did you do to load the PDDocument content inside a Document of pdf-box? I want to add a paragraph of a Document inside a defined PDDocument but it just showed white page...

ashleynguci avatar Dec 11 '20 07:12 ashleynguci

Did anyone resolve this issue?

drevil2109 avatar May 16 '23 13:05 drevil2109

It would be awesome if it would be possible to get the original org.apache.pdfbox.pdmodel.PDDocument from the rst.pdfbox.layout.elements.Document class. Anyone combined ralfstuckert's lib with the boxable?

baszero avatar Jun 05 '23 13:06 baszero