We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Adding Watermark to Document

Hi


I am using Aspose,Word to convert image to pdf.
In Addition I want to add watermark that will appear in all pages,

To add the watermark I added Shape to the Document.
The shape has been added properly, however the image cover some part of the watermark.
Can you assist me find how to make image behind the watermark?

this is the code i wrote for adding the stamp:

Aspose.Words.Drawing.Shape watermark = new Aspose.Words.Drawing.Shape(doc, Aspose.Words.Drawing.ShapeType.TextPlainText);
watermark.TextPath.Text = watermarkText;
// Create a new paragraph and append the watermark to this paragraph.
Paragraph watermarkPara = new Paragraph(doc);
watermarkPara.AppendChild(watermark);

this is the code i used to add image:
builder.InsertImage(image, RelativeHorizontalPosition.Page, leftPos, RelativeVerticalPosition.Page, tmpTopMargin + 20, fixedSize.Width, fixedSize.Height, WrapType.Through);

sample attached

Thx
Yaniv

Hi Yaniv,


Thanks for your inquiry. In your case, we suggest you please insert watermark in each page of document. Please use the following code example to achieve your requirement. Hope this helps you.


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 = 0;

foreach (Paragraph para in paragraphs)

{

if (collector.GetStartPageIndex(para) == pageIndex)

{

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.Name = string.Format(“WaterMark_{0}”, Guid.NewGuid());

watermark.WrapType = WrapType.None;

anchorPara.AppendChild(watermark);

pageIndex++;

}

}

}



Hi,


Thanks for your answer

the solution is not good, since the watermake is not at the center of the page.
and the text still apear below to image

see screenshot attached

Thx
Yaniv

Thanks


The last soultion is much better but it still has defect.
In case the document has more than 1 page then the watermark appear only on the last page.

sample attached

Hi Yaniv,


Thanks for your inquiry. Could you please attach your input Word document here for testing? We will investigate the issue on our side and provide you more information.

Here are the steps I am doing:

  1. Load template.doc
  2. Add tiff image into it
  3. Add watermark
  4. save as PDF

i am using the below code to add the image to the template

internal void AddFixedImageToCenterOfPage(DocumentBuilder builder, Stream sourceImageStream, int pageWidth, int pageHeight, bool enableHeaderAndFooter)
{
int tmpTopMargin = TopMargin;
int tmpBottomMargin = BottomMargin;
if (enableHeaderAndFooter == false)
{
tmpTopMargin = 5; tmpBottomMargin = 5;
}
pageWidth -= (LeftMargin + RightMargin);
pageHeight -= (tmpTopMargin + tmpBottomMargin);

List sourceImage = GetAllPages(sourceImageStream);
int pageCount = sourceImage.Count - 1;
foreach (var image in sourceImage)
{
// Get fixedSize with proprotion which match to the page size
var fixedSize = GetFixedSize(image, pageWidth, pageHeight);
// Add the image at the middle of the page
int leftPos = ((int)builder.PageSetup.PageWidth - fixedSize.Width) / 2;
var shape = builder.InsertImage(image, RelativeHorizontalPosition.Page, leftPos, RelativeVerticalPosition.Page, tmpTopMargin + 20, fixedSize.Width, fixedSize.Height, WrapType.Through);
// Add each image in different page
if (pageCount > 0)
{
Run pageBreakRun = new Run(builder.Document, ControlChar.PageBreak);
builder.Document.LastSection.Body.LastParagraph.AppendChild(pageBreakRun);
}
pageCount–;
}

}


Attached the template and the tiff
Thanks
Yaniv

Hi Yaniv,

Thanks for your inquiry. Please call Document.UpdatePageLayout method before saving document to Pdf.

We have tested the scenario using latest version of Aspose.Words for .NET 16.11.0 and following code example. We have not found the shared issue. Please use Aspose.Words for .NET 16.11.0. We have attached the output Pdf with this post for your kind reference.

Document doc = new Aspose.Words.Document(MyDir + "template.docx");

DocumentBuilder builder = new DocumentBuilder(doc);

builder.InsertImage(MyDir + "196482993.TIF");

builder.Writeln();

builder.InsertImage(MyDir + "196482993.TIF");

InsertWatermarkAtEachPage(doc, "Watermark");

doc.UpdatePageLayout();

doc.Save(MyDir + "Out v16.11.0.pdf");

I apricate your help but i still see the watermark only on the last page.


please note that the tiff contains 2 pages.
at your sample i will see only the first page.

you can use this method to get all images of the TIFF file:

private List GetAllPages(Stream stream)
{
List images = new List();
Bitmap bitmap = (Bitmap)Image.FromStream(stream);
int count = bitmap.GetFrameCount(FrameDimension.Page);
for (int idx = 0; idx < count; idx++)
{
// save each frame to a bytestream
bitmap.SelectActiveFrame(FrameDimension.Page, idx);
MemoryStream byteStream = new MemoryStream();
bitmap.Save(byteStream, ImageFormat.Tiff);

// and then create a new Image from it
images.Add(Image.FromStream(byteStream));
}
return images;
}

Hi Yaniv,

Thanks for your inquiry. The code shared in this forum thread inserts the watermark (Shape node) in the first paragraph of each page. Your input document contains only one paragraph in the section's body. So the multiple images are inserted in the same paragraph and watermark is not visible on first page.

In your case, we suggest you please insert the paragraph break after inserting the image. Hope this helps you.

var shape = builder.InsertImage(image, RelativeHorizontalPosition.Page, leftPos, RelativeVerticalPosition.Page, tmpTopMargin + 20, fixedSize.Width, fixedSize.Height, WrapType.Through);

builder.InsertBreak(BreakType.ParagraphBreak);

Hi Tahir


Thanks for you help. Its indeed resolve the issue.
However, if the TIFF document contains 3 images, then the watermark appears only at the first page and last page.

attached TIFF document, and PDF with the missing watermark

Thx
Yaniv
Hi Yaniv,

Thanks for your inquiry. We have tested the scenario using latest version of Aspose.Words for .NET 16.11.0 with following code example and have not found the shared issue. Please use Aspose.Words for .NET 16.11.0. We have attached the output Pdf with this post for your kind reference.

If you still face problem, 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. We will investigate the issue on our side and provide you more information.

Document doc = new Aspose.Words.Document(MyDir + "template.docx");

DocumentBuilder builder = new DocumentBuilder(doc);

List<Image> sourceImage = GetAllPages(new MemoryStream(File.ReadAllBytes(MyDir + "3_pages.tif")));

int pageCount = sourceImage.Count - 1;

foreach (var image in sourceImage)

{

var shape = builder.InsertImage(image);

builder.InsertBreak(BreakType.ParagraphBreak);

}

InsertWatermarkAtEachPage(doc, "Watermark");

doc.UpdatePageLayout();

doc.Save(MyDir + "Out v16.11.0.pdf");

Hi Tahir

I think the defect related to the fact that i dont have licence for Aspose.Word 16.11
Casue the first page in the PDF document has red Disclaimer "Evaluation Only...."

Could you please send me temporary lic?

Thx
Yaniv

Hi Yaniv,


Thanks for your inquiry. Please request for 30-days temporary license from here:

Get temporary license

Hi Tahir


Thanks for you help.
When i tried your code on my machine its indeed work - the pdf docs created with watermark
The thing is, that i want to add the image at the center of the screen thats why i am not using the metthod:
builder.InsertImage(image);
I am using the method:
builder.InsertImage(image, RelativeHorizontalPosition.Page, leftPos, RelativeVerticalPosition.Page, tmpTopMargin + 20, fixedSize.Width, fixedSize.Height, WrapType.None);

this change cause to watermark to appear incorrectly.
my code is attached

Yhx
Yaniv

Hi Yaniv,


Thanks for your inquiry. Shape.ZOrder property determines the display order of overlapping shapes. Please set its value to “2” and use the following modified InsertWatermarkAtEachPage method to insert the image in the middle of a page. We have attached the output document and modified code with this post for your kind reference.

public static void InsertWatermarkAtEachPage(Document doc, string
watermarkText)<o:p></o:p>

{

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)

{

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

watermark.TextPath.Text = watermarkText;

watermark.TextPath.FontFamily = "Arial";

//Set the width height according to your requirements

watermark.Width = 600;

watermark.Height = 60;

watermark.BehindText = false;

watermark.Rotation = -40;

watermark.Fill.Color = Color.Gray;

watermark.StrokeColor = Color.Gray;

para.AppendChild(watermark);

watermark.RelativeHorizontalPosition = Aspose.Words.Drawing.RelativeHorizontalPosition.Page;

watermark.RelativeVerticalPosition = Aspose.Words.Drawing.RelativeVerticalPosition.Page;

watermark.HorizontalAlignment = HorizontalAlignment.Center;

watermark.VerticalAlignment = VerticalAlignment.Center;

watermark.ZOrder = 2;

pageIndex++;

}

}

}

}

1 Like

Thanks for you help.


I copy your new code, and comment out this condition

//if (collector.GetStartPageIndex(para) == pageIndex)

and its resolve the issue!!!
Now I can see the watermark on all pages

Can you tell me why we need this condition?

Thx
Yaniv

Hi Yaniv,


Thanks for your inquiry. If you comment out the condition, watermark (Shape node) will be added to each paragraph of document. The InsertWatermarkAtEachPage inserts the watermark (Shape node) in the first paragraph of each page. So, please do not comment out the condition.

Nevertheless, I removed this line and the PDF document looks fine.

Without this line the watermark does not display on the second page (2 of 3)

Hi Yaniv,


Thanks for your inquiry. Please check my reply here. We have not found any issue while using latest version of Aspose.Words for .NET 16.11.0 and modified InsertWatermarkAtEachPage method. Please use Aspose.Words for .NET 16.11.0.