I need to merge cells vertically and I found a sample code here at stackoverflow and it works ok, but afterwards when converting my XWPFDocument to pdf I'm getting following error fr.opensagres.poi.xwpf.converter.core.XWPFConverterException: org.apache.xmlbeans.impl.values.XmlValueDisconnectedException it happens if "cell.removeParagraph" executed.
public void convertToPdf() throws Exception {
InputStream targetStream = new FileInputStream("table.docx");
XWPFDocument document = new XWPFDocument(targetStream);
XWPFTable table = document.getTables().get(0);
mergeCellVertically(table, 0, 3, 7);
document.close();
FileOutputStream out = new FileOutputStream("table.pdf");
PdfOptions options = PdfOptions.create();
PdfConverter.getInstance().convert(document, out, options);
}
private void mergeCellVertically(XWPFTable table, int col, int fromRow, int toRow) {
for(int rowIndex = fromRow; rowIndex <= toRow; rowIndex++) {
XWPFTableCell cell = table.getRow(rowIndex).getCell(col);
CTVMerge vmerge = CTVMerge.Factory.newInstance();
if (rowIndex == fromRow){
// The first merged cell is set with RESTART merge value
vmerge.setVal(STMerge.RESTART);
} else {
// Cells which join (merge) the first one, are set with CONTINUE
vmerge.setVal(STMerge.CONTINUE);
// and the content should be removed
for (int i = cell.getParagraphs().size(); i > 0; i--) {
cell.removeParagraph(0);
}
cell.addParagraph();
}
// Try getting the TcPr. Not simply setting an new one every time.
CTTcPr tcPr = cell.getCTTc().getTcPr();
if (tcPr != null) {
tcPr.setVMerge(vmerge);
} else {
// only set an new TcPr if there is not one already
tcPr = CTTcPr.Factory.newInstance();
tcPr.setVMerge(vmerge);
cell.getCTTc().setTcPr(tcPr);
}
}
}