Reputation: 106
So, I'm trying to insert a table at a certain point in a .docx file using Apache POI (Vers. 3.15). I'm able to create it, using the code below. That throws the table that I need into the end of the document. (If this is superfluous, my apologies! Trying to give as much info as possible!)
/* Creates table for the questions */
private XWPFTable createMainTable(XWPFDocument doc, ArrayList<String> qs, ArrayList<String> avgScores, ArrayList<String> favScores){
XWPFTable table = doc.createTable();
int currRow = 0;
for(String q : qs){
XWPFTableRow curRow = table.getRow(currRow);
if(currRow == 0){
curRow.getCell(0).setText("Question");
}else{
curRow.getCell(0).setText(q);
}
if(currRow < qs.size()-1){
table.createRow();
currRow++;
}else{
currRow++;
}
}
currRow = 0;
for(String avg : avgScores){
XWPFTableRow curRow = table.getRow(currRow);
curRow.addNewTableCell();
if(currRow == 0){
curRow.getCell(1).setText(" Average Score ");
}else{
curRow.getCell(1).setText(avg);
}
currRow++;
}
currRow = 0;
for(String fav : favScores){
XWPFTableRow curRow = table.getRow(currRow);
curRow.addNewTableCell();
if(currRow == 0){
curRow.getCell(2).setText(" Favorable Score ");
}else{
curRow.getCell(2).setText(fav);
}
currRow++;
}
return table;
}
Now, in order to replace the rest of the text in the document, I use the following method:
/* Replace text in the document */
private long replaceText(ArrayList<String> comments, String targ, XWPFDocument doc) {
long count = 0;
for (XWPFParagraph paragraph : doc.getParagraphs()) {
List<XWPFRun> runs = paragraph.getRuns();
StringBuilder sb = new StringBuilder();
for (String c : comments){
sb.append(c);
sb.append("|");
}
String find = targ;
String repl = sb.toString();
TextSegement found = paragraph.searchText(find, new PositionInParagraph());
if ( found != null ) {
count++;
if ( found.getBeginRun() == found.getEndRun() ) {
// whole search string is in one Run
XWPFRun run = runs.get(found.getBeginRun());
String runText = run.getText(run.getTextPosition());
String replaced = runText.replace(find, repl);
run.setText(replaced, 0);
} else {
// The search string spans over more than one Run
// Put the Strings together
StringBuilder b = new StringBuilder();
for (int runPos = found.getBeginRun(); runPos <= found.getEndRun(); runPos++) {
XWPFRun run = runs.get(runPos);
b.append(run.getText(run.getTextPosition()));
}
String connectedRuns = b.toString();
String replaced = connectedRuns.replace(find, repl);
// The first Run receives the replaced String of all connected Runs
XWPFRun partOne = runs.get(found.getBeginRun());
partOne.setText(replaced, 0);
// Removing the text in the other Runs.
for (int runPos = found.getBeginRun()+1; runPos <= found.getEndRun(); runPos++) {
XWPFRun partNext = runs.get(runPos);
partNext.setText("", 0);
}
}
}
}
return count;}
What that 2nd method does is find a certain keyword in the document, and replace it with a String. Right now, it's passed an ArrayList, but that changes as I'm trying different things out. I was able to get the memory address of the table to print out (by changing the input to an XWPFTable), but that's as far as I was able to get. I feel like I'm missing something stupid, but I just can't figure it out.
Upvotes: 2
Views: 3346
Reputation: 106
Much thanks to Axel, I was able to get the desired result! I changed my creation method a little bit, and passed it just an empty table to fill. Then, using the cursor like Axel suggested, it now finds the target string ("%TABLE"), and replaces it with the table, exactly as I needed.
/* Replaces table */
private long replaceTable(XWPFDocument doc, ArrayList<String> qs, ArrayList<String> avgScores, ArrayList<String> favScores) {
XWPFTable table = null;
long count = 0;
for (XWPFParagraph paragraph : doc.getParagraphs()) {
List<XWPFRun> runs = paragraph.getRuns();
String find = "%TABLE";
TextSegement found = paragraph.searchText(find, new PositionInParagraph());
if ( found != null ) {
count++;
if ( found.getBeginRun() == found.getEndRun() ) {
// whole search string is in one Run
XmlCursor cursor = paragraph.getCTP().newCursor();
table = doc.insertNewTbl(cursor);
XWPFRun run = runs.get(found.getBeginRun());
// Clear the "%TABLE" from doc
String runText = run.getText(run.getTextPosition());
String replaced = runText.replace(find, "");
run.setText(replaced, 0);
} else {
// The search string spans over more than one Run
StringBuilder b = new StringBuilder();
for (int runPos = found.getBeginRun(); runPos <= found.getEndRun(); runPos++) {
XWPFRun run = runs.get(runPos);
b.append(run.getText(run.getTextPosition()));
}
String connectedRuns = b.toString();
XmlCursor cursor = paragraph.getCTP().newCursor();
table = doc.insertNewTbl(cursor);
String replaced = connectedRuns.replace(find, ""); // Clear search text
// The first Run receives the replaced String of all connected Runs
XWPFRun partOne = runs.get(found.getBeginRun());
partOne.setText(replaced, 0);
// Removing the text in the other Runs.
for (int runPos = found.getBeginRun()+1; runPos <= found.getEndRun(); runPos++) {
XWPFRun partNext = runs.get(runPos);
partNext.setText("", 0);
}
}
}
}
fillTable(table, qs, avgScores, favScores);
return count;
}
Upvotes: 2