I am using your recommended code for adding watermarks to a document via a TextPlainText shape inserted to the headers.
My TextPath.Text value is “2 LINE\r\nWATERMARK” and the TextPath.TextPathAlignment is Center. I’m getting two lines of text, but the text is left justified…
2 LINE
WATERMARK
rather than…
2 LINE
WATERMARK
When the shape is rotated, the lines retain this left alignment, the W remains below the 2.
In Microsoft Word, the WordArt object supports multiple line text and the text on each line is centered. Is there a way to do this in Aspose.Words? I am currently evaluating version 11.7.
Hi David,
Thanks for your query. Please use the following code snippet with Aspose.Words for .NET 11.7.0. I have attached the output Docx file with this post. The “2 LINE\r\nWATERMARK” is center aligned in output document. Please let us know if you have any more queries.
Document doc = new Document();
InsertWatermarkText(doc, "2 LINE\r\nWATERMARK");
doc.Save(MyDir + "AsposeOut.docx");
///
/// 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 WordArt shape.
// You are free to try other shape types as watermarks.
Shape watermark = new Shape(doc, ShapeType.TextPlainText);
// Set up the text of the watermark.
watermark.TextPath.Text = watermarkText;
watermark.TextPath.FontFamily = "Arial";
watermark.Width = 500;
watermark.Height = 100;
// 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 = Color.Gray; // Try LightGray to get more Word-style watermark
watermark.StrokeColor = Color.Gray; // Try LightGray to get more Word-style watermark
// Place the watermark in the page center.
watermark.RelativeHorizontalPosition = RelativeHorizontalPosition.Page;
watermark.RelativeVerticalPosition = RelativeVerticalPosition.Page;
watermark.WrapType = WrapType.None;
watermark.VerticalAlignment = VerticalAlignment.Center;
watermark.HorizontalAlignment = Aspose.Words.Drawing.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));
}
That was the code block I was using (plus a few changes for color). While this produces the center aligned watermark in a docx file, saving the document to a PDF file or sending it to a printer yields a left-aligned watermark. Please see the attached files showing the different output in DOCX and PDF.
Hi David,
Thanks for your query. I have managed to reproduce the same issue at my side. I have logged this issue as WORDSNET-7016 in our issue tracking system. I have linked this forum thread to the same issue and you will be notified via this forum thread once this issue is resolved.
We apologize for your inconvenience.