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

Free Support Forum - aspose.com

Watermark issue for docx and pdf documents

Hi

I noticed watermark issue for more than two line text, e.g. This\r\nIs\r\nWatermark\r\nTest

and when top and left -999995. When I save document as .docx, watermark is not added to the document. when I save the document as pdf, watermark is added but the watermark text is cut. I have attached the code file as .txt and watermark files created. Please advise what changes I have to do to get the right watermark placement.

Hi there,

Thanks for your inquiry.

Why are you using such a large negative value for the position of the watermark? Please try using the values below instead.

double fontSize = 48.0;<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

bool isBold = false;

double rotation = 315;

double posTop = 0;

double posLeft = 0;

double opacity = 50.0;

Thanks,

Hi,

thanks for reply. I used posTop and posLeft=0, the watermark files generated still has problem, watermark in word document apprears as compressed and in pdf the text is still cut. I Have attached the documents.

Hi

Thanks for your request. As I can see you are using old version of Aspose.Words (9.7). Please try using the latest version. You can download it from here:

http://www.aspose.com/community/files/51/.net-components/aspose.words-for-.net/category1188.aspx

Best regards,

Hi

I got Aspose.words v10.3.0.0, but I don't get the desired results. Also, when I apply watermark to docx or doc file, I get incorrect format exception. Please check attached docs.

I want this text (For Your Information Only.\r\nNot to be Shown to Other customers.\r\nNot to be Distributed or Used in Sales/Promotional Detailing.) to appear at the bottom of the page with font size 12, color red and with center horizontal alignment to word and pdf docs. Can you please send me the code which can take care of this requirement?

Hi

Thanks for your request. The file with .doc extension is actually a PDF document. I suppose this is the reason why you get incorrect format exception.

I think, in your case, you should use a text box as a watermark instead of WordArt shape. I created a simple code example for you:

[Test]

public void Test001()

{

Document doc = new Document("C:\\Temp\\in.doc");

InsertWatermarkText(doc, "For Your Information Only.\nNot to be Shown to Other customers.\nNot to be Distributed or Used in Sales/Promotional Detailing.");

doc.Save("C:\\Temp\\out.doc");

doc.Save("C:\\Temp\\out.pdf");

}

///

/// Inserts a watermark into a document.

///

/// The input document.

/// Text of the watermark.

private static void InsertWatermarkText(Document doc, string watermarkText)

{

// Create a watermark shape. This will be a TextBox shape.

// You are free to try other shape types as watermarks.

Shape watermark = new Shape(doc, ShapeType.TextBox);

Paragraph par = new Paragraph(doc);

watermark.AppendChild(par);

// We will use DocumentBuilkder to insert the text.

DocumentBuilder builder= new DocumentBuilder(doc);

// Move builder into watermark.

builder.MoveTo(par);

// Setup text properties.

builder.Font.Size = 12;

builder.Font.Color = Color.Red;

builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;

builder.ParagraphFormat.SpaceAfterAuto = false;

builder.ParagraphFormat.SpaceBeforeAuto = false;

builder.ParagraphFormat.SpaceAfter = 0;

builder.ParagraphFormat.SpaceBefore = 0;

// Insert text.

builder.Write(watermarkText);

// Set up the text box.

watermark.Width = 500;

watermark.Height = 60;

watermark.Stroked = false;

// Place the watermark in the page center.

watermark.RelativeHorizontalPosition = RelativeHorizontalPosition.Page;

watermark.RelativeVerticalPosition = RelativeVerticalPosition.Page;

watermark.WrapType = WrapType.None;

watermark.VerticalAlignment = VerticalAlignment.Bottom;

watermark.HorizontalAlignment = HorizontalAlignment.Center;

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

Paragraph watermarkPara = new Paragraph(doc);

watermarkPara.AppendChild(watermark);

// Insert the watermark into all headers of each document section.

foreach (Section sect 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);

}

}

private static void InsertWatermarkIntoHeader(Paragraph watermarkPara, Section sect, HeaderFooterType headerType)

{

HeaderFooter header = sect.HeadersFooters[headerType];

if (header == null)

{

// There is no header of the specified type in the current section, create it.

header = new HeaderFooter(sect.Document, headerType);

sect.HeadersFooters.Add(header);

}

// Insert a clone of the watermark into the header.

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

}

Hope this helps.

Best regards,

Thank you very much. This works as expected.