Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Thu Mar 09, 2023 6:39 pm

Hi!, I'm having some trouble with the code below, and I would really appreciate any help.

Currently, I am creating a Word document that has a table template in it. I use the ".deepClone()" function to make copies of the table and replace the template with different data to create several tables, the problem I’m having is the spacing between tables, they are either generated on top of each other or too far away when I use a paragraph with setBeforeSpacing() and setAfterSpacing() between the tables.
I tried generating temporary paragraphs with bookmarks beforehand and then putting the tables on them, but the tables are overlapping and I can’t find a way to get the previous table height to calculate how many empty paragraphs (spacing) I need to insert in order for the tables to not overlap.
My code is something like this:

Code: Select all
TextRange range = txtTemplateTable.getAsOneRange();
          //Get table template
          Table templateTable = (Table) range.getOwner().getOwner().getOwner().getOwner();
          
          generateParagraphsForEachObject(objects);
          
          for (Object o : objects) {
          
             Table table = templateTable.deepClone();
             Data data = getData(o);
             writeDataToDocument(table, data);
             
              // find the generated paragraph
              TextSelection txt = doc.findString("GenerateTableId"+o.getId(), true, true);
              Paragraph para = txt.getAsOneRange(true).getOwnerParagraph();
              Section section = (Section) para.getOwner().getOwner();
              //Insert Bookmark in paragraph
              para.appendBookmarkStart("Bookmark"+o.getId());
              para.appendBookmarkEnd("Bookmark"+o.getId());
   
              BookmarksNavigator bn = new BookmarksNavigator(doc);
              bn.moveToBookmark("Bookmark"+o.getId(), true, true);
              // insert new generated table
              bn.insertTable(table);
             
              // delete the pargraph
              int ix = section.getParagraphs().indexOf(para);
              section.getParagraphs().removeAt(ix);
          }
          // remove template table
          Section section = (Section) tableOriginal.getOwner().getOwner();
         section.getTables().remove(tableOriginal);

p.alv123
 
Posts: 1
Joined: Tue Mar 07, 2023 10:30 pm

Fri Mar 10, 2023 3:49 am

Hi,

Thanks for your inquiry.
Could you please provide us with the following messages to help us work out a solution for you? You can send them to us via email (support@e-iceblue.com) or attach them here. Thanks for you corporation.
1) your template and output document.
2) your full test code.
3) the effect you want.

Sincerely,
Triste
E-iceblue support team
User avatar

Triste.Dai
 
Posts: 1000
Joined: Tue Nov 15, 2022 3:59 am

Return to Spire.Doc