how to Create table in word doc using docx4j in specific bookmark without overwritting the word doc

StackOverflow https://stackoverflow.com/questions/18254279

  •  24-06-2022
  •  | 
  •  

Pregunta

I need to create a table at the location of particular bookmark. ie i need to find the bookmark and insert the table . how can i do this using docx4j Thanks in Advance


Sorry Jason, I am new to Stackoverflow so i couldnt write my problem clearly, here is my situation and problem.

I made changes in that code as you suggested and to my needs, and the code is here

//loop through the bookmarks
for (CTBookmark bm : rt.getStarts()) {

// do we have data for this one?
String bmname =bm.getName(); 
// find the right bookmark (in this case i have only one bookmark so check if it is    not null)
if (bmname!=null) {
String value = "some text for testing run";
//if (value==null) continue;

List<Object> theList = null;
//create bm list 
theList = ((ContentAccessor)(bm.getParent())).getContent();

// I set the range as 1 (I assume this start range is to say where the start the table creating)
int rangeStart = 1;



WordprocessingMLPackage wordPackage =   WordprocessingMLPackage.createPackage();

// create the table

Tbl table = factory.createTbl();

//add boards to the table 
addBorders(table);

for(int rows = 0; rows<1;rows++)
{// create a row
Tr row = factory.createTr();
for(int  colm = 0; colm<1;colm++)
{

// create a cell

Tc cell = factory.createTc();

// add the content to cell

cell.getContent().add(wordPackage.getMainDocumentPart()
.createParagraphOfText("cell"+colm));

// add the cell to row
row.getContent().add(cell);
} 

// add the row to table

table.getContent().add(row);

// now add a run (to test whether run is working or not)
org.docx4j.wml.R run = factory.createR();
org.docx4j.wml.Text t = factory.createText();
run.getContent().add(t);    
t.setValue(value);
//add table to list
theList.add(rangeStart, table);
//add run to list
//theList.add(rangeStart, run);
}

I dont need to delete text in bookmark so i removed it. I dont know whats the problem, program is compiling but I cannot open the word doc , it says "unknown error". I test to write some string "value" it writes perfectly in that bookmark and document is opening but not in the case of table. Please help me Thanks in advance

¿Fue útil?

Solución

You can adapt the sample code BookmarksReplaceWithText.java

In your case:

  • line 89: the parent won't be p, it'll be body or tc. You could remove the test.
  • line 128: instead of adding a run, you want to insert a table

You can use TblFactory to create your table, or the docx4j webapp to generate code from a sample docx.

Otros consejos

For some reason bookmark replacement with table didn't workout for me, so I relied on text replacement with table. I created my tables from HTML using XHTML importer for my use case

MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();

    String xhtml= <your table HTML>;

    XHTMLImporterImpl XHTMLImporter = new XHTMLImporterImpl(wordMLPackage);
    int ct = 0;
    List<Integer> tableIndexes = new ArrayList<>();
    List<Object> documentContents = documentPart.getContent();
    for (Object o: documentContents) {

        if (o.toString().contains("PlaceholderForTable1")) {
            tableIndexes.add(ct);
        }
        ct++;
    }

    for (Integer i: tableIndexes) {
        documentPart.getContent().remove(i.intValue());
        documentPart.getContent().addAll(i.intValue(), XHTMLImporter.convert( xhtml, null));
    }

In my input word doc, I defined text 'PlaceholderForTable1' where I want to insert my table.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top