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

Free Support Forum - aspose.com

Insert Image at any Specific Word Document Page using C# .NET | Watermark Image at Top Left Corner of Page

Hello there,

I would like to dynamically insert an image at a specific page in the document. The DocumentBuilder.InsertImage method works only for the first page, but I would like to let the user choose the target page at runtime. I also cannot use the header or footer sections, because I don’t want the image to appear on every page the footer or header corresponds to. At least there is the possibility to use bookmarks. My problem is, that the image should be placed at the very left page boundary. But I cannot set a bookmark to that position, because of the document’s margin.

So what do I need is the InsertImage method mentioned above, where I can insert a floating (absolutely positioned) image but with the ability to select the target page.
Do you have any idea, how I can achieve my purpose?

Best regards
Mario Wegner

@mariowegner,

Please try using the following code:

int pageIndex = 2;

Document doc = new Document("E:\\Temp\\input.docx");

Node[] runs = doc.GetChildNodes(NodeType.Run, true).ToArray();
for (int i = 0; i < runs.Length; i++)
{
    Run run = (Run)runs[i];
    int length = run.Text.Length;

    Run currentNode = run;
    for (int x = 1; x < length; x++)
    {
        currentNode = SplitRun(currentNode, 1);
    }
}

DocumentBuilder builder = new DocumentBuilder(doc);
PageSetup ps = builder.PageSetup;

NodeCollection smallRuns = doc.GetChildNodes(NodeType.Run, true);
LayoutCollector collector = new LayoutCollector(doc);

foreach (Run run in smallRuns)
{
    if (collector.GetStartPageIndex(run) == pageIndex)
    {
        builder.MoveTo(run);
        Shape watermark = builder.InsertImage("E:\\Temp\\HDTV1000_01.png");

        watermark.RelativeHorizontalPosition = RelativeHorizontalPosition.Page;
        watermark.RelativeVerticalPosition = RelativeVerticalPosition.Page;
        watermark.HorizontalAlignment = Aspose.Words.Drawing.HorizontalAlignment.Left;
        watermark.VerticalAlignment = VerticalAlignment.Top;
        watermark.WrapType = WrapType.None;

        break;
    }
}

doc.Save("E:\\temp\\20.4.docx"); 

Hope, this helps.

@awais.hafeez

Thank you for the fast answer. I copied the code to test it, but unfortunately I get an error, because the method SplitRun() is unknown:

Is there something missing?

@mariowegner,

Please use the following method:

private 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), (0) + (position));
    run.ParentNode.InsertAfter(afterRun, run);
    return afterRun;
}

Hope, this helps and sorry for the inconvenience.

@awais.hafeez

Now it works. I adapted your solution to use paragraphs instead of runs, so this solution now works for empty pages, too. I appreciate your help - thank very much.

Best regards

@mariowegner,

Thanks for your feedback. It is great that you were able to find what you were looking for. Please let us know any time you may have any further queries in future.