@zhengkai Here is java version of the code:
Document doc = new Document("C:\\Temp\\in.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
// Get all top level tables.
Node[] tables = doc.getChildNodes(NodeType.TABLE, true).toArray();
// Render tables to images and replace.
for (Node tableNode : tables)
{
// process only top level tables
if (tableNode.getAncestor(NodeType.TABLE) != null)
continue;
Table table = (Table)tableNode;
byte[] tableImage = renderTable(table);
// insert paragraph before the table.
Paragraph p = new Paragraph(doc);
table.getParentNode().insertBefore(p, table);
// Move document builder to the crete paragraph and insert the image.
builder.moveTo(p);
builder.insertImage(tableImage);
// Remove the table
table.remove();
}
doc.save("C:\\Temp\\out.docx");
public static byte[] renderTable(Table tableToRender) throws Exception
{
Document oneTableDoc = (Document)tableToRender.getDocument().deepClone(false);
oneTableDoc.ensureMinimum();
oneTableDoc.getFirstSection().getBody().prependChild(oneTableDoc.importNode(tableToRender, true, ImportFormatMode.USE_DESTINATION_STYLES));
// Set maximum allowed page height
oneTableDoc.getFirstSection().getPageSetup().setPageHeight(1584);
LayoutCollector collector = new LayoutCollector(oneTableDoc);
LayoutEnumerator enumerator = new LayoutEnumerator(oneTableDoc);
Table table = oneTableDoc.getFirstSection().getBody().getTables().get(0);
// Calculate table size.
// For demonstration purposes the example purposes the while table is on the same page.
enumerator.setCurrent(collector.getEntity(table.getFirstRow().getFirstCell().getFirstParagraph()));
int startPageIndex = enumerator.getPageIndex();
// Move enumerator to a row.
while (enumerator.getType()!= LayoutEntityType.ROW)
enumerator.moveParent();
double top = enumerator.getRectangle().y;
double left = enumerator.getRectangle().x;
// Move enumerator to the last row.
enumerator.setCurrent(collector.getEntity(table.getLastRow().getFirstCell().getFirstParagraph()));
int endPageIndex = enumerator.getPageIndex();
// Move enumerator to a row.
while (enumerator.getType()!= LayoutEntityType.ROW)
enumerator.moveParent();
double bottom = enumerator.getRectangle().y + enumerator.getRectangle().height;
double right = enumerator.getRectangle().x + enumerator.getRectangle().width;
// Reset margins
PageSetup ps = oneTableDoc.getFirstSection().getPageSetup();
ps.setPageWidth(ps.getPageWidth()-ps.getLeftMargin()-ps.getRightMargin());
ps.setLeftMargin(0);
ps.setRightMargin(0);
ps.setPageHeight(ps.getPageHeight()-ps.getTopMargin()-ps.getBottomMargin());
ps.setTopMargin(0);
ps.setBottomMargin(0);
// Set calculated width
ps.setPageWidth(right - left);
// Do not set page height if table spans several pages.
if(startPageIndex == endPageIndex)
ps.setPageHeight(bottom - top);
oneTableDoc.updatePageLayout();
// Render table to image and return image bytes
ByteArrayOutputStream tableImageStream = new ByteArrayOutputStream();
oneTableDoc.save(tableImageStream, SaveFormat.PNG);
return tableImageStream.toByteArray();
}