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++;
}
}
}
Thanks a lot!
Am able to get watermark with my code.
The code u suggest will have test as watermark input where as am trying out with a image as input. More over the output format is PDF where as am generating a Word document as output. In the document the water alignment is changing from text to table or image. Please gave a look at my earlier document attached.
Thanks for your inquiry. It would be great if you please share following detail for investigation purposes.
Please attach your input Word document. If watermarktest.docx is your input, no need to share this 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. We 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.