Hi,
I am currently using this code :
LayoutCollector collector = new LayoutCollector(templateDoc);
LayoutEnumerator enumerator = new LayoutEnumerator(templateDoc);
Table t = (Table)templateDoc.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);
REPEATING_SECTION_START_POS = (int)SummaryUtil.convertingToPixels(result_rect.getY());
to get the repeating section start position but the issue with this code is if in the template we have 2 tables which are not repeating section and 1 table which is a repeating section then this code is considering the repeating section start position for the non-repeating section table. Can we find the repeating section start position for only repeating section tables only ?
@Sri_Harsha Could you please attach your input document here for our reference? We will check it and provide you more information.
@alexey.noskov I am attaching the template and in the xml file I have data where POLine is the repeating section
TotalSummaryTemplate.docx (48.0 KB)
@Sri_Harsha You should simply get the table with repeating section SDT. You can use the following code to achieve this:
Document doc = new Document("C:\\Temp\\in.docx");
Node[] tags = doc.getChildNodes(NodeType.STRUCTURED_DOCUMENT_TAG, true).toArray();
StructuredDocumentTag repeatingSectionSdt = (StructuredDocumentTag)Arrays.stream(tags)
.filter(sdt-> ((StructuredDocumentTag)sdt).getSdtType() == SdtType.REPEATING_SECTION)
.findFirst()
.orElse(null);
// Get parent table of repeating section SDT.
if (repeatingSectionSdt != null)
{
Table parentTable = (Table)repeatingSectionSdt.getAncestor(NodeType.TABLE);
if (parentTable != null)
{
// Process the table.
// ...................
}
}
@alexey.noskov Thanks for the code can you also provide the code for the retrieving the row height of the repeating section table for the above code currently I am using :
ROW_HEIGHT = (int) SummaryUtil.convertingToPixels(result_rect.getHeight());
with in the above code
@Sri_Harsha The code is the same, just move layout enumerator to the first row of the table and get the row rectangle:
// Move LayoutEnumerator to the first row
enumerator.setCurrent(collector.getEntity(table.getFirstRow().getFirstCell().getFirstParagraph()));
while (enumerator.getType() != LayoutEntityType.ROW)
enumerator.moveParent();
//Get rectangle of the first row of the table.
Rectangle2D first_rect = enumerator.getRectangle();
ROW_HEIGHT = (int)SummaryUtil.convertingToPixels(first_rect.getHeight());
@alexey.noskov Thanks for the help, I am pasting the code over here can you please confirm is this code is correct for retrieving Repeating section start position of a repeating section table and row height of the repeating section table?
Node[] tags = templateDoc.getChildNodes(NodeType.STRUCTURED_DOCUMENT_TAG, true).toArray();
StructuredDocumentTag repeatingSectionSdt = (StructuredDocumentTag)Arrays.stream(tags)
.filter(new Predicate<Node>() {
@Override
public boolean test(Node sdt) {
return ((StructuredDocumentTag)sdt).getSdtType() == SdtType.REPEATING_SECTION;
}
})
.findFirst()
.orElse(null);
// Get parent table of repeating section SDT.
if (repeatingSectionSdt != null)
{
Table t = (Table)repeatingSectionSdt.getAncestor(NodeType.TABLE);
if (t != null)
{
// Process the table.
// ...................
LayoutCollector collector = new LayoutCollector(templateDoc);
LayoutEnumerator enumerator = new LayoutEnumerator(templateDoc);
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);
REPEATING_SECTION_START_POS = (int) SummaryUtil.convertingToPixels(result_rect.getY());
ROW_HEIGHT = (int) SummaryUtil.convertingToPixels(first_rect.getHeight());
}
}
@Sri_Harsha You code is correct, but since it is required to get only repeating section start position, it is not required to calculate last row bounds:
Node[] tags = templateDoc.getChildNodes(NodeType.STRUCTURED_DOCUMENT_TAG, true).toArray();
StructuredDocumentTag repeatingSectionSdt = (StructuredDocumentTag)Arrays.stream(tags)
.filter(new Predicate<Node>() {
@Override
public boolean test(Node sdt) {
return ((StructuredDocumentTag)sdt).getSdtType() == SdtType.REPEATING_SECTION;
}
})
.findFirst()
.orElse(null);
// Get parent table of repeating section SDT.
if (repeatingSectionSdt != null)
{
Table t = (Table)repeatingSectionSdt.getAncestor(NodeType.TABLE);
if (t != null)
{
// Process the table.
// ...................
LayoutCollector collector = new LayoutCollector(templateDoc);
LayoutEnumerator enumerator = new LayoutEnumerator(templateDoc);
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();
REPEATING_SECTION_START_POS = (int) SummaryUtil.convertingToPixels(first_rect.getY());
ROW_HEIGHT = (int) SummaryUtil.convertingToPixels(first_rect.getHeight());
}
}
@alexey.noskov Thanks for the help
1 Like
@alexey.noskov For the below attached template I am getting the row height as 40 pixels and Repeating section start position as 800 pixels are they correct values,can you confirm it from your end?
The code which I am using to get the Row Height is :
Rectangle2D first_rect = enumerator.getRectangle();
REPEATING_SECTION_START_POS = (int) SummaryUtil.convertingToPixels(first_rect.getY());
ROW_HEIGHT = (int) SummaryUtil.convertingToPixels(first_rect.getHeight());
MyPageTotal12 - Copy.docx (21.4 KB)
Here, the repeating section is Company
@Sri_Harsha It looks like you are using Aspose.Words in evaluation mode and the table is pushed down by the injected evaluation message. If use Aspose.Words in licensed mode the following code gives me the following results:
System.out.println("Vertical position: "+first_rect.getY()+"pt = "+ ConvertUtil.pointToPixel(first_rect.getY())+"px");
System.out.println("Row height: "+first_rect.getHeight()+"pt = "+ ConvertUtil.pointToPixel(first_rect.getHeight())+"px");
Vertical position: 517.2930297851562pt = 689.7240397135417px
Row height: 29.299999237060547pt = 39.06666564941406px
in evaluation mode results are the following:
Vertical position: 599.5910034179688pt = 799.4546712239583px
Row height: 29.299999237060547pt = 39.06666564941406px
Coordinates returned by Aspose.Words are correct:
@alexey.noskov Ok now I have changed my code from evaluation mode to licensed mode and getting correct values for the template which I have previously attached but for the below template I think the values of the repeating section and row height are wrong. I am getting
Repeating section start position is : 312
Row height is : 38
In pixels can you confirm these values are correct or not?
Here is the template:
TotalSummaryTemplate.docx (36.6 KB)
@Sri_Harsha I hat the same coordinates on my side:
Vertical position: 233.8070068359375pt = 311.74267578125px
Row height: 27.356000900268555pt = 36.474667867024735px
The values are correct and matches MS Word.
@alexey.noskov Thanks for the confirmation
1 Like