Hi I am reading a word file in meomryStream and using Aspose.word 17.8 licensed version , inserting a watermark in it and finally save it in a new word file but output file does not shows watermark. below is my sample code , please help me out to reach on a solution.
string dataDir = @"D:/Tasks/1_OBT 2.0/TKTs/CR_807_1655/";
Aspose.Words.Document doc = new Aspose.Words.Document(dataDir + "sampleDoc.docx");
// Create a watermark shape
Shape watermark = new Shape(doc, ShapeType.TextPlainText)
{
TextPath = { Text = "Confidential and Proprietary – AT&T Inc", FontFamily = "Arial", Size = 100 },
Fill = { Color = System.Drawing.Color.Gray }, // Set watermark color
StrokeColor = System.Drawing.Color.Transparent,
WrapType = WrapType.None,
RelativeHorizontalPosition = RelativeHorizontalPosition.Page,
RelativeVerticalPosition = RelativeVerticalPosition.Page,
HorizontalAlignment = Aspose.Words.Drawing.HorizontalAlignment.Center,
VerticalAlignment = Aspose.Words.Drawing.VerticalAlignment.Center,
Rotation = -45 // Rotate watermark to give it a diagonal effect
};
// Apply the watermark to each section's headers
foreach (Aspose.Words.Section section in doc.Sections)
{
// Loop through all header types (Primary, EvenPage, FirstPage)
foreach (HeaderFooterType headerType in Enum.GetValues(typeof(HeaderFooterType)))
{
Aspose.Words.HeaderFooter header = section.HeadersFooters[headerType];
// If the header doesn't exist, create it
if (header == null)
{
header = new Aspose.Words.HeaderFooter(doc, headerType);
section.HeadersFooters.Add(header);
}
// Create a paragraph to hold the watermark shape
Paragraph para = new Paragraph(doc);
// Add the watermark shape to the paragraph
para.AppendChild(watermark.Clone(true));
// Append the paragraph to the header
header.AppendChild(para);
}
}
// Save the document
doc.Save(dataDir + "AddTextWatermark_out.docx");
@lookatabhishek
It seems that you are having trouble inserting a watermark into a Word document using Aspose.Words version 17.8. Based on your provided code, it looks like you are correctly creating a watermark shape and attempting to add it to the headers of each section. However, there are a few things you might want to check or modify to ensure the watermark appears in the output document.
-
Watermark Visibility: Ensure that the watermark is not being obscured by other content in the document. You can adjust the Fill
and StrokeColor
properties to make the watermark more visible. For example, you might want to set the Fill.Color
to a lighter shade or make it semi-transparent.
-
Watermark Positioning: The watermark is being added to the headers, which is correct. However, ensure that the headers are not empty or that there is no other content that might be overlapping with the watermark. You can also try adjusting the alignment and rotation to see if that helps.
-
Using the Watermark Class: Instead of manually creating a shape for the watermark, you can use the built-in watermark functionality provided by Aspose.Words. This can simplify the process and ensure that the watermark is applied correctly. Here’s an example of how to add a text watermark using the Watermark
class:
Document doc = new Document(dataDir + "sampleDoc.docx");
TextWatermarkOptions options = new TextWatermarkOptions()
{
FontFamily = "Arial",
FontSize = 100,
Color = Color.Gray,
Layout = WatermarkLayout.Diagonal,
IsSemitrasparent = true
};
doc.Watermark.SetText("Confidential and Proprietary – AT&T Inc", options);
doc.Save(dataDir + "AddTextWatermark_out.docx");
-
Check for Errors: Make sure there are no exceptions being thrown during the execution of your code. If there are any issues with the document or the watermark, they might prevent the watermark from being added.
-
Update Aspose.Words: If possible, consider updating to a newer version of Aspose.Words. There may have been bug fixes or improvements related to watermark handling in later versions.
By following these suggestions, you should be able to successfully insert a watermark into your Word document. If the issue persists, please provide any error messages or additional details about the behavior you are observing.
Sources:
[1]: Add Text Watermark With Specific Options - Aspose API References
[2]: Working with Watermark in C#|Aspose.Words for .NET