How to make PdfPTable calculate column-width dynamically - java

I want to create a PdfPTable with columns containing Strings of different lengths. I learned that the width of each cell/column are the same for each column (default) or one can set the fraction of space every column shall occupy.
However, I want the columns to be as wide as required, but not any wider, depending on the data inserted. Assume that the table fits easily on the page (no breaks!). Of course, I can manually step through all my data and calculate the maximum-String-length of each column, setting the properties of the table accordingly, but I wondered whether such a common option is already provided by itext itself.

iText is not HTML, so it doesn't have options to automatically adjust width, instead is page oriented and so you've to decide your widths, either as percentage or absolute values.
I'm afraid that to do what you ask, you've to implement your own extension by measuring the widths, as you said. See how to calculate the string with with iText.

Related

Why is Sheet.setColumnWidth not in Points? Is there a workaround?

I'm curious why there is no implementation for Sheet.setColumnWidth that accepts a Points parameter.
There is a method to set the height of a Row in points, which is setHeightInPoints
The only available method to set the width is accepting a rather weird scale, defined as
width = Truncate([{Number of Visible Characters} * {Maximum Digit Width} + {5 pixel padding}]/{Maximum Digit Width}*256)/256
This means it depends on some width of a digit, so the value used in the parameter is
just 1/256 of what is wanted here, some straight representation of a width. In Excel itself
you set the width in Points, too.
This means behaviour of Row-height and Column-width is not symetric.
Is there any rational reason for only having that version of setColumnWidth?
This leads to serious problems, as the best I can get has a different result on every computer,
because the width which is set is depending on the users default font setting.
I believe it has something to do with displaying nicely on many different setups, as the comment of pnuts suggests. But it is only usable in a very narrow field.
At the moment I believe that there is no simple workaround for that, and I cannot find one right now. (just one that works for my case perhaps)
Is there any good way to calculate a column width value from a desired points value?
I.e. I want 120 points on any computer that is using the excel export functionality. What is the width value to use as parameter here to get the wanted points width?

Building and printing complex table layouts with Java

A customer requested me a software, and one of its requirements is build a form and fill it with data collected from database.
This form is currently being created in Excel. It uses cells to build the form, some cells have blank background, others blank background with black bottom border (to look like a line where text is typed), others have gray background with white text, and there's also a logo image. In Excel, some cells are merged to become bigger than other cells. They fill the text in another spreadsheet and the required cells in the form take that text and format it.
I've looked many report frameworks in Java, some are very complex and some look like Excel's graph builders, but I saw none that can make a complex 2D form like this.
Data filled in it is simple, like name, quantity, some numbers, but they have different length requiring for example that name's cell to be merged to cover a full horizontal line, and some have smaller font size. There's no repeated data that would require sorting and I have no problem gathering the data.
In the end, the filled form must also be printed, so I can't use normal Swing table or grid. It will be used in Windows now, but it'd be nice to support Linux printing too.
Any suggestion of a Java component that builds a 2D layout like this and fills it with strings will be very much appreciated. I even thought of taking a screenshot of their current form and just use 2D Graphics to print the text, but I'd not be able to print it.
This is an example of the kind of form I must build, it's somewhat like that but some areas have gray background with white text:
No, it's not a duplicate, but it is a good example of the layout.

Java image library - turn grid image into array

If I have an image of a table of boxes, with some coloured in, is there an image processing library that can help me turn this into an array?
Thanks
You can use a thresholding function to binarize the image into dark/light pixels so dark pixels are 0 and light ones are 1.
Then you would want to remove image artifacts using dilation and erosion functions to remove noise (all these are well defined on Wikipedia).
Finally if you know where the boxes are, you can just get the value in the center of each box to determine the array value, or possibly use an area near the center and take the prevailing value (i.e. more 0's is a filled in square, more 1's is and empty square).
If you are scanning these boxes and there is a lot of variation in the position of the boxes, you will have to perform some level of image registration using known points, or fiducials.
As far as what tools to use to do this, I'd recommend first trying this manually using a tool like ImageJ, which has a UI and can also be used programatically since it is written all in Java.
Other good libraries for this include OpenCV and the Java Advanced Imaging API.
Your results will definitely vary depending on the input images and how consistenly lit and positioned they are.
The best way to see how it will do for your data is to try applying these processing steps manually to see where your threshold value should be, how much dilating/eroding you need to get consistent results.

Get columns to stretch entire width of the table

I'm working in Java using the Vaadin framework.
I have a table with 14 columns. My problem is that that there's a small gap to the right of the last column, like the beginning of a new column that shouldn't be there..
The image shows the problem:
I've tried solving it with using column expand ratio on the last column, but this makes it abnormally big..
Do any of you know of a property you can use, CSS or Java, that makes the columns stretch over the entire width of the table without causing large column disproportion?
This is known issue, it was closed already Ticket #6677
If you set your table to fullsize that should probably close the "gap".
YourTable.setSizeFull();
I gues you ahve your table in a layout. Set this layout margin to false and also remove spacing.

Calculation of required display space for different subclasses of JComponent

For my current project i am writing a JTable based GUI. One of the main features is the ability to adjust the sizes of all cells at runtime, depending on the contents (which change over time). Currently all cells have the same height and width, when the application is started. I would like to change that to a more sophisticated approach. I was wondering if it would be somehow possible to determine the space needed by "the content" to be displayed properly. That is without to much empty space or cutting something of.
"The content" is a string for starters. It is loaded from a database and i can't make any assumptions whatsoever about it. It may be null. In this case there should be any kind of default size for the corresponding cell.
In the long run there will be all different kinds of content to be displayed, like pictures, video and so on.
I tried working with FontMetrics to calculate the length of the strings. But since i'm using JTextPanes to display them, i can't get it to work exactly. I think this has to do with JTextPanes automatic word wrapping because sometimes the lines aren't filled up. This screws up my calculations.
Well long story short: I need some kind of design guideline to achieve the feature descriped above. I'm sure one of you clever guys knows just how to do it.
Thanks in advance,
DeKay
Maybe the text pane size calculation in this How can I measure/calculate the size a Document needs to render itself? will help you out.
As you are using JTextPane for rendering, you may find this Q&A helpful.
The conversion textPane.modelToView() always comes out to null.
Note that modelToView "Returns: the coordinates as a rectangle ... or null if the component does not yet have a positive size."
I have still no idea, how to calculate the amount of space needed in general.
IIUC, the key to understanding #camickr's example is the use of setPreferredSize() to include the text pane's changed boundary, followed by validate() which "is used to cause a container to lay out its subcomponents again."
To set the height of a row in a JTable, look at here:
public void setRowHeight(int row, int rowHeight)
To set a column width, you have to look at the TableCOlumn API here:
public void setWidth(int width)
Hope this can help

Categories

Resources