Incorrect method of Adding an Image to Each Page of a Document without using Header or Footer in samples

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

and in Samples https://releases.aspose.com/words/net** (project name is AddImageToEachPage)

is incorrect.

Contrary instance is in attachment

I would like to get feedback from developers,
can you reproduce this issue?

Hi Alex,

Thanks for your inquiry. We are checking with this scenario and will get back to you soon.

Best regards,

Hello, Aspose Team,

could you tell me what progress is there with this issue?

Best regards,
Alex

Hi Alex,

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;
}

I hope, this helps.

Best regards,

Thank you for your help, I`ll try it.

Best regards