Method of Adding an Image to Each Page of a Document without using Header or Footer,
described at https://blog.aspose.com/2013/02/09/revealing-the-killer-new-feature-in-aspose.words-13.1.0-for-the-start-of-2013-native-access-to-page-layout-information
Thanks for being patient. Your document contains one paragraph and there is only one gigantic run inside that paragraph which spans across multiple pages. This is a very rare case. However, you can try executing code like below to workaround this problem:
Document doc = new Document(@"C:\Temp\TestFile.doc");
DocumentBuilder builder = new DocumentBuilder(doc);
if (SplitRunsAcrossPages(doc) > 0)
{
LayoutCollector layoutCollector = new LayoutCollector(doc);
NodeCollection runs = doc.GetChildNodes(NodeType.Run, true);
Run anchorNode;
int pageIndex = 1;
foreach (Run run in runs)
{
if (layoutCollector.GetStartPageIndex(run) == pageIndex)
{
anchorNode = run;
builder.MoveTo(anchorNode);
builder.InsertImage(@"C:\Temp\Aspose.Words.png", RelativeHorizontalPosition.Page, 60,
RelativeVerticalPosition.Page, 60, -1, -1, WrapType.None); ;
pageIndex++;
}
}
doc.JoinRunsWithSameFormatting();
}
doc.Save(@"c:\temp\out.doc");
public static int SplitRunsAcrossPages(Document doc)
{
LayoutCollector layoutCollector = new LayoutCollector(doc);
ArrayList splitRunsStart = new ArrayList();
ArrayList splitRunsEnd = new ArrayList();
foreach (Run run in doc.GetChildNodes(NodeType.Run, true))
{
if (layoutCollector.GetNumPagesSpanned(run) > 0)
{
Run currentRun = run;
splitRunsStart.Add(run);
while (currentRun.Text.Length > 1)
currentRun = SplitRun(currentRun, 1);
splitRunsEnd.Add(currentRun);
}
}
return splitRunsStart.Count;
}
public static Run SplitRun(Run run, int position)
{
Run afterRun = (Run)run.Clone(true);
afterRun.Text = run.Text.Substring(position);
run.Text = run.Text.Substring(0, position);
run.ParentNode.InsertAfter(afterRun, run);
return afterRun;
}