I want to know the startPositionOfRepeatingSection in pixels. Currently I am using
shape.getAbsoluteVerticalDistance() but it is not working as intended because it is not displaying correct value related to page margin. Can you help me with this?
@Sri_Harsha You can use LayoutCollector
and LayoutEnumerator
classes to calculate actual bounds of shapes in the document. Please see the following code:
Document doc = new Document("C:\\Temp\\in.docx");
LayoutCollector collector = new LayoutCollector(doc);
LayoutEnumerator enumerator = new LayoutEnumerator(doc);
Iterable<Shape> shapes = doc.getChildNodes(NodeType.SHAPE, true);
for (Shape s : shapes)
{
// Make sure we work with top level shape and shape is not in header/footer.
if (s.isTopLevel() && s.getAncestor(NodeType.HEADER_FOOTER) == null)
{
enumerator.setCurrent(collector.getEntity(s));
Rectangle2D rect = enumerator.getRectangle();
System.out.println("Page: " + enumerator.getPageIndex() + "\tX=" + rect.getX() + "; Y=" + rect.getY() + "; Width=" + rect.getWidth() + "; Height=" + rect.getHeight());
}
}
@Sri_Harsha Complementing @alexey.noskovβs reply, Aspose.Words API usually represents measurements in Points. To convert it to Pixels, you can use the ConvertUtil
class (here you can found an example of it usage).
I want to find the starting position of repeating section(NodeType.Table) in pixels Currently I am using
shape.getAbsoluteVerticalDistance() in my POC but in my case PageHeightin pixels : 1056.0 so when I place my repeating section at bottom of the page then the repeating section should be around 1056 but it is showing around 720 pixels is shape.getAbsoluteVerticalDistance() is the right way to find the start position of repeating section ?
@Sri_Harsha As I have mentioned, you can use LayoutCollector
and LayoutEnumerator
classes to calculate actual bounds of nodes in MS Word documents.
Could you please attach your input document here for our reference? We will check it and provide you more information.
Actually I am using NodeType.TABLE
instead of shape but still with your code I created a template with a rectangle shape and placed it at bottom of the page of page Height = 1056 pixels but I am getting Y co-ordinate value as 1140.0 pixel which is practically impossible may be I am using wrong method to calculate the page Height , I am using
POLineTemp - Copy.docx (28.9 KB)
POLineTemp.docx (16.3 KB)
PageSetup page = doc.getFirstSection().getPageSetup();
double pageHeightInPixels = convertToPixels(page.getPageHeight());
To calculate the page height and I used your code to calculate the start position of the repeating section i.e rectangle. Can you help me with this issue?. I am attaching the template which I created for this code testing i.e of rectangle shape(POLineTemp.docx) and I am also attaching my original template of table(POLineTemp - Copy.docx) as well.
Thanks in advance for the help.
@Sri_Harsha Your code to get page height is correct. You can use the following code to calculate bounding box of table. But please note, the code is simplified to demonstrate the basic technique and converts only tables placed on a single page. In MS Word tables can span more than one page.
// Open document
Document doc = new Document("C:\\Temp\\in.docx");
// Create LayoutCollector and LayoutEnumerator classes to get layout information of nodes.
LayoutCollector collector = new LayoutCollector(doc);
LayoutEnumerator enumerator = new LayoutEnumerator(doc);
// Get the table
Table t = (Table)doc.getChild(NodeType.TABLE, 0, true);
// LayoutCollector and LayoutEnumerator classes do not work with header/footer nodes
if (t.getAncestor(NodeType.HEADER_FOOTER) != null)
throw new InvalidParameterException("LayoutCollector and LayoutEnumerator classes do not work with header/footer nodes");
// Move LayoutEnumerator to the first row
enumerator.setCurrent(collector.getEntity(t.getFirstRow().getFirstCell().getFirstParagraph()));
while (enumerator.getType() != LayoutEntityType.ROW)
enumerator.moveParent();
//Get rectangle of the first row of the table.
Rectangle2D first_rect = enumerator.getRectangle();
// Do the same with last row
enumerator.setCurrent(collector.getEntity(t.getLastRow().getFirstCell().getFirstParagraph()));
while (enumerator.getType() != LayoutEntityType.ROW)
enumerator.moveParent();
// Get rectangle of the last row in the table.
Rectangle2D last_rect = enumerator.getRectangle();
// Union of the rectangles is the bounding box of the table.
Rectangle2D result_rect = first_rect.createUnion(last_rect);
System.out.println("Table rectangle : x=" + result_rect.getX() + ", y=" + result_rect.getY() + ", width=" + result_rect.getWidth() + ", height=" + result_rect.getHeight());
// Create rectangle of the page.
Section parentSect = (Section)t.getAncestor(NodeType.SECTION);
Rectangle2D page_rect = new Rectangle2D.Double(0, 0, parentSect.getPageSetup().getPageWidth(), parentSect.getPageSetup().getPageHeight());
System.out.println("Page rectangle : x=" + page_rect.getX() + ", y=" + page_rect.getY() + ", width=" + page_rect.getWidth() + ", height=" + page_rect.getHeight());
The code returns correct coordinates for the table:
Table rectangle : x=0.25, y=709.5599975585938, width=545.1500244140625, height=29.575977325439453
Page rectangle : x=0.0, y=0.0, width=612.0, height=792.0
package com.local.aspose.project1.test1;
import java.awt.geom.Rectangle2D;
import java.security.InvalidParameterException;
import com.aspose.words.Document;
import com.aspose.words.LayoutCollector;
import com.aspose.words.LayoutEntityType;
import com.aspose.words.LayoutEnumerator;
import com.aspose.words.NodeType;
import com.aspose.words.Section;
import com.aspose.words.Table;
public class Test14 {
public static void main(String[] args) throws Exception {
Document doc = new Document("FailingTemplates\\PoLineTemp - Copy.docx");
LayoutCollector collector = new LayoutCollector(doc);
LayoutEnumerator enumerator = new LayoutEnumerator(doc);
// Get the table
Table t = (Table)doc.getChild(NodeType.TABLE, 0, true);
if (t.getAncestor(NodeType.HEADER_FOOTER) != null)
throw new InvalidParameterException("LayoutCollector and LayoutEnumerator classes do not work with header/footer nodes");
// Move LayoutEnumerator to the first row
enumerator.setCurrent(collector.getEntity(t.getFirstRow().getFirstCell().getFirstParagraph()));
while (enumerator.getType() != LayoutEntityType.ROW)
enumerator.moveParent();
//Get rectangle of the first row of the table.
Rectangle2D first_rect = enumerator.getRectangle();
// Do the same with last row
enumerator.setCurrent(collector.getEntity(t.getLastRow().getFirstCell().getFirstParagraph()));
while (enumerator.getType() != LayoutEntityType.ROW)
enumerator.moveParent();
// Get rectangle of the last row in the table.
Rectangle2D last_rect = enumerator.getRectangle();
// Union of the rectangles is the bounding box of the table.
Rectangle2D result_rect = first_rect.createUnion(last_rect);
System.out.println("Table rectangle : x=" + SummaryUtil.convertingToPixels(result_rect.getX()) + ", y=" + SummaryUtil.convertingToPixels(result_rect.getY()) + ", width=" + SummaryUtil.convertingToPixels(result_rect.getWidth()) + ", height=" + SummaryUtil.convertingToPixels(result_rect.getHeight()));
// Create rectangle of the page.
Section parentSect = (Section)t.getAncestor(NodeType.SECTION);
Rectangle2D page_rect = new Rectangle2D.Double(0, 0, parentSect.getPageSetup().getPageWidth(), parentSect.getPageSetup().getPageHeight());
System.out.println("Page rectangle : x=" + SummaryUtil.convertingToPixels(page_rect.getX()) + ", y=" + SummaryUtil.convertingToPixels(page_rect.getY()) + ", width=" + SummaryUtil.convertingToPixels(page_rect.getWidth()) + ", height=" + SummaryUtil.convertingToPixels(page_rect.getHeight()));
}
}
With your code I just used my docx which I attached to you previously and I changed from point to pixel but still I am not getting correct value of Y co-ordinate when I placed the repeating section at very bottom I am getting Table rectangle : x=2.0, y=795.0, width=728.0, height=40.0
Page rectangle : x=0.0, y=0.0, width=816.0, height=1056.0
Here, page Height is correct but Table rectangle should be around page height in pixel i.e 1056 but it displaying 795 pixel. SummaryUtil.ConvertingToPixels is this below Code which I used to convert point to pixel.
public static double convertingToPixels(double value) {
return getCeilValue(ConvertUtil.pointToPixel(getCeilValue(value)));
}
Can you help with this?
Thank You POLineTemp - Copy.docx (22.3 KB)
@Sri_Harsha Code returns correct coordinates on my side. The problem might occur because fonts used in your document are not available in the environment where you process the document.
As you may know, MS Word documents are flow documents and do not contain any information about document layout. The consumer applications, like MS Word or Open Office builds document layout on the fly. Aspose.Words uses itβs own layout engine to build document layout while rendering the document to fixed page formats (PDF, XPS, Image etc.). The same layout engine is used for providing document layout information via LayoutCollector
and LayoutEnumerator
classes.
To built proper document layout the fonts used in the original document are required. If Aspose.Words cannot find the fonts used in the document the fonts are substituted . This might lead into the layout difference (incorrect coordinates returned by LayoutEnumerator
), since substitution fonts might have different font metrics. You can implement IWarningCallback to get a notification when font substitution is performed.
@alexey.noskov Thanks for the reply I will check that. For finding rowHeight also I am using (
SummaryUtil.convertingToPixels(shape.getFirstRow().getRowFormat().getHeight()) + shape.getLastRow().getRowFormat().getHeight());
I am getting 30 pixels as height
Is it correct ?
@Sri_Harsha RowFormat().getHeight()
returns an absolute height of the row only if the row height rule is HeightRule.EXACTLY
. In other case height of the row is adjusted to the content. So you cannot rely on this property to calculate actual height of the table.
@Sri_Harsha Yes, using LayoutCollector
and LayoutEnumerator
classes as has been demonstrated in the code example provided above.
@alexey.noskov Thank You , Can you please try the code of finding the repeatingsection start position with my docx and in pixels which I attached previously and give me the response because the code you sent was with your docx and not in pixels and the code which I sent to you as reply has that
@Sri_Harsha I have used your document as an input for testing, just renamed it, and the code returns the correct coordinates. The units does no matter in this case. Aspose.Words main measurement units are points. Once you get coordinates in points you can convert them to any other measurement units.
@alexey.noskov, Now I am able to get correct Repeating Section Start Position but when I try to generate a pdf with the attached template and data xml with one element I am getting null pointer exception and unable to read the template can you please tell me what may be the reasons to get such errors and how to resolve them?.POLineTemp - Copy.docx (22.5 KB)
@Sri_Harsha Unfortunately, I cannot reproduce the problem on my side. Could you please provide a simple code example and required data, that will allow us to reproduce the problem? We will check the issue and provide you more information.
@alexey.noskov, When I am converting node(dataXml) to document then I am getting DOM Exception stating that NOT_SUPPORTED_ERR: The implementation does not support the requested type of object or operation. when I used this code below:
Node node = doc.importNode(dataXml, true);
doc.appendChild(node);
How to resolve the issue