We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

How to draw shape on not first page

Hi all!
I want to draw rectangle on some page in document. I use next code and draw rectangle on first page.
But I can’t draw rectangle on any other pages. Could you please help me with it?

DocumentBuilder documentBuilder = new DocumentBuilder(document);
Shape textBox = new Shape(document, ShapeType.RECTANGLE);
textBox.setWrapType(WrapType.NONE);
textBox.setLeft(0);
textBox.setTop(0);
textBox.setWidth(200);
textBox.setHeight(20);
textBox.setFillColor(Color.BLACK);
textBox.setVerticalAlignment(VerticalAlignment.CENTER);
documentBuilder.moveToDocumentStart();
documentBuilder.insertNode(textBox);

Any recommendations?

Hi there,

Thanks for your inquiry. 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 read about LayoutCollector and LayoutEnumerator from here:
https://reference.aspose.com/words/java/com.aspose.words/LayoutCollector
https://reference.aspose.com/words/java/com.aspose.words/layoutenumerator

Please use LayoutCollector.getStartPageIndex method to get 1-based index of the page where node begins. Returns 0 if node cannot be mapped to a page. Please use the following code example to achieve your requirements. Hope this helps you.

Document doc = new Document(MyDir + "in.docx");
LayoutCollector collector = new LayoutCollector(doc);
Paragraph anchorPara = null;
for (Paragraph para : (Iterable<Paragraph>)doc.getChildNodes(NodeType.PARAGRAPH, true))
{
    if (collector.getStartPageIndex(para) == 2)
    {
        anchorPara = para;
        Shape shape = new Shape(doc, ShapeType.RECTANGLE);
        shape.setWidth(200);
        shape.setHeight(20);
        shape.setLeft(0);
        shape.setTop(0);
        shape.setWrapType(WrapType.SQUARE);
        anchorPara.appendChild(shape);
        break;
    }
}
doc.save(MyDir + "Out.docx");