Find Rectangular Coordinates (x,y) Position of Word Document Elements using Java API

We used to use jacob.

the method is

public static Integer getRangeEnd(String path) {
	Integer endRange = 0;
	ActiveXComponent app = new ActiveXComponent("Word.Application");
	try {
		app.setProperty("Visible", new Variant(false));
		Dispatch docs = app.getProperty("Documents").toDispatch();
		Dispatch doc = Dispatch.invoke((Dispatch) docs, "Open", Dispatch.Method,
				new Object[] { path, new Variant(false), new Variant(true) }, new int[3]).toDispatch();
		Dispatch content = Dispatch.get(doc, "Content").toDispatch();

		Variant variant = Dispatch.get(content, "End");
		endRange = variant.getInt(); doc, "Close", new Variant(false));
	} catch(Exception e) {
		endRange = 0;
	} finally {
		app.invoke("Quit", new Variant[] {});
	return endRange;

Which method of your API do we need to replace?


Unfortunately, your question is not clear enough therefore we request you to please elaborate your inquiry further by providing complete details of your usecase. Also, to ensure a timely and accurate response, please ZIP and attach the following resources here for testing:

  • Your simplified input Word document you want to process with Aspose.Words API
  • Your expected Word document showing the desired output. You can create expected document by using MS Word.
  • Please also provide a comparison screenshot highlighting the desired areas that you want to modify/process by using Aspose.Words.

As soon as you get these pieces of information ready, we will start investigation into your scenario and provide you code to achieve the same by using Aspose.Words. Thanks for your cooperation.

	Document document = new Document(path1);
	DocumentBuilder builder = new DocumentBuilder(document);

I want to get the position of this cursor(DocumentEnd).
How to achieve it?


I think, you can make use of DocumentBuilder.getCurrentParagraph(), DocumentBuilder.getCurrentNode(), DocumentBuilder.getCurrentSection() and DocumentBuilder.getCurrentStory() methods to meet this requirement. Please see the following code for example:

Document doc= new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

builder.writeln("First Paragraph");
for (int i=1; i<=10; i++) {
    builder.writeln("Paragraph # " + i);
builder.writeln("Last Paragraph");

// move to document start and check its position
System.out.println("Position is at " + builder.getCurrentParagraph().toString((SaveFormat.TEXT)));

// now move to last Paragraph
builder.moveToParagraph(11, 0);
System.out.println("Position is at " + builder.getCurrentParagraph().toString((SaveFormat.TEXT)));

We use VSTO to operate word. We need to use coordinates to mark the location. So we need the position value of the cursor at the end of word.

I need this value.52165efb8923cb489a3ea074a000e2b4.png (39.3 KB)

word document is (51.5 KB)


Please check:

For example, the following code will return coordinates rectangle [(left, top)] and [(width, height)] of all Shapes (images) in Word document:

Document doc = new Document("E:\\Temp\\testpaper\\testpaper.docx");

LayoutCollector collector = new LayoutCollector(doc);
LayoutEnumerator enumerator = new LayoutEnumerator(doc);

for (Shape shape : (Iterable<Shape>) doc.getFirstSection().getBody().getChildNodes(NodeType.SHAPE, true)) {

    String left = String.format("%.2f", enumerator.getRectangle().getX());
    String top = String.format("%.2f", enumerator.getRectangle().getY());
    String width = String.format("%.2f", enumerator.getRectangle().getWidth());
    String height = String.format("%.2f", enumerator.getRectangle().getHeight());

    System.out.print("[(x, y) = (" + left + ", " + top + ")]");
    System.out.println(" AND [(width, height) = (" + width + ", " + height + ")]");

Hope, this helps in achieving what you are looking for.