简体   繁体   中英

How to insert a floating shape on each page of an Aspose.Words document?

I have an Aspose.Words document. I would like to insert a TextBox Shape on each page of my document.

This is my document:

 // Aspose.Words document
 Document document = new Document();

 // The DocumentBuilder
 DocumentBuilder builder = new DocumentBuilder(document);

 builder.insertHtml("Here, there is a big String with a lot of HTML.");

This is how I do it for the first page:

 Shape textBox = new Shape(document, ShapeType.TEXT_BOX);
 textBox.setWrapType(WrapType.SQUARE);
 // Shape position
 textBox.setDistanceTop(0);
 textBox.setDistanceLeft(42);
 // Shape dimensions
 textBox.setWidth(200);
 textBox.setHeight(20);

 // ... other options useless here.

 // Paragraph, content of the Shape
 Paragraph paragraph = new Paragraph(document);
 Run run = new Run(document);
 run.setText("Here some text.");
 paragraph.appendChild(run);

 textBox.appendChild(paragraph);

 // Now I insert my Shape on the first page.
 builder.moveToDocumentStart();
 builder.insertNode(textBox);

This is working great for the first page.

I also know that I can get the number of pages doing:

 document.getPageCount();

However I do not know how to go through all the pages.

Any suggestions?

my name is Nayyer and I am support developer/technical evangelist at Aspose.

If you need something to display on all pages, then you must have to move that content in Headers/Footers area. Please try following the approach similar to How to add Watermark to document . Please note that content/object can be placed anywahere on the page but in order to display something on all pages, the content should just be anchored inside header/footers.

Now concerning to specifying the location for shape, please note that shapes have RelativeHorizontalPosition property and if you set this to RelativeHorizontalPosition.Page and then set the Left and Top positions, you can float this anywhere at the center or even at the bottom of Page (Even though it is anchored inside Header).

PS, Ms Word just repeats whatever is inside header/footer for every Page at run-time, but of course there is only a single copy of content.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM