@aelum This is expected behavior. Aspose.Words layouts the document into pages while saving document to image. In order to get the whole big table as a single image, you should enlarge page height using PageSetup.PageHeight . But you should note that the maximum allowed height of the page in MS Word document is 1584 points.
You can use LayoutCollector to determine page numbers where table starts and ends.
my requirements: we are extracting all the data from the above documents i.e images, charts, tables , text
Images and charts working fine but issue with tables
when using tables :
problems: not getting all the tables data as images. only getting a single page image e.g (if a table having data which needs two pages to store it as images) . output shown in first reply.
my code for tables:
{
int i=1;
NodeCollection tables = doc.getChildNodes(NodeType.TABLE, true);
for (Table table : (Iterable<Table>) tables) {
renderNode(table, new ImageSaveOptions(SaveFormat.JPEG), i);
i++;
}
public static void renderNode(Node node, ImageSaveOptions imageOptions, int i) throws Exception
{
if (node == null)
throw new Exception("Node cannot be null");
// If no image options are supplied, create default options.
// imageOptions.set
if (imageOptions == null)
imageOptions = new ImageSaveOptions(SaveFormat.JPEG);
imageOptions.setPaperColor(new Color(0, 0, 0, 0));
// There a bug which affects the cache of a cloned node. To avoid this we
// instead clone the entire document including all nodes,
// find the matching node in the cloned document and render that instead.
Document doc = ((Document) node.getDocument()).deepClone();
node = doc.getChild(NodeType.ANY, node.getDocument().getChildNodes(NodeType.ANY, true).indexOf(node), true);
// Create a temporary shape to store the target node in. This shape will be
// rendered to retrieve
// the rendered content of the node.
Shape shape = new Shape(doc, ShapeType.TEXT_BOX);
Section parentSection = (Section) node.getAncestor(NodeType.SECTION);
// Assume that the node cannot be larger than the page in size.
shape.setWidth(parentSection.getPageSetup().getPageWidth());
shape.setHeight(parentSection.getPageSetup().getPageHeight());
shape.setFillColor(new Color(0,0,0,0)); // We must make the shape and paper color transparent.
// Don't draw a surronding line on the shape.
shape.setStroked(false);
Node currentNode = node;
// If the node contains block level nodes then just add a copy of these nodes to
// the shape.
if (currentNode instanceof InlineStory || currentNode instanceof Story)
{
CompositeNode composite = (CompositeNode) currentNode;
for (Node childNode : (Iterable<Node>) composite.getChildNodes())
{
shape.appendChild(childNode.deepClone(true));
}
}
else
{
// Move up through the DOM until we find node which is suitable to insert into a
// Shape (a node with a parent can contain paragraph, tables the same as a
// shape).
// Each parent node is cloned on the way up so even a descendant node passed to
// this method can be rendered.
// Since we are working with the actual nodes of the document we need to clone
// the target node into the temporary shape.
while (!(currentNode.getParentNode() instanceof InlineStory || currentNode.getParentNode() instanceof Story
|| currentNode.getParentNode() instanceof ShapeBase
|| currentNode.getNodeType() == NodeType.PARAGRAPH))
{
CompositeNode parent = (CompositeNode) currentNode.getParentNode().deepClone(false);
currentNode = currentNode.getParentNode();
parent.appendChild(node.deepClone(true));
node = parent; // Store this new node to be inserted into the shape.
}
// Add the node to the shape.
shape.appendChild(node.deepClone(true));
}
// We must add the shape to the document tree to have it rendered.
parentSection.getBody().getFirstParagraph().appendChild(shape);
shape.getShapeRenderer().save("C:\\Users\\DHANANJAY\\Desktop\\jovus-up\\wordReader file\\Question_ask\\file made\\one\\" + "Out"+i+""+".jpeg", imageOptions);
BufferedImage renderedImage = ImageIO.read(new File("C:\\Users\\DHANANJAY\\Desktop\\jovus-up\\wordReader file\\Question_ask\\file made\\one\\" + "Out"+i+""+".jpeg"));
// Extract the actual content of the image by cropping transparent space around
// the rendered shape.
Rectangle cropRectangle = FindBoundingBoxAroundNode(renderedImage);
BufferedImage out = renderedImage.getSubimage(cropRectangle.x, cropRectangle.y, cropRectangle.width,
cropRectangle.height);
File outputfile = new File("C:\\Users\\DHANANJAY\\Desktop\\jovus-up\\wordReader file\\Question_ask\\file made one\\" + "Out"+i+""+".jpeg");
ImageIO.write(out, "jpeg", outputfile);
}
public static Rectangle FindBoundingBoxAroundNode(BufferedImage originalBitmap)
{
Point min = new Point(Integer.MAX_VALUE, Integer.MAX_VALUE);
Point max = new Point(Integer.MIN_VALUE, Integer.MIN_VALUE);
for (int x = 0; x < originalBitmap.getWidth(); ++x)
{
for (int y = 0; y < originalBitmap.getHeight(); ++y)
{
int argb = originalBitmap.getRGB(x, y);
if (argb != new Color(0, 0, 0, 0).getRGB())
{
min.x = Math.min(x, min.x);
min.y = Math.min(y, min.y);
max.x = Math.max(x, max.x);
max.y = Math.max(y, max.y);
}
}
}
return new Rectangle(min.x, min.y, (max.x - min.x) + 1, (max.y - min.y) + 1);
}
@aelum Unfortunately, it is not quite clear what is the expected output. Could you please elaborate your requirements in more details and provide the expected output you would like to get?
If your requirement is to get whole table as a single image, then, as I mentioned earlier, to get the whole big table as a single image, you should enlarge page height using PageSetup.PageHeight to make the table fit one page.
suppose i have a docx ,which has table containing 250 records(rows)
my requirements is to get 40 records per images from table ,
for 250 records it should be generating 7 images.
Then how can i do this. ?
@aelum In your case you should copy the table that should be converted to image into a separate document and use code I have suggested earlier to render this document to images:
Document doc = new Document(@"C:\Temp\in.docx");
ImageSaveOptions opt = new ImageSaveOptions(SaveFormat.Png);
for (int page = 0; page < doc.PageCount; page++)
{
opt.PageSet = new PageSet(page);
doc.Save(string.Format(@"C:\Temp\out_{0}.png", page), opt);
}
@aelum In this case you need to determine the actual size of the table and adjust page size accordingly. You can use LayoutCollector and LayoutEnumerator to get actual bounds of a table. For example see the following that demonstrates the technique:
Document doc = new Document("C:\\Temp\\in.docx");
LayoutCollector collector = new LayoutCollector(doc);
LayoutEnumerator enumerator = new LayoutEnumerator(doc);
Table table = doc.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()));
// 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()));
// 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;
System.out.println("X=" + left + "; Y="+top+"; X1="+right+"; Y1="+bottom);
@aelum Once you calculated size of the table using the suggested code, you can set height and width of page accordingly in temporary document page setup:
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.