Watermark - Problem when printing

Hey,

I am able to add a watermark on a Word document. But when i print the document, the watermark does not appear completely.

Here is the code i use :

using (Stream file = FileUtilities.GetFileStream(versionId))
{
	using (Stream output = new System.IO.MemoryStream())
	{
		Aspose.Words.Document doc = new Aspose.Words.Document(file);
		InsertWatermarkText(doc);
		
		doc.Save(output, Aspose.Words.SaveFormat.Docx);
	}
}



public void InsertWatermarkText(Aspose.Words.Document doc)
{
	Aspose.Words.Drawing.Shape watermark = new Aspose.Words.Drawing.Shape(doc, Aspose.Words.Drawing.ShapeType.TextPlainText);
	Aspose.Words.Drawing.Shape watermark2 = null;
	Aspose.Words.Drawing.Shape watermark3 = null;

	watermark.TextPath.Text = "First Line watermark text sample. \n Additional text for the second line that is not printed correctly when printed. azerty azerty.";

	watermark.Width = 500;
	watermark.Height = 30;


	switch (position)
	{
		case "Bottom":
			watermark.Rotation = 0;
			watermark.VerticalAlignment = Aspose.Words.Drawing.VerticalAlignment.Bottom;
			watermark.HorizontalAlignment = Aspose.Words.Drawing.HorizontalAlignment.Center;
			break;

		case "Left":
			watermark.Rotation = -90;
			watermark.VerticalAlignment = Aspose.Words.Drawing.VerticalAlignment.Center;
			watermark.HorizontalAlignment = Aspose.Words.Drawing.HorizontalAlignment.Left;
			break;
	}

	watermark.RelativeHorizontalPosition = RelativeHorizontalPosition.Page;
	watermark.RelativeVerticalPosition = RelativeVerticalPosition.Page;
	watermark.WrapType = WrapType.None;

	Aspose.Words.Paragraph watermarkPara = new Aspose.Words.Paragraph(doc);
	watermarkPara.ParagraphBreakFont.Size = 1;
	watermarkPara.AppendChild(watermark);
	InsertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HeaderPrimary);
	InsertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HeaderFirst);
	InsertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HeaderEven);
}


public static void InsertWatermarkIntoHeader(Aspose.Words.Paragraph watermarkPara, Aspose.Words.Section sect, HeaderFooterType headerType)
{
	Aspose.Words.HeaderFooter header = sect.HeadersFooters[headerType];

	if (header == null)
	{
		header = new Aspose.Words.HeaderFooter(sect.Document, headerType);
		sect.HeadersFooters.Add(header);
	}

	header.AppendChild(watermarkPara.Clone(true));
}

You can find a document exemple attached to this post.

Thanks for your help.

WatermarkedDocument.zip (16.7 KB)

@lisi

Thanks for your inquiry. Please call Document.UpdatePageLayout method before saving the PDF document. If you still face problem, attach the following resources here for testing:

  • Your input Word document.
  • Please attach the output PDF file that shows the undesired behavior.
  • Please create a standalone console application ( source code without compilation errors ) that helps us to reproduce your problem on our end and attach it here for testing.

As soon as you get these pieces of information ready, we will start investigation into your issue and provide you more information. Thanks for your cooperation.

PS: To attach these resources, please zip and upload them.

Hi Tahir,

I think you answered to the wrong topic.

I don’t convert the file to PDF.

I just add a watermark to a Word document. The watermark is added to the document without problem.

When i print the Word document after adding the watermark, the problem occurs. Meaning the watermark is not visible entirely on the printed document.

Do you have a printer ?

I uploaded the console app solution and the word document.

You need to print the document to see the problem.

WordDocument.zip (9.7 KB)
ConsoleApplication1.zip (5.3 MB)

@lisi

Thanks for sharing the detail. We have tested the scenario using the latest version of Aspose.Words for .NET 18.12 and have not found the shared issue. So, please upgrade to the latest version of Aspose.Words for .NET 18.12. We have attached the output document with this post for your kind reference. output.zip (9.9 KB)

I have the same problem with the document you attached (18.12.docx)
I tried to print your document with Office 365, Office 2016 and Office 2010.

Please see my new attachement where i take a screenshot of the print preview in Word.

Can you send me a screenshot of the print preview you have ?

Thanks

problem_when_printingJPG.zip (69.9 KB)

@lisi

Thanks for your inquiry. Please check the attached image of MS Word 2016 (64 bit).

Hi

Thanks for your answer.

If i choose the XPS printer like you did in the screenshot it’s OK the watermark is completely rendered.

Can you please try to select a “real” printer, are the two lines of watermark still correctly rendered ?

It may be due to the printer margins. How can i take those into acocunt ?
Is it possible to add the watermark at the top of footer section ?

In the code provided, if you choose the “Left” position for the watermark a similar issue occurs.

@lisi

Thanks for your inquiry. Please note that Aspose.Words mimics the behavior of MS Word. If you insert the same watermark in the header of document and set its position using MS Word, you will get the same output.

In your case, we suggest you please insert the watermark in the footer of document instead of header. We have attached the modified code and output document with this post for your kind reference. Code and Document.zip (9.1 KB)

Your code works fine for this document.

However on another document that i have attached here (WordDocument_2) , the last line of watermark is not completely visible.

Do you have an idea why ?WordDocument_2.zip (49.9 KB)

@lisi

Thanks for your inquiry. You are facing the expected behavior. In this case, we suggest you please set the watermark’s (shape) position as shown below to get the desired output.

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

watermark.TextPath.Text = "First Line watermark text sample. \n Additional text for the second line that is not printed correctly when printed. azerty azerty.\n";

watermark.TextPath.Size = 12;
watermark.Width = 500;
watermark.Height = 30;

watermark.Fill.Color = System.Drawing.Color.Red;
watermark.TextPath.FontFamily = "Arial"; ;
watermark.Left = 0;
watermark.Top = -doc.FirstSection.PageSetup.BottomMargin;

Hi Tahir

Thank you very much for all your answers.
It is working and your last reply helped me a lot understanding how to position the watermark.

I understand watermarks are “under” the document content (table, text or image). It is the normal Word behavior. Is there a way to place it upon the content with Aspose ?

Have a nice day.

@lisi

Thanks for your inquiry. Yes, you can place the watermark at any place on the page using Aspose.Words. Could you please share some more detail about your query along with expected output Word document? We will then provide you more information about your query.

I have added as attachment one example of document i need to watermark.

In the table there are blue cells that are rendered “upon” the watermark.

Is there any kind of layer management int the header in order to place the watermark on top ?

WordDocument_3.zip (30.5 KB)

code_watermark.zip (718 Bytes)

@lisi

Thanks for your inquiry. Please note that Aspose.Words mimics the behavior of MS Word. If you add the same watermark in your document, you will get the same output. However, you can achieve your requirement by inserting the watermark on each page of Word document instead of Header/Footer.

Following code example shows how to insert the watermark on each page of document. Hope this helps you.

public static void InsertWatermarkImageAtEachPage(Document doc, string watermarkImagePath)
{
    DocumentBuilder builder = new DocumentBuilder(doc);
    LayoutCollector collector = new LayoutCollector(doc);

    int pageIndex = 1;
    foreach (Section section in doc.Sections)
    {
        NodeCollection paragraphs = section.Body.GetChildNodes(NodeType.Paragraph, true);
        foreach (Paragraph para in paragraphs)
        {
            if (collector.GetStartPageIndex(para) == pageIndex)
            {
                builder.MoveToParagraph(paragraphs.IndexOf(para), 0);
                builder.StartBookmark("BM_Page" + pageIndex);
                builder.EndBookmark("BM_Page" + pageIndex);
                pageIndex++;
            }
        }
    }

    collector = new LayoutCollector(doc);
    LayoutEnumerator layoutEnumerator = new LayoutEnumerator(doc);

    const int PageRelativeY = 0;
    const int PageRelativeX = 0;

    foreach (Bookmark bookmark in doc.Range.Bookmarks)
    {
        if (bookmark.Name.StartsWith("BM_"))
        {
            Paragraph para = (Paragraph)bookmark.BookmarkStart.ParentNode;

            Shape textbox = new Shape(doc, Aspose.Words.Drawing.ShapeType.Image);

            textbox.Top = PageRelativeY;
            textbox.Left = PageRelativeX;
            textbox.ImageData.SetImage(watermarkImagePath);

            //Set the width height according to your requirements
            textbox.Width = 100;
            textbox.Height = 50;
            textbox.BehindText = false;

            para.AppendChild(textbox);

            textbox.RelativeHorizontalPosition = Aspose.Words.Drawing.RelativeHorizontalPosition.Page;
            textbox.RelativeVerticalPosition = Aspose.Words.Drawing.RelativeVerticalPosition.Page;

            bool isInCell = bookmark.BookmarkStart.GetAncestor(NodeType.Cell) != null;
            if (isInCell)
            {
                var renderObject = collector.GetEntity(bookmark.BookmarkStart);
                layoutEnumerator.Current = renderObject;

                layoutEnumerator.MoveParent(LayoutEntityType.Cell);
                RectangleF location = layoutEnumerator.Rectangle;

                textbox.Top = PageRelativeY - location.Y;
                textbox.Left = PageRelativeX - location.X;
            }
        }
    }
}

Hi

Thanks for your answer.
I am looking to add text as watermark.

Though your previous answers are enough to meet most of our requirements.

Thanks for all.

Have a nice day.

@lisi

Thanks for your feedback.

Are you still facing the issue? Please share your feedback. We will then answer your query accordingly.