Hi there,
Thanks for your inquiry. Please use following code example to achieve your requirements. Hope this helps you.
The Aspose.Words.Layout namespace provides classes that allow to access information such as on what page and where on a page particular document elements are positioned, when the document is formatted into pages. Please use these APIs as shown below to get the height of table.
Following code example does the following:
- Import the table node that contains the group shape into empty document.
- Get the height of table using Aspose.Words.Layout API.
- Set the page’s width and height of new document according to table’s size.
- Export the document to image.
Document doc = new Document(MyDir + "Test4.docx");
Document dstDoc = new Document();
DocumentBuilder builder = new DocumentBuilder(dstDoc);
Table table = (Table)doc.getChild(NodeType.TABLE, 0, true);
NodeImporter importer = new NodeImporter(doc, dstDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING);
Node newNode = importer.importNode(table, true);
dstDoc.getFirstSection().getBody().appendChild(newNode);
dstDoc.getFirstSection().getBody().getFirstParagraph().remove();
builder.moveToDocumentStart();
BookmarkStart bStart = builder.startBookmark("Start_BM");
builder.endBookmark("Start_BM");
builder.moveToDocumentEnd();
BookmarkStart bStart2 = builder.startBookmark("End_BM");
builder.endBookmark("End_BM");
LayoutCollector collector = new LayoutCollector(dstDoc);
LayoutEnumerator enumerator = new LayoutEnumerator(dstDoc);
Object renderObject = collector.getEntity(bStart);
enumerator.setCurrent(renderObject);
double startP = enumerator.getRectangle().getY();
renderObject = collector.getEntity(bStart2);
enumerator.setCurrent(renderObject);
double endP = enumerator.getRectangle().getY();
dstDoc.getFirstSection().getPageSetup().setPageHeight(endP - startP);
dstDoc.getFirstSection().getPageSetup().setPageWidth(table.getPreferredWidth().getValue());
dstDoc.getFirstSection().getPageSetup().setLeftMargin(0.0);
dstDoc.getFirstSection().getPageSetup().setRightMargin(0.0);
dstDoc.getFirstSection().getPageSetup().setTopMargin(0.0);
dstDoc.getFirstSection().getPageSetup().setBottomMargin(0.0);
ImageSaveOptions options = new ImageSaveOptions(SaveFormat.PNG);
options.setPageCount(1);
options.setPageIndex(0);
dstDoc.updatePageLayout();
dstDoc.save(MyDir + "17.2.0.png", options);