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

Free Support Forum - aspose.com

Place image on a certain page

I’m trying to place an image on a certain page, in word interop I would do:

document.ActiveWindow.Selection.GoTo(WdGoToItem.wdGoToPage, WdGoToDirection.wdGoToFirst, PageNumber)
and then place my image.

How would I accomplish this with Aspose Words? I tried the DocumentBuilder class but this has no option to navigate to a certain page.

Thank you.

Found the answer. I used the LayoutCollector class using the GetStartPageIndex to determine on which page the node was at then just placed the image on the node which matched the page number.


But I noticed that once I call the GetStartPageIndex my Document class does not display the changes I make to it when I save it. I have to save and load the Document after I call the GetStartPageIndex and then make my changes and save it that way.

Something like this, in C#:
var doc = new Document(@“SOMEPATH”);

var layoutCollector = new LayoutCollector(doc);
int index = 0;
foreach (Paragraph p in doc.SelectNodes("//Body/Paragraph"))
{
if (layoutCollector.GetStartPageIndex§ == PAGENUMBER)
{
break;
}
index++;
}

using (var str = new MemoryStream())
{
doc.Save(str, SaveFormat.Docx);

doc = new Document(str);

}

var builder = new DocumentBuilder(doc);
builder.MoveTo(doc.SelectNodes("//Body/Paragraph")[index]);
builder.InsertImage(@“MYIMAGE”);

Hi Isidro,


Thanks for your inquiry. Please use the following code snippet to achieve your requirements. I suggest you please read following documentation links.
http://www.aspose.com/docs/display/wordsnet/LayoutCollector+Class
http://www.aspose.com/docs/display/wordsnet/LayoutEnumerator+Class


Document doc = new
Document(MyDir + “Sample.docx”);<o:p></o:p>

LayoutCollector layoutCollector = new LayoutCollector(doc);

LayoutEnumerator layoutEnumerator = new LayoutEnumerator(doc);

int PAGENUMBER = 2;

int index = 0;

foreach (Paragraph p in doc.SelectNodes("//Body/Paragraph"))

{

var renderObject = layoutCollector.GetEntity(p);

layoutEnumerator.Current = renderObject;

RectangleF location = layoutEnumerator.Rectangle;

int page = layoutEnumerator.PageIndex;

Console.WriteLine(page.ToString());

if (page == PAGENUMBER)

{

break;

}

index++;

}

var builder = new DocumentBuilder(doc);

builder.MoveTo(doc.SelectNodes("//Body/Paragraph")[index]);

builder.InsertImage("http://www.aspose.com/images/aspose-logo.gif");

doc.Save(MyDir + "out.docx");


Moreover, please check DocumentLayoutHelper and EnumerateLayoutElements projects sample from the offline samples pack. This sample demonstrates how to easily work with the layout elements of a document and access the pages, lines, spans etc.

Hope this helps you. Please let us know if you have any more queries.