ralfstuckert / pdfbox-layout

MIT License
156 stars 74 forks source link

Possible to create a text-table? #55

Closed arnthom closed 6 years ago

arnthom commented 6 years ago

Hi Ralf!

I try to create a multipage pdf with lots of data, that go into a table. There ist no need to draw lines, just display the words and numbers in a defined structure.

Is it possible to use a few indentations in only one line? Does indentation work, when there is (for this cell of the table) no text to be displayed? I find, that when there is just column 4 used, the indentation for cell 2 and 3 seems to have no effect.

Here is a piece of code of my project (with enumerations of the data columns, strings s1 to s4 have no line feed "\n"):

            Enumeration e1 = ...
            Enumeration e2 = ...
            Enumeration e3 = ...
            Enumeration e4 = ...
            while (e1.hasMoreElements()) {
                String s1 = (String) e1.nextElement();
                String s2 = (String) e2.nextElement();
                String s3 = (String) e3.nextElement();
                String s4 = (String) e4.nextElement();

                Paragraph p = new Paragraph();
                p.addMarkup(s1, 12, basefont);
                p.add(new Indent(140, SpaceUnit.pt));
                p.addMarkup(s2, 12, basefont);
                p.add(new Indent(70, SpaceUnit.pt));
                p.addMarkup(s3, 12, basefont);
                p.add(new Indent(50, SpaceUnit.pt));
                p.addMarkup(s4, 12, basefont);
                document.add(p);
            }

How do I get this to work as I want? Any idea, any work in progress towards this direction?

Update:

just adding s1 and s2, works fine. When adding s3, the string s2 ist not visible anymore. When finally adding s4, all strings but s2 are visible and intended as I want. Maybe a bug?

Second update: multiple indents in one line should behave like tabstopps in a word document. Too bad, I cannot use "\t" in the text, \0009 could not be found in the font, whatever I tried.

I would really appreciate any reply, may it be Ralf, may it be a user with similiar problem and perhaps a solution.

Update2:

since I cannot comment (don't know why), I have to answer to Ralfs reply this way: Thank you for the feedback, Ralf! As I said, Indent seems have a bug, generally I can use a few indentations in just one line, but some text is missing.

final String[] texts = new String[7]; final Alignment[] aligns = { Alignment.Left, Alignment.Right, Alignment.Right, Alignment.Right, Alignment.Right, Alignment.Right, Alignment.Right, }; final float[] widths = { 140, 30, 140, 70, 70, 20, 20, };

texts[0] = "spalte 1"; texts[1] = "spalte 2"; texts[2] = "spalte 3"; texts[3] = "spalte 4"; texts[4] = "spalte 5"; texts[5] = "spalte 6"; texts[6] = "spalte 7";

switch (...) { case '1': texts[2] = "text2"; texts[3] = ""; addZeile(texts, widths, aligns); ...

protected void addZeile(String[] texts, float[] widths, Alignment[] alignments) throws IOException, PdfCException { if (texts.length != alignments.length || texts.length != widths.length) { throw new PdfCException("falsche Tabellendefinition"); } pArray = new Paragraph[texts.length]; ColumnLayout col = new ColumnLayout(texts.length + 1, 5); document.add(col); for (int i = 0; i < texts.length; i++) { pArray[i] = getParagraph(alignments[i], widths[i]); pArray[i].addMarkup(texts[i], 10, myStandardFont); document.add(pArray[i]); document.add(ColumnLayout.NEWCOLUMN); } doc.add(new VerticalLayout()); }

Now I try the column approach line by line, which seems to work better, but there is a problem with the column width. I try to set it by paragraph width, but not all columns have the width that I set. I suspect, the column width is identical for all colums, but the paragraph may write up to its width whether this in the column boundery or not. Am I right?

btw: when adding NEWCOLUMN after the last paragraph of the array, it works as a NEWPAGE element, so I had to remove the last one ...

ralfstuckert commented 6 years ago

Hi, I'am currently working on tables, but honestly I have to say that I don't have much time to spend on this project. Indent is implemented to work at the begin of a line only, so I don't think this will work. Sorry Ralf

arnthom commented 6 years ago

OK, I'll be watching for the update with tables, meanwhile the columnlayout workaround does work well ...