Free Support Forum - aspose.com

Avoid Misaligned Watermark to Front of Word Document's Content by using "Is Layout In Cell" Property of Shape Class C# .NET

Hey,

When I try insert watermark to the front of document, in certain pages my watermark looks misaligned…Can you please help me with this?? I have attached the doc file within this…The watermark in Page 22 is misaligned.

I have used following code to bring watermark to the front of document.

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

            int pageIndex = collector.GetStartPageIndex(smallRuns[0]);
            foreach (Run run in smallRuns)
            {
                if (collector.GetStartPageIndex(run) == pageIndex)
                {
                   
                    var watermark = CreateWatermark(watermarkText, doc, false).Clone(true);
                    builder.MoveTo(run);
                    builder.CurrentSection.PageSetup.RestartPageNumbering = false;
                    builder.InsertNode(watermark);

                    pageIndex++;
                }
            }

and this for creating watermark

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

        // Set up the text of the watermark.
        watermark.TextPath.Text = watermarkText;
        watermark.TextPath.FontFamily = "Arial";
        watermark.Width = 500;
        watermark.Height = 100;

        // Text will be directed from the bottom-left to the top-right corner.
        watermark.AllowOverlap = true;
        watermark.Rotation = -40;

        // Remove the following two lines if you need a solid black text.
        watermark.Fill.Color = System.Drawing.Color.LightGray;
        watermark.StrokeColor = System.Drawing.Color.LightGray; // Try LightGray to get more Word-style watermark
        watermark.BehindText = behindText;

        // Place the watermark in the page center.
        watermark.RelativeHorizontalPosition = RelativeHorizontalPosition.Page;
        watermark.RelativeVerticalPosition = RelativeVerticalPosition.Page;
        watermark.WrapType = WrapType.None;
        watermark.VerticalAlignment = VerticalAlignment.Center;
        watermark.HorizontalAlignment = HorizontalAlignment.Center;

        // Create a new paragraph and append the watermark to this paragraph.

        return watermark;

WatermarkedDoc.zip (2.7 MB)

@neethuan,

It seems that you have generated the PDF file (WatermarkedDoc.pdf) by using a very old 18.10 version of Aspose.Words for .NET API on your end. Have you also tried the latest (20.8) version of Aspose.Words for .NET on your end?

In case the problem still remains, please ZIP and attach the following resources here for testing:

  • Your simplified input Word document you are getting this problem with
  • Please also create a standalone simple Console Application (source code without compilation errors) that helps us to reproduce your current problem on our end and attach it here for testing. Please do not include Aspose.Words DLL files in it to reduce the file size.

As soon as you get these pieces of information ready, we will start further investigation into your scenario and provide you more information.

@awais.hafeez…Thanks for the response…I have tried that with the latest version as well. But the issue still exists.I am attaching the zipped folder containing input pdf and the console application for further investigation within this.

Thanks in advanceConsoleApplication5.zip (9.3 MB)

@neethuan,

You can build logic on the following C# code that adds watermark text to the front of all PDF pages:

Document doc = new Document("C:\\Temp\\ConsoleApplication5\\input.pdf");

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

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

ArrayList pagesWithNoRuns = new ArrayList();
ArrayList firstRunsOnPages = new ArrayList();
for (int i = 1; i <= doc.PageCount; i++)
{
    bool flag = false;
    foreach (Run run in smallRuns)
    {
        if (collector.GetStartPageIndex(run) == i)
        {
            firstRunsOnPages.Add(run);
            flag = true;
            break;
        }
    }

    if (!flag)
        pagesWithNoRuns.Add(i);
}

NodeCollection shapes = doc.GetChildNodes(NodeType.Shape, true);
ArrayList firstShapesOnPages = new ArrayList();
for (int i = 1; i <= doc.PageCount; i++)
{
    foreach (Shape shape in shapes)
    {
        if (collector.GetStartPageIndex(shape) == i)
        {
            firstShapesOnPages.Add(shape);
            break;
        }
    }
}

Shape watermark = CreateWatermark("Confidential", doc, false);
foreach (Run run in firstRunsOnPages)
{
    builder.MoveTo(run);
    builder.InsertNode(watermark.Clone(true));
}

foreach (Shape shape in firstShapesOnPages)
{
    builder.MoveTo(shape);
    builder.InsertNode(watermark.Clone(true));
}

doc.UpdatePageLayout();
doc.Save("C:\\Temp\\ConsoleApplication5\\20.8.pdf");

private static Shape CreateWatermark(string watermarkText, Document doc, bool behindText = true)
{
    Shape watermark = new Shape(doc, Aspose.Words.Drawing.ShapeType.TextPlainText);
    watermark.ZOrder = 2;

    watermark.TextPath.Text = watermarkText;
    watermark.TextPath.FontFamily = "Arial";
    watermark.Width = 500;
    watermark.Height = 100;

    watermark.AllowOverlap = true;
    watermark.Rotation = -40;

    watermark.BehindText = behindText;

    watermark.RelativeHorizontalPosition = RelativeHorizontalPosition.Page;
    watermark.RelativeVerticalPosition = RelativeVerticalPosition.Page;
    watermark.WrapType = WrapType.None;
    watermark.VerticalAlignment = VerticalAlignment.Center;
    watermark.HorizontalAlignment = HorizontalAlignment.Center;

    return watermark;
}

@awais.hafeez…Thanks for the response…But unfortunately my watermark still looks misaligned.I guess the problem is when we try to watermark the pages that contain tables…
image.png (105.0 KB)
See the image

@neethuan,

Please check we used this as an input PDF file (input.pdf (2.5 MB)) and then the code from my previous post generated this output PDF file (20.8.pdf (2.7 MB)) showing the correct Watermark text on all pages.

Yes, we can observe the watermark text misalignment on 22nd page of WatermarkedDoc.pdf that you shared earlier in this post. Can you please ZIP and attach the input Word / PDF document you had generated WatermarkedDoc.pdf from here for further testing?

Confidential.pdf (2.8 MB)

Please find the input document

@neethuan,

Please specify ‘false’ to ShapeBase.IsLayoutInCell Property to get the desired output:

...
...
Shape watermark = CreateWatermark("Confidential", doc, false);
watermark.IsLayoutInCell = false;
foreach (Run run in firstRunsOnPages)
{
    builder.MoveTo(run);
    builder.InsertNode(watermark.Clone(true));
}
...
...

I have attached the output PDF file here for your referece:

@awais.hafeez…Thanks for investigating…
I have tried that as well, but didn’t give any difference…Also in your output file I can see there is a page break in page 24, is the same code used to generate that?? Please see the attached output pdfconfidentail with layoutincell false.pdf (2.8 MB)

@neethuan,

Yes, the same codes from here and here were used to watermark PDF file on our end. Even running the following code of 20.8 version of Aspose.Words for .NET API against these PDF files (source and output pdf files.zip (5.2 MB)) cause additional pages i.e. source PDF file (Confidential.pdf) contains 25 pages but Aspose.Words produces 27 pages in output PDF.

Document doc = new Document("C:\\Temp\\Confidential.pdf");
doc.Save("C:\\Temp\\20.8.pdf");

For the sake of correction, we have logged this problem in our issue tracking system. The ID of this issue is WORDSNET-20962. We will further look into the details of this problem and will keep you updated on the status of correction. We apologize for your inconvenience.

@awais.hafeez, Thanks for looking into it… Is that possible to look into that issue?? Is there any URL for that issue?

2 posts were split to a new topic: Adding Text Stamp Watermark to PDF

@neethuan,

I am afraid, there is no direct way that you can use to interact with the issue tracking system. But, you are welcome to ask your issue status via this forum thread. We will verify the status from our internal issue tracking system and reply you about the progress. Currently, the fix of WORDSNET-20962 is in development phase. We will notify you here as soon as this issue will get resolved in future.

1 Like

The issues you have found earlier (filed as WORDSNET-20962) have been fixed in this Aspose.Words for .NET 20.9 update and this Aspose.Words for Java 20.9 update.