thuliobuarque / java2word

Automatically exported from code.google.com/p/java2word
0 stars 0 forks source link

Alternative for those who want to use paragraphs in table #82

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
Hi Leonardo,  first I want to congratulate you for your excellent work. I am 
studying your framework and would like to help you improve the  table element. 
For those who are having trouble using paragraph in table,  there's a tip:

public class MyTable extends Table {

    public void addTableEle(TableEle tableEle, Paragraph ... cols){

               if(cols != null && cols.length > 0){
            StringBuilder th = new StringBuilder("");
            ITableItemStrategy item = TableFactoryMethod.getInstance().getTableItem(tableEle);

            for (int i = 0; i < cols.length; i++) {         
                //### commented in order to render the cell regardless of null or empty string
                if(cols[i] != null){ 
                                   th.append("\n" + getMyMiddle().replace("{value}", cols[i].getContent()));
                } else {
                                   th.append("\n" + item.getMiddle().replace("{value}", "" )); 
                                }
            }

            if(!"".equals(th.toString())){
                th.insert(0, item.getTop());
                th.append(item.getBottom());
            }

            String finalResult = setUpRepeatTableHeaderOnEveryPage(th);
            txt.append(finalResult);//final result appended
        }   
    }

    private String getMyMiddle() {
        String td =
            "                <w:tc> "
            +"\n                    <w:tcPr> "
                        +"\n                        <w:tcW w:w=\"4258\" w:type=\"dxa\"/> "
            +"\n                    </w:tcPr> "
            +"\n                    {value} "
            +"\n                </w:tc> "               
            ;
        return td;
    }
}

usage:

        MyTable tbl = new MyTable();
        tbl.addTableEle(TableEle.TH, "Name", "Number of gols", "Country");
        tbl.setRepeatTableHeaderOnEveryPage();
        Paragraph p1 = (Paragraph) Paragraph.withPieces(ParagraphPiece.with("111.111,11").withStyle().bold().create()).withStyle().align(ParagraphStyle.Align.RIGHT).create() ;
        Paragraph p2 = (Paragraph) Paragraph.withPieces(ParagraphPiece.with("222.222,22").withStyle().bold().create()).withStyle().align(ParagraphStyle.Align.RIGHT).create();
        tbl.addTableEle(TableEle.TD, 
                p1, 
                p2,
                null
                );
        myDoc.addEle(tbl);

Original issue reported on code.google.com by fabiocor...@gmail.com on 27 Jan 2012 at 12:01

GoogleCodeExporter commented 8 years ago
thanks a lot for your contribution!
I am on holidays and can't look at this now properly. 

just a quick question: could you achieve the same if you use the current String 
method:

tbl.addTableEle(TableEle.TD, Paragraph.with("* Arthur 
Friedenreich").create().getContent(), "1329", "Brazil");

because the "getContent()" returns the XML string that represents the 
paragraph. You can do the same approach to add Images or other elements inside 
a Table.

HAve a think about it. Maybe I didn't document this properly but java2word is 
supposed to be like a Composite design pattern where you add elements inside 
other elements. 

If someone wants to add images inside a table, we would have to create another 
methods to accept Image as parameter. This is what we don't want...

Let me know what you think. 

Thanks for your code! I really appreciate. Java2word has been built with 
contribution from many people like you, keen to help and get involved.

cheers

Leonardo Correa

Original comment by leonardo...@gmail.com on 27 Jan 2012 at 4:18

GoogleCodeExporter commented 8 years ago
Thanks for replying so quickly, but I had already done this way and the result 
was not expected. The problem is that the result has more characters than 
necessary.

thanks again 

Fabio

Original comment by fabiocor...@gmail.com on 27 Jan 2012 at 11:47

GoogleCodeExporter commented 8 years ago
Take a look at: 
http://code.google.com/p/java2word/source/browse/trunk/java2word/src/main/java/w
ord/w2004/elements/Table.java

I added a overloaded method to receive a Paragraph as parameter.  
It will be deployed on a JAR version 3.1 today.

thanks
Leonardo 

Original comment by leonardo...@gmail.com on 2 Feb 2012 at 2:01

GoogleCodeExporter commented 8 years ago

Original comment by leonardo...@gmail.com on 2 Feb 2012 at 12:32