How to reset table format after InsertHtml

Last post 02-13-2012, 2:36 PM by alexey.noskov. 8 replies.
Sort Posts: Previous Next
  •  02-07-2012, 3:12 AM 359671

    How to reset table format after InsertHtml Java

    Attachment: Present (inaccessible)
    I'm trying to build a document from HTML snippets alternating with tables (containing some meta info about the HTML snippets). When an HTML snippet contains a table, the table formatting of the builder is changed. How can I reset the builder to it's prior settings?

    Example code follows, resulting document attached.

        public void testTable() throws Exception {
            Document doc = new Document();
            DocumentBuilder builder = new DocumentBuilder(doc);
       
            insertTable(builder);
            builder.insertHtml("<p>Html Paragraph</p>");
            insertTable(builder);
            builder.insertHtml("<table><tr><td>Html Table</td></tr></table>");
            insertTable(builder);
           
            doc.save("c:/tmp/aspose/TestTable.doc");
        }

        private void insertTable(DocumentBuilder builder) {
            builder.insertParagraph();
            builder.startTable();
            builder.insertCell();
            builder.write("Inserted table by builder.");
            builder.endTable();
            builder.insertParagraph();
        }

     
  •  02-07-2012, 4:31 AM 359724 in reply to 359671

    Re: How to reset table format after InsertHtml

    Hi Jens,

    Thanks for your query. I have modified your insertTable function code. Please use following insertTable function and read this documentation link for your kind reference.

    public static void insertTable(DocumentBuilder builder) {

     

        builder.insertParagraph();

        Table table = builder.startTable();

        builder.insertCell();

        table.setPreferredWidth(PreferredWidth.fromPercent(100));

        table.setBorders(LineStyle.SINGLE, 1, Color.BLACK);

        builder.write("Inserted table by builder.");

        builder.endTable();

        builder.insertParagraph();

    }

     



    Best Regards,
    Tahir Manzoor
    Support Developer, Aspose Sialkot Team
    http://www.aspose.com
    Your File Format Experts
    Keep in touch! We're on Twitter and Facebook
     
  •  02-09-2012, 8:40 AM 360407 in reply to 359724

    Re: How to reset table format after InsertHtml

    Thanks, at first this seemed to help.
    Unfortunately it doesn't work when exporting the document to pdf. (Aspose.Words 11.0.0)

            doc.save("c:/tmp/aspose/TestTable.pdf");

     
  •  02-09-2012, 9:22 AM 360431 in reply to 359724

    Re: How to reset table format after InsertHtml

    Hi Jen,

    Please use following line of code before saving the pdf.

    doc.updateTableLayout();

    Best Regards,
    Tahir Manzoor
    Support Developer, Aspose Sialkot Team
    http://www.aspose.com
    Your File Format Experts
    Keep in touch! We're on Twitter and Facebook
     
  •  02-10-2012, 1:55 AM 360570 in reply to 360431

    Re: How to reset table format after InsertHtml

    Attachment: Present (inaccessible)
    Thank you Tahir,

    I meanwhile found that hint in many forum entries, but unfortunately it doesn't really help: the tables inserted by insertTable are OK afterwards, but the table inserted by insertHtml is unnecessary made smaller (see attached). In more complicted layouts many tables get corrupt afterwards or are unchanged when they should be changed. It seems that updateTableLayout() does some heuristic only (, but most probably this is all that can be done).

    Is it preferable to use the DocumentBuilder only to insert the HTML parts and construct the other tables directly into the DOM? Is it recommended to merge DocumentBuilder and DOM actions in general, and what must be considered then?
     
  •  02-10-2012, 3:30 AM 360602 in reply to 360570

    Re: How to reset table format after InsertHtml

    Attachment: Present (inaccessible)
    Hi Jen,

    In your shared code you did not mention the table's width and border. Please use the following line of code to insert table by using
    insertHtml method. Please find the generated output pdf file in attachment.

    builder.insertHtml("<table width='100%' border='1'><tr><td>Html Table</td></tr></table>");

    Best Regards,
    Tahir Manzoor
    Support Developer, Aspose Sialkot Team
    http://www.aspose.com
    Your File Format Experts
    Keep in touch! We're on Twitter and Facebook
     
  •  02-10-2012, 6:15 AM 360704 in reply to 360602

    Re: How to reset table format after InsertHtml

    Attachment: Present (inaccessible)
    Hi Tahir,

    the monster has arisen again when I tried to combine mergecells and html. The table is totally messed up in the word document even after inserting a simple html paragraph. The pdf is more friendly, but doesn't manage the merge after inserting an html table. Is there any documentation about how to reset the builder after inserting html?

        public void testTable() throws Exception {
            Document doc = new Document();
            DocumentBuilder builder = new DocumentBuilder(doc);
       
            insertMergeCellTable(builder);
            builder.insertHtml("<p>Html Paragraph</p>");
            insertMergeCellTable(builder);
            builder.insertHtml("<table><tr><td>Html Table</td></tr></table>");
            insertMergeCellTable(builder);
            doc.updateTableLayout();
            doc.save("c:/tmp/aspose/TestTable.doc");
            doc.save("c:/tmp/aspose/TestTable.pdf");
        }
       
        private void insertMergeCellTable(DocumentBuilder builder) {
            builder.startTable();
            builder.insertCell();
            builder.write("Text in one cell.");

            builder.insertCell();
            builder.write("Text in another cell.");
            builder.endRow();

            builder.insertCell();
            builder.getCellFormat().setHorizontalMerge(CellMerge.FIRST);
            builder.write("Text in merged cells.");

            builder.insertCell();
            // This cell is merged to the previous and should be empty.
            builder.getCellFormat().setHorizontalMerge(CellMerge.PREVIOUS);
            builder.endRow();
            Table table = builder.endTable();
            table.setPreferredWidth(PreferredWidth.fromPercent(100));
            table.setBorders(LineStyle.SINGLE, 1, Color.BLACK);
        }
    Regards
    Jens
     
  •  02-10-2012, 10:32 AM 360790 in reply to 360704

    Re: How to reset table format after InsertHtml

    Hi Jen,

    I have tested the scenario and have manged to reproduce the same issue at my end. I have logged this issue in our issue tracking system and you will be updated via this forum thread once this issue is resolved.

    We apology for your inconvenience.

    Best Regards,
    Tahir Manzoor
    Support Developer, Aspose Sialkot Team
    http://www.aspose.com
    Your File Format Experts
    Keep in touch! We're on Twitter and Facebook
     
  •  02-13-2012, 2:36 PM 361209 in reply to 360790

    Re: How to reset table format after InsertHtml

    Hi Jen,

     

    Thanks for your inquiry. UpdateTableLayout usually gives good result. But sometimes with complex tables it can give incorrect result, i.e. table layout can be broken. This is because algorithm of UpdateTableLayout  is not the same as MS Word uses to layout tables. That is why it is normally don recommended to call updateTableLayout when you save your output document to MS Word formats. Sometimes it is necessary to call this method when you render document.

     

    Best regards, 


    Alexey Noskov
    Developer/Technical Support
    Aspose Auckland Team
     
View as RSS news feed in XML