phax / ph-pdf-layout

Java library for creating fluid page layouts with Apache PDFBox. Supporting multi-page tables, different page layouts etc.
Apache License 2.0
64 stars 11 forks source link

Integrating PDFBox and XChart with ph-pdf-layout #17

Closed mailidpankaj closed 4 years ago

mailidpankaj commented 4 years ago

Hi,

I am trying to integrate two tables created in ph-pdf-layout with PDFBox. I am not able to add it to PDFBox PDDocument(main document where we add all elements like content, tables, etc).

How can we integrate -

1) PDFs created using PDFBox(with their elements like tables, images) with ph-pdf-layout and/or 2) PDF created using ph-pdf-layout(with their elements like tables, images) with PDFBox

I have created a Donut chart using XChart library, which can be integrated with PDFBox, but finally, when I try to integrate with ph-pdf-layout, I have the same problem of not being able to integrate.

Hence, I need help in integrating the PDFBox and XChart with ph-pdf-layout or vice-versa. Thanks.

I have attached the source file for your reference. Thanks.

PhaxPDFBox.DOCX

Regards, Pankaj

phax commented 4 years ago

Hi,

Ad 1.: Class PageLayoutPDF contains a member variable of type IPDDocumentCustomizer that you can set. It is called at the end of creation and you could add additional stuff.

Ad 2.: This is currently not foreseen. What I could do is, to add a method PageLayoutPDF.applyToPDDocument (PDDocument) that would ignore the document properties etc. and just add to the document. But especially with Fonts etc. this can become tricky and result in bogus PDFs...

Can you eventuallly convert the XChart image to a BufferedImage than you can than use with PLImage to insert?

mailidpankaj commented 4 years ago

Hi,

Regarding Ad 1.: "Class PageLayoutPDF contains a member variable of type IPDDocumentCustomizer that you can set. It is called at the end of creation and you could add additional stuff."

Do I have to set IPDDocumentCustomizer to PDFBox PDDocument ? Means, when PageLayoutPDF is finally set with all elements like tables, images, etc, in the end, then I have to set it to PDFBox PDDocument or any other elements of PDFBox like PDPageContentStream content ? I did not get this part. Any code example will be helpful.

Regarding Ad 2.: "This is currently not foreseen."

Means PDFBox can't be integrated when PDF is created(PDPageContentStream is closed and PDDocument is saved) with ph-pdf-layout? You mean this is not supported currently.

Regards, Pankaj

phax commented 4 years ago

Hi Pankaj, in your example code, you already have a BufferedImage. Why don't you use PLImage to place the image at the correct place?

Modifying a PDDocument with the content of ph-pdf-layout (one way or the other) will most likely just complicate things. PDF is a tricky file format and you should adhere to the simplest way forward.

The original intention of IPDDocumentCustomizer is to add watermarks (like DRAFT) or printing information on each page. But it is not meant to change what was previously rendered. I really ask you to look at the examples - you should be able to do all the things you mentioned with the current library version "as is".

hth, Philip

mailidpankaj commented 4 years ago

Hi Philip,

"Why don't you use PLImage to place the image at the correct place?" The chart is in BufferedImage already, I will take care of this to place the chart at appropriate place. Thanks.

"Modifying a PDDocument with the content of ph-pdf-layout (one way or the other) will most likely just complicate things. PDF is a tricky file format and you should adhere to the simplest way forward."

Do you mean we should either use PDFBox or ph-pdf-layout(not both) to create the PDF(with tables, images, etc), as converting from one to another will mess things up? Do I correctly understand that we should only use one library(any of two) to create PDF, but not try to make use of capabilities from both library to create a final PDF?

Regards, Pankaj

phax commented 4 years ago

Yes correctly. ph-pdf-layout uses PDFBox under the hood. It is a "layout" engine where you provide the rules (padding, margin, alignment, border etc.) and it renders it to where you place it. Once you rendered the document, it is very hard to manually insert new objects at the right place, because positioning happens dynamically.

If the image is at the wrong place:

hth, Philip

phax commented 4 years ago

Hi Pankaj, did you manage to integrate the image? Can I close this issue? Thanks, Philip