Hello,
I’m trying to add watermark to a Word file, watermark gets added but is not visible if there is an image on the page. the watermark is behind the image. I want it to be visible all the time no matter what content the word document has.
I’ve tried setting ZOrder to 1000 but it did not work.
Also opacity is not working. it is not changing the opacity level.
Sub InsertWatermarkText(ByVal doc As Aspose.Words.Document, ByVal watermarkText As String)
’ Create a watermark shape. This will be a WordArt shape.
’ You are free to try other shape types as watermarks.
'Dim watermark As New Aspose.Words.Drawing.Shape(doc, Aspose.Words.Drawing.ShapeType.TextPlainText)
Dim watermark As New Aspose.Words.Drawing.Shape(doc, Aspose.Words.Drawing.ShapeType.Image)
’ Set up the text of the watermark.
'watermark.TextPath.Text = watermarkText
watermark.ImageData.SetImage(docDir & "watermark.jpg")
watermark.TextPath.FontFamily = "Arial"
watermark.Width = 200
watermark.Height = 200
’ Text will be directed from the bottom-left to the top-right corner.
watermark.Rotation = -40
’ Remove the following two lines if you need a solid black text.
watermark.Fill.Color = System.Drawing.Color.Blue ’ Try LightGray to get more Word-style watermark
watermark.StrokeColor = System.Drawing.Color.Gray ’ Try LightGray to get more Word-style watermark
watermark.AllowOverlap = True
watermark.BehindText = False
watermark.Fill.Opacity = 0.5
watermark.ZOrder = 1000
’ Place the watermark in the page center.
watermark.RelativeHorizontalPosition = RelativeHorizontalPosition.Page
watermark.RelativeVerticalPosition = RelativeVerticalPosition.Page
watermark.WrapType = WrapType.None
watermark.VerticalAlignment = Aspose.Words.Drawing.VerticalAlignment.Center
watermark.HorizontalAlignment = Aspose.Words.Drawing.HorizontalAlignment.Center
’ Create a new paragraph and append the watermark to this paragraph.
Dim watermarkPara As New Aspose.Words.Paragraph(doc)
watermarkPara.AppendChild(watermark)
’ Insert the watermark into all headers of each document section.
For Each sect As Section In doc.Sections
’ There could be up to three different headers in each section, since we want
’ the watermark to appear on all pages, insert into all headers.
InsertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HeaderPrimary)
InsertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HeaderFirst)
InsertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HeaderEven)
Next sect
End Sub
Thanks for your inquiry. I have tested the scenario and have not found any issue with watermark. The code you are using is correct. Please make sure that the image path is correct.
thank for your response. I think you misunderstood my problem. The watermark is getting added as expected but it is not visible if there is already an image on the page. The watermark is behind the image.
I want the watermark to be visible all the time irrespective of pdf page content.
Thanks for your inquiry. It would be great if you please share
following detail for investigation purposes.
Please attach your input Word document.
Please
create a standalone/runnable simple application (for example a Console
Application Project) that demonstrates the code you used to generate
your output document
Please attach the output Word/Pdf file that shows the undesired behavior.
Unfortunately,
it is difficult to say what the problem is without the Document(s) and
simplified application. We need your Document(s) and simple project to
reproduce the problem. As soon as you get these pieces of information to
us we’ll start our investigation into your issue.
Thanks for your inquiry. The problem occurs because watermark shape resides inside header footer story and images are inside body story (please see Story class). If you insert a watermark using Microsoft Word 2013 in source document, you will observe the same behaviour. However, you can overcome this problem by manually inserting watermarks in each Page. You can achieve this by moving the cursor to the first Paragraph in each Page of your document and then making those Paragraphs as an anchor points for your watermarks. Here is how you can achieve this:
Do the same trick will work on JAVA?
i converted the code in java, after this change, the bookmark is not visible at all on any page.
Thanks in advance…
public static void InsertWatermarkTextAtEachPage(Document doc, String watermarkText) throws Exception
{
DocumentBuilder builder = new DocumentBuilder(doc);
PageSetup ps = builder.getPageSetup();
NodeCollection paragraphs = doc.getChildNodes(NodeType.PARAGRAPH, true);
LayoutCollector collector = new LayoutCollector(doc);
Paragraph anchorPara = null;
int pageIndex = 1;
for (int i=0; i<paragraphs.getCount();i++)
{
Paragraph para = (Paragraph) paragraphs.get(i);
// System.out.println(collector.getStartPageIndex(para) );
if ( collector.getStartPageIndex(para) == pageIndex && para.getAncestor(NodeType.GROUP_SHAPE) == null)
{
anchorPara = para;
Shape watermark = new Shape(doc, ShapeType.TEXT_PLAIN_TEXT);
watermark.getTextPath().setText(watermarkText);
watermark.getTextPath().setFontFamily("Arial");
watermark.setWidth(500);
watermark.setHeight(100);
// Text will be directed from the bottom-left to the top-right corner.
watermark.setRotation(-40);
// Remove the following two lines if you need a solid black text.
watermark.getFill().setColor(Color.GRAY); // Try LightGray to get more Word-style watermark
watermark.setStrokeColor(Color.GRAY); // Try LightGray to get more Word-style watermark
// Place the watermark in the page center.
watermark.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);
watermark.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);
watermark.setWrapType(WrapType.NONE);
watermark.setVerticalAlignment(VerticalAlignment.CENTER);
watermark.setHorizontalAlignment(HorizontalAlignment.CENTER);
// watermark.setName(String.format("WaterMark_{0}", UUID.randomUUID().toString()));
// watermark.setWrapType(WrapType.NONE);
anchorPara.appendChild(watermark);
pageIndex++;
}
}
}
Thanks for your inquiry. I have tested the scenario and have not found the shared issue. It would be great if you please share following detail for investigation purposes.
Please attach your input Word document.
Please attach the output Word file that shows the undesired behavior.
Please
attach your target Word document showing the desired behavior. You can
use Microsoft Word to create your target Word document. I will
investigate as to how you are expecting your final document be generated
like.
As soon as you get these pieces of information to
us we’ll start our investigation into your issue.
Hi,
Thanks for reply…
Actually i misunderstood the behaviour, the waterMark is getting added in DOC but when i save it to PDF format the water mark is not visible.
The Water Mark is visible if i save Document object to DOC format.
com.aspose.words.Document lWordDocument = new com.aspose.words.Document("Data/sourceLarge.doc");
InsertWatermarkTextAtEachPage(lWordDocument, "WATERMARKED");
// lWordDocument.save("Data/output/OutPut.doc"); // This file is showing watermark
lWordDocument.save("Data/output/OutPut.pdf",SaveFormat.PDF); // This file is not showing any watermarked
ALSO,
In OutPut.doc file the Water Mark is not properly inserted on page 3.
I Have Attached the Source.doc(Source File) and both output files.
Please call the Document.updatePageLayout method before saving your Pdf document. This will solve the issue which you are facing.
This method is automatically invoked when you first convert a document to PDF, XPS, image or print it. However, if you modify the document after rendering and then attempt to render it again - Aspose.Words will not update the page layout automatically. In this case you should call UpdatePageLayout before rendering again.
Thanks for your inquiry. A valid Word document must contain a Paragraph. Could you please attach your input Word document here for testing? We will investigate the issue on our side and provide you more information.
Thanks for sharing the document. Perhaps, you are using an older version of Aspose.Words; as with Aspose.Words v15.12.0, we are unable to reproduce the shared issue on our side. Please use Aspose.Words for Java 15.12.0.
We noticed that the latest version of Aspose.Words for Java does not render the watermark correctly in output Pdf. We logged this issue as WORDSJAVA-1265. Please check the attached output Pdf. You will be notified via this forum thread once this issue is resolved. We apologize for your inconvenience.
Thanks for sharing the detail. We have tested the scenario using latest version of Aspose.Words for .NET 15.12.0 and have not found the shared issue. Please use Aspose.Words for .NET 15.12.0 and following code example to get the required output. Hope this helps you.
Document doc = new Document(MyDir + "source.doc");
InsertWatermarkTextAtEachPage(doc, "This is a watermark");
ImageSaveOptions imgOptions = new ImageSaveOptions(SaveFormat.Png);
doc.UpdatePageLayout();
imgOptions.PageCount = 1;
imgOptions.Resolution = 300;
for (int i = 0; i < doc.PageCount; i++)
{
imgOptions.PageIndex = i;
doc.Save(MyDir + "Out" + i + ".png", imgOptions);
}
public static void InsertWatermarkTextAtEachPage(Document doc, string watermarkText)
{
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 = 400;
watermark.Height = 100;
watermark.Left = 100;
watermark.Top = 100;
watermark.Rotation = -40;
watermark.Fill.Color = Color.Gray;
watermark.StrokeColor = Color.Gray;
watermark.Name = string.Format("WaterMark_{0}", Guid.NewGuid());
watermark.WrapType = WrapType.None;
anchorPara.AppendChild(watermark);
pageIndex++;
}
}
}