iText PDF template dynamic row insertion on tables - java

I have a requirement where I need to generate a PDF document in Java using iText.
The ask is to use a PDF template, and inject the data values in a user designed PDF template to produce the desired output PDF.
The documentation on the iText site is not clear/or I didnt search them completely, so asking for support or article regarding this,
Our use case is to generate a table from a PDF template, where the number of rows is determined by the data (i.e. in here JSON). I should be able to dynamically scale the number of rows in a PDF template based on the data size.
Is this possible?
I was referring to this article:
https://github.com/koendehaen/itext_summit_pdf_templates
In the above GITHUB example, the rows were prepopulated in the PDF template, I want something like the below screenshot, similar to how Handlebars provide, where a for loop on a hbs template, and I can inject data.
Example:
Suppose I have a table like below
But here the row count is dynamic based on data set, how to scale the table dynamically
Note:
I cant create the template in HTML, like handlebars, etc...
Since business people with little to no technical background will be the users who will be generating the PDF template.

You can search the all key values of your JSON, and before insert this value in PdFTable. In my case, i get the lastCellNum:
Row row = rowIterator.next();
PdfPTable table = new PdfPTable(row.getLastCellNum());
Iterator<Cell> cellRow = row.iterator();
while(cellRow.hasNext()) {
Cell cell = cellRow.next();

Related

canShrink property on LabelHandle is not working with PDF and EXCEL Report using BIRT

I am working with BIRT(4.6) and Eclipse mars2(4.5).
I am creating a report using EngineConfig object and creating the report in HTML, PDF and EXCEL formats.
The number of the tables and data inside the table I am filling dynamically.
I am creating the CellHandle for each column in my report and adding the LabelHandle inside it. I was setting the width of a LabelHandle based on the size of the String that I am going to set it and All three reports(PDF, HTML and EXCEL) were displaying properly. But setting the LabelHandle width based string size, is not a good practice and sometimes data is missing in the reports.
So Tried with 'canShrink' property on LabelHandle as shown below.
labelHandleObj.setProperty("canShrink", "True");
The above statement automatically adjust LableHandle width based on the String size and this avoids me to set the LableHandle size manually based string size.
With the canShrink property my HTML report is generating properly with no data loss and No extract space/padding in cell. This is the way I want the report.
But when it comes to PDF and EXCEL, Tables are not displaying properly and all columns are shirked and data is not displaying properly. You can see the PDF Report by clicking the below link.
PDF Report after setting the canShrink property on LableHandle
Can any one kindly suggest me, how to get the data properly in PDF and Excel Report. But I want to use the CanShrink property, because it is creating the proper HTML report.I tried with both True and False value for CanShrink but no use with PDF and EXCEL.
Is there any way to get the three reports properly without using CanShrink.
Thanks in Advance.
When something like this happens you should try copy your table and insert it under original table, then hide original table from PDF using the visibility tab, then hide the copy from everything but PDF. I've done this a lot of times and it really solves the problem, but major disadvantage of this is that if you want to change something you have to do in two tables. Example report:

How to read the table styles using apache POI from powerpoint

I am trying to read the table styles that is in a pptx template.
Tried using XSLFTableStyle xslfTableStyle = xslfTableStyles.getStyles() but that returns empty list.
I am able to see the file which has the table styles in the pptx package in the location ppt/tableStyles.xml which has the styles for all the available tables and also the selected style for this template.
Even if I write some custom code to read this xml, I don't see a way to create a table in a slide using this style. XSLFTable tbl = tableSlide.createTable(); this does not take any style inputs
Any out of the box way to create a table using the available table style?
Think this is not yet developed by Apache POI team, should be in their roadmap
Thanks,

Updating existing Image and Table fields

I have a pdf built with LiveCycle Designer, I am easily able to update all TextFields using iText 5.4.3. My issue is how do I update the existing ImageField and access a Table object?
I have obtained the position of the image field and am able to add the image onto the form, then remove the image field.
Rectangle rect = acroFields.getFieldPositions("TopmostSubform[0].Page5[0].processFlowImage[0]")
.get(0).position;
String realPath = FacesContext.getCurrentInstance()
.getExternalContext().getRealPath("/");
Image img = Image.getInstance(realPath + wsSelected.getImage());
// use scalePercent as images are process flow diagrams and image
// ratios are not consistent and making them absolute causes distortion
img.scalePercent((float) 47.0);
img.setAbsolutePosition(rect.getLeft(), rect.getBottom());
stamper.getOverContent(5).addImage(img);
While this works, I would think there would, or should, be the ability to get the image field and populate with an InputStream or file.
Another issue is when working with Pre-defined tables on the form. I can get the field names and update the appropriate cells.
However, I have not been able to find any examples of obtaining the table, as a table object, and adding new rows. I can obtain the location, create a new PdfPTable and stamp it onto the form with the data. This will not work. The table will have a dynamic number of rows, based on db data. It is in a flowable layout, between pre-defined text and headers. The form (template) is pre-built and provided as a template. The form is populated with DB values and some user input from a JSF application, and presented back to the user as a finished interface agreement.
My thought is, if I can't access the table object and it's attributes (rows, columns, cells) I would need to read the form, effectively line by line, create and insert the dynamic tables to the output stream when and where they need to be, then continue reading the form and stamping the end result to a new form.

Droplist in Excel using Poi

How can I create a drop list in excel like drop list in html with the inbuilt "value" like attribute?
My Requirement is: I want to show the description which is not stored in database, but the code for description.
Ex: I have a subject list in an excel cell, for Science the description is "Science", but I want to store the code "SCI" in the Database.
You could use an ActiveX ComboBox in a worksheet, available from the Developers tab.
Enter both columns of data into the worksheet - you can hide the columns. Then set properties of the combobox:
ColumnCount 2
BoundColumn 1
ColumnWidths 0 pt; 20 pt
Set the ListFillRange and LinkedCell.
I understand there used to be issues with distributing workbooks containing ActiveX controls. I am not sure if this is still an issue, particularly when used a common (standard) control.
Of course, Excel is not designed to be a front-end to a database, so you'll need to write all the code to keep everything in-sync.
You could use the simpler Form Control/ComboBox. This will only store the index number in a cell - it does not have any events that you can use. You could use a formula based on the linked cell, that stores the description in another cell. When the user (presumably) clicks a button to submit the data, you would retrieve and store the description from this cell.
Hmm I can see many placeholder occurs all around the web related to java poi... but it seems there is no solution for your question. The only way I found to manage this is to set the cell value with my default text - the first value!

JTable Cell Data

I am facing a problem with JTable cell data in Java (Swing based application),
In each cell of JTable if I entered any data it is automatically appending to old data,
instead of that I want new data every time in that cell is just like Excel sheet Cell
(For example in Excel sheet when ever we want enter some data in a cell, at that moment old data of the cell is eraised and it allow new data ) my problem is simillar to that only
Advanced Thanks
From kleopatra's link, there is another link: http://tips4java.wordpress.com/2008/10/20/table-select-all-editor/
The code in there will select the content of the cell, you can modify the code to delete the contents instead of select.

Categories