javaitextpdfptable

Can you determine column-width in iText based on input (e.g. a string)?


I am working on a project using iText and Java. In the project, I will be making a pdf. In this pdf, there will be multiple tables and they will have changing column counts and content, which means the column-widths have to be determined in a flexible way too.

A lot works well so far, but I am stuck on the whole column-width issue. My current attempt is to determine them based on a database-entry. So let's say I have 2 columns, and I want one to be 20 % of the table size and the other 80 %. Then if I did it manually, I would just have to write the following code:

table.setWidths(new float[] { 20, 80 });

or

table.setWidths(new int[] { 20, 80 });

An example of my failed attempts is when I use this data-entry:

20, 80

And then I attempt to make the entry into a float or int through parsing it as a string to a float, but of course it gives me a parsing error.

Does anyone have a fix for my current problem or got an idea for how to approach a new solution?


Solution

  • What was needed was to split the string into an int[], like so:

    String inputFromDatabase = "1,5";
    String[] p = inputFromDatabase.trim().split(",");
    int[] ans = new int[p.length];
    for (int a = 0; a < p.length; a++) {
        ans[a] = Integer.parseInt(p[a]);
    }
    table.setWidths(ans);
    

    The answer was found here