Watermark visibility in top of page and some of the pages displayed twice

Hi,

we are using aspose-word licensed version 14.12.0.0. I would like to integrate a watermark into a word document. We are facing two types of issue.

  1. Watermark is shown on all pages in top of page.
  2. Some of the pages watermark displayed in twice.
    First issue, I need watermark is shown center of the pages and second issue, only one watermark shown in the pages instead of twice.

Is there any way to avoid this behavior. How can i achieve this?. Also we attached the same project and screenshot for your review and process to quick solution.

Note: Please include the dll aspose-word version 14.12.0.0.

We are using following code for watermark.

Aspose.Words.Document doc = new Aspose.Words.Document(outStream);

        DocumentBuilder builder = new DocumentBuilder(doc);

        PageSetup ps = builder.PageSetup;


        NodeCollection paragraphs = doc.GetChildNodes(NodeType.Paragraph, true);

        LayoutCollector collector = new LayoutCollector(doc);

        Paragraph anchorPara = null;


        int pageIndex = 1;

        foreach (Paragraph para in paragraphs)

            if (collector.GetStartPageIndex(para) == pageIndex && para.GetAncestor(NodeType.GroupShape) == null)

            {

                anchorPara = para;

                Shape watermark = new Shape(doc, Aspose.Words.Drawing.ShapeType.TextPlainText);


                watermark.TextPath.Text = watermarkText;

                watermark.TextPath.FontFamily = "Arial";


                watermark.Width = 300;

                watermark.Height = 70;

                watermark.Left = 100;

                watermark.Top = 100;


                watermark.Rotation = -40;


                watermark.Fill.Color = Color.Gray;

                watermark.StrokeColor = Color.Gray;

                watermark.WrapType = WrapType.None;

                anchorPara.AppendChild(watermark);

                pageIndex++;

            }

        doc.Save(outStream, SaveFormat.Docx);

WaterMarkIssue.zip (458.4 KB)
WaterMark.jpg (50.5 KB)
WaterMark1.jpg (46.1 KB)

@smani,

We are working on your query and will get back to you soon.

@smani,

We tested the scenario and have managed to reproduce the same problems on our end. For the sake of corrections, we have logged these problems in our issue tracking system. Your ticket number is WORDSNET-18192. We will further look into the details of these problems and will keep you updated on the status of corrections. We apologize for your inconvenience.

@smani,

Please use the following code to get the desired output:

Document doc = new Document("E:\\Water-Mark-Word.docx");
doc.CompatibilityOptions.OptimizeFor(MsWordVersion.Word2010);

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

int pageIndex = 1;
foreach (Run run in smallRuns)
{
    if (collector.GetStartPageIndex(run) == pageIndex)
    {
        Shape watermark = new Shape(doc, Aspose.Words.Drawing.ShapeType.TextPlainText);
        watermark.IsLayoutInCell = false;

        watermark.RelativeHorizontalPosition = RelativeHorizontalPosition.Page;
        watermark.RelativeVerticalPosition = RelativeVerticalPosition.Page;

        watermark.Width = 300;
        watermark.Height = 70;
        watermark.HorizontalAlignment = Aspose.Words.Drawing.HorizontalAlignment.Center;
        watermark.VerticalAlignment = VerticalAlignment.Center;

        watermark.Rotation = -40;
        watermark.Fill.Color = Color.Gray;
        watermark.StrokeColor = Color.Gray;

        watermark.TextPath.Text = "watermarkText";
        watermark.TextPath.FontFamily = "Arial";

        watermark.Name = string.Format("WaterMark_{0}", Guid.NewGuid());
        watermark.WrapType = WrapType.None;

        builder.MoveTo(run);
        builder.InsertNode(watermark);

        pageIndex++;
    }
}

doc.Save("E:\\19.2.doc"); 

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

Output document is also attached: 19.2.zip (61.7 KB)

Hi awais.hafeez,

After upgraded latest version both word and pdf 19.4. I am changed the above code you shared and tested local environment, still i am facing same issues and some of the pages displayed twice. Aspose latest version also i am facing issue. How can i achieve this?

Also any update on this ticket WORDSNET- 18192.

@smani,

We had closed WORDSNET-18192 with “Not a Bug” status and provided you workaround code to achieve what you were looking for.

Please ZIP and upload your new Word document (you are getting this problem with) here for testing. We will then investigate the issue further on our end and provide you more information.