Pledgie



if you think this library has been useful, feel free to give any help. Don't be shy.


Click here to lend your support to: Java2word, coding for fun! and make a donation at www.pledgie.com !

Friday, February 10, 2012

huge ms word documents with java2word

Someone a while ago asked if Java2word can generate huge MS Word documents. I guess the limit is on the MS Word itself and space on your hard-disk.

Take a look at the screenshot:

There are 187 pages on this document. I also opened on Word Viewer running on Linux Ubuntu via Wine.

This is the code that generates this document:


        IDocument myDoc = new Document2004();
        TableV2 tbl = new TableV2();
        
        tbl.addRow( TableRow.with("Table Header in all Pages", "Usefull for reports").withStyle().repeatTableHeaderOnEveryPage().create() );
        
        tbl.addRow( TableRow.with("Simple String cell", "Another simple String cell") ); 
        tbl.addRow( TableRow.with( TableCell.with(Paragraph.with("TableCell- Style to the whole cell, Par").create()), "Simple String" ).withStyle().bold().create() );
        tbl.addRow( TableRow.with("Style to the whole cell, Str", "String").withStyle().bold().create() );
        tbl.addRow( TableRow.with( TableCell.with(Paragraph.with("TableRowV2 with merge").create()).withStyle().gridSpan(2).create() ).withStyle().bold().create() );
        tbl.addRow( TableRow.with( TableCell.with(Paragraph.withPieces( ParagraphPiece.with("Paragraph with Style inside TableCell").withStyle().bold().fontSize("20").create() ).create()).withStyle().bgColor("00FFFF").create(), "String"  ));
        
        String img = Image.from_WEB_URL("http://www.google.com/images/logos/ps_logo2.png").setHeight("100").setWidth("300").create().getContent();
        tbl.addRow( TableRow.with("this google logo: ", "Image here: "+ img + " == image before") );
        
        for (int i = 0; i < 8000; i++) {
            tbl.addRow( TableRow.with("111 ", "222") );            
        }
        
        tbl.addRow( TableRow.with("LAST", "LAST") );            
                
        myDoc.addEle(tbl.getContent());

By the way this is the new Table V2 (coming soon, thanks Fabio Cordeiro) where you'll be able to apply style and do a lot more with it.

cheers

Leonardo Correa

3 comments:

  1. Dear Leonard,
    I am getting the following error when I try to run the above code. Am I missing something?
    Please guide me ASAP.
    I badly need this thing working.

    java.io.FileNotFoundException: build.properties (The system cannot find the file specified)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.(Unknown Source)
    at java.io.FileInputStream.(Unknown Source)
    at word.utils.TestUtils.createLocalDocument(TestUtils.java:45)
    at word.utils.TestUtils.createLocalDoc(TestUtils.java:30)
    at Demo_package.Java2WordTest.main(Java2WordTest.java:45)
    java.io.FileNotFoundException: null\Java2word_allInOne.doc (The system cannot find the path specified)
    at java.io.FileOutputStream.open(Native Method)
    at java.io.FileOutputStream.(Unknown Source)
    at java.io.FileOutputStream.(Unknown Source)
    at java.io.PrintWriter.(Unknown Source)
    at word.utils.TestUtils.createLocalDocument(TestUtils.java:58)
    at word.utils.TestUtils.createLocalDoc(TestUtils.java:30)
    at Demo_package.Java2WordTest.main(Java2WordTest.java:45)
    Exception in thread "main" java.lang.NullPointerException
    at word.utils.TestUtils.createLocalDocument(TestUtils.java:64)
    at word.utils.TestUtils.createLocalDoc(TestUtils.java:30)
    at Demo_package.Java2WordTest.main(Java2WordTest.java:45)

    Regards,
    Chai

    ReplyDelete
  2. you are trying to build the project yourself.
    create a new file called build.properties based on build.properties.EXAMPLE
    build should work now

    ReplyDelete