Hi Team,
I am able to add watermark text into word document using Aspose .net and i am not able to read the existing watermark text from that word document. I want to read the watermark text along with font family, font size, Text Color and layout. Can you please provide me a solution. I am attaching the input document.
existing watermark.docx (18.2 KB)
@Sandhya_Rani Watermarks in MS Word document are simple shape in the header behind main content. To distinguish them among other shapes MS Word and Aspose.Words use special shape names that starts either from "PowerPlusWaterMarkObject"
or "WordPictureWatermark"
as shown in the code below:
Document doc = new Document("C:\\Temp\\in.docx");
foreach (Shape s in doc.GetChildNodes(NodeType.Shape, true))
{
if (s.Name.Contains("PowerPlusWaterMarkObject") || s.Name.Contains("WordPictureWatermark"))
{
// The shape is watermark
// ..................
}
}
I am able to get PowerPlusWaterMarkObject but i am not able to see the watermark text. Therefore i can able to see text as empty when i debug.
For our scenario we have to read the watermark text along with the properties from the word document.
Thanks @alexey.noskov now i am able to read watermark text but how can we get font size and rotation angle from shape and apply to our watermark stamp and our goal is we want to read whole watermark properties from word document and need to create exact same watermark in pdf using stamp.
@Sandhya_Rani You can use Shape.TextPath properties to get font properties of the watermark. Rotation of the watermark can be get using Shape.Rotation property. Other properties of Shape object to get size and other option of the shape.
You can also use ShapeRenderer to render the watermark shape to image and then use this image as a watermark in your PDF.
@alexey.noskov
Thanks for the quick response. I am attaching the input word document which is having watermark now our concern is we want the same watermark to be copied in pdf document. But currently watermark in pdf looks different from word. How to fix that ?
Input file name : existing watermark.docx
existing watermark.docx (18.2 KB)
Output filename : Output pdf document.pdf
Output pdf document.pdf (225.8 KB)
Below is the code snippet :
using Aspose.Pdf;
using Aspose.Pdf.Text;
using Aspose.Words;
using SaveFormat = Aspose.Words.SaveFormat;
namespace PDFWatermark
{
static class Program
{
static void Main(string[] args)
{
TextStamp WatermarkStamp = new TextStamp("Watermark");
// converting Aspose Word Document to Aspose Pdf Document
Aspose.Words.Document document1 = new Aspose.Words.Document(@"C:/Users/sjangeti/Downloads/existing watermark.docx");
foreach ( Node shape1 in document1.GetChildNodes(NodeType.Shape, true))
{
Aspose.Words.Drawing.Shape s1 = (Aspose.Words.Drawing.Shape) shape1;
if (s1.Name.Contains("PowerPlusWaterMarkObject") || s1.Name.Contains("WordPictureWatermark"))
{
var watermarkText = s1.TextPath.Text;
WatermarkStamp = new TextStamp(watermarkText);
WatermarkStamp.TopMargin = 10;
WatermarkStamp.TextState.FontSize = (float)s1.TextPath.Size;
WatermarkStamp.TextState.Font = FontRepository.FindFont(s1.TextPath.FontFamily);
WatermarkStamp.TextState.ForegroundColor = Color.FromRgb(System.Drawing.Color.FromName( s1.FillColor.Name));
WatermarkStamp.HorizontalAlignment = HorizontalAlignment.Center;
WatermarkStamp.VerticalAlignment = VerticalAlignment.Center;
var fontrot = s1.Rotation;
WatermarkStamp.RotateAngle = s1.Rotation;
WatermarkStamp.Opacity = 0.5;
WatermarkStamp.setStampId(3);
break;
}
}
document1.Watermark.Remove();
document1.Save(@"C:/Users/sjangeti/Downloads/source.pdf", SaveFormat.Pdf);
Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document(@"C:/Users/sjangeti/Downloads/source.pdf");
// Add watermark on all pages
foreach (Page page in pdfDocument.Pages)
{
page.AddStamp(WatermarkStamp);
}
// Save updated document
pdfDocument.Save(@"C:/Users/sjangeti/Downloads/Output pdf document.pdf");
System.Console.WriteLine("Working Done");
}
}
}
@Sandhya_Rani The easies way to achieve what you need is to render the watermark to image and use this image as a stamp. For example see the following code:
Aspose.Words.Document doc = new Aspose.Words.Document(@"C:\Temp\in.docx");
using (MemoryStream watermarkImageStream = new MemoryStream())
{
foreach (Shape s in doc.GetChildNodes(NodeType.Shape, true))
{
if (s.Name.Contains("PowerPlusWaterMarkObject") || s.Name.Contains("WordPictureWatermark"))
{
// Render the watermark to graphics, since ShapeRenderer.Save sets background of the rendered image to white
// and we need a transparent background.
ShapeRenderer renderer = s.GetShapeRenderer();
Size imageSize = renderer.GetSizeInPixels(1, 96);
using (System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(imageSize.Width, imageSize.Height))
using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bmp))
{
renderer.RenderToSize(g, 0, 0, imageSize.Width, imageSize.Height);
bmp.Save(watermarkImageStream, System.Drawing.Imaging.ImageFormat.Png);
}
break;
}
}
watermarkImageStream.Position = 0;
doc.Watermark.Remove();
doc.Save(@"C:\Temp\out.pdf");
Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document(@"C:\Temp\out.pdf");
// Add watermark on all pages
foreach (Aspose.Pdf.Page page in pdfDocument.Pages)
{
Aspose.Pdf.ImageStamp stamp = new Aspose.Pdf.ImageStamp(watermarkImageStream);
stamp.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Center;
stamp.VerticalAlignment = Aspose.Pdf.VerticalAlignment.Center;
double scale = Math.Min(page.PageInfo.Width / stamp.Width, page.PageInfo.Height / stamp.Height);
if (scale < 1)
{
stamp.Width *= scale;
stamp.Height *= scale;
}
page.AddStamp(stamp);
}
// Save updated document
pdfDocument.Save(@"C:\\Temp\\out_pdf.pdf");
}
out_pdf.pdf (412.5 KB)
But actually it is not clear why you remove watermark from document before saving it to PDF and then add the same watermark by postprocessing PDF.
Hi @alexey.noskov , I am Sathish working with sandhya. This is the answer to your question why we are trying to apply watermark into pdf instead of converting from word. Remove watermark from Aspose pdf document If we convert word to pdf, whenever we want remove watermark from pdf It is not working. We can’t wait until you guys fix this. So, we are thinking of the above work around.
And I can see your soultion you are using System.Drawing as per Microsoft Breaking change: System.Drawing.Common only supported on Windows - .NET | Microsoft Learn System.Drawing not going to work in linux. As we wanted to run our project in linux container this solution is not going to work.
So could you please suggest as to how we can create text stamps from shape.
The goal is the watermark style should be same in word and pdf. Currently we can be able to get the text, but we are facing issues with font size, color, and rotation angle. How to fix this? If there is any constant value we need to add or multiply to the shape property to get proper stamp property, we are ready to do that as well.
@sarathisathish90 @Sandhya_Rani If you are waking in Linux, I suppose you are using either .NET Standard 2.0 or .NET6 version of Aspose.Words, which uses SkiaSharp to deal with graphics instead of System.Drawing. So the code should be modified like this:
Aspose.Words.Document doc = new Aspose.Words.Document(@"C:\Temp\in.docx");
using (MemoryStream watermarkImageStream = new MemoryStream())
{
foreach (Shape s in doc.GetChildNodes(NodeType.Shape, true))
{
if (s.Name.Contains("PowerPlusWaterMarkObject") || s.Name.Contains("WordPictureWatermark"))
{
// Render the watermark to graphics, since ShapeRenderer.Save sets background of the rendered image to white
// and we need a transparent background.
ShapeRenderer renderer = s.GetShapeRenderer();
Size imageSize = renderer.GetSizeInPixels(1, 96);
using (SkiaSharp.SKBitmap bmp = new SkiaSharp.SKBitmap(imageSize.Width, imageSize.Height))
using (SkiaSharp.SKCanvas g = new SkiaSharp.SKCanvas(bmp))
{
renderer.RenderToSize(g, 0, 0, imageSize.Width, imageSize.Height);
bmp.Encode(watermarkImageStream, SkiaSharp.SKEncodedImageFormat.Png, 100);
}
break;
}
}
watermarkImageStream.Position = 0;
doc.Watermark.Remove();
doc.Save(@"C:\Temp\out.pdf");
Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document(@"C:\Temp\out.pdf");
// Add watermark on all pages
foreach (Aspose.Pdf.Page page in pdfDocument.Pages)
{
Aspose.Pdf.ImageStamp stamp = new Aspose.Pdf.ImageStamp(watermarkImageStream);
stamp.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Center;
stamp.VerticalAlignment = Aspose.Pdf.VerticalAlignment.Center;
double scale = Math.Min(page.PageInfo.Width / stamp.Width, page.PageInfo.Height / stamp.Height);
if (scale < 1)
{
stamp.Width *= scale;
stamp.Height *= scale;
}
page.AddStamp(stamp);
}
// Save updated document
pdfDocument.Save(@"C:\\Temp\\out_pdf.pdf");
}
Regarding setting font size and color for text stamp in Aspose.PDF, it is better to consult with Aspose.PDF support in the appropriate forum. My colleagues will help you shortly.
Hi @alexey.noskov , Thanks for your quick help. Looking forward to here from Aspose pdf team. Because using text stamps will make our job easier. So could you please route this ticket to the proper team.
@sarathisathish90 I have create a topic in Aspose.PDF forum for you:
https://forum.aspose.com/t/specify-font-size-text-color-and-rotation-of-text-stamp/277531
Thanks @alexey.noskov i can able to get the watermark text on all the pdf pages with the below code snippet. How can we remove applied watermark text for all pages on the output pdf document ?
Aspose.Words.Document doc = new Aspose.Words.Document(@"C:\Temp\in.docx");
using (MemoryStream watermarkImageStream = new MemoryStream())
{
foreach (Shape s in doc.GetChildNodes(NodeType.Shape, true))
{
if (s.Name.Contains("PowerPlusWaterMarkObject") || s.Name.Contains("WordPictureWatermark"))
{
// Render the watermark to graphics, since ShapeRenderer.Save sets background of the rendered image to white
// and we need a transparent background.
ShapeRenderer renderer = s.GetShapeRenderer();
Size imageSize = renderer.GetSizeInPixels(1, 96);
using (SkiaSharp.SKBitmap bmp = new SkiaSharp.SKBitmap(imageSize.Width, imageSize.Height))
using (SkiaSharp.SKCanvas g = new SkiaSharp.SKCanvas(bmp))
{
renderer.RenderToSize(g, 0, 0, imageSize.Width, imageSize.Height);
bmp.Encode(watermarkImageStream, SkiaSharp.SKEncodedImageFormat.Png, 100);
}
break;
}
}
watermarkImageStream.Position = 0;
doc.Watermark.Remove();
doc.Save(@"C:\Temp\out.pdf");
Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document(@"C:\Temp\out.pdf");
// Add watermark on all pages
foreach (Aspose.Pdf.Page page in pdfDocument.Pages)
{
Aspose.Pdf.ImageStamp stamp = new Aspose.Pdf.ImageStamp(watermarkImageStream);
stamp.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Center;
stamp.VerticalAlignment = Aspose.Pdf.VerticalAlignment.Center;
double scale = Math.Min(page.PageInfo.Width / stamp.Width, page.PageInfo.Height / stamp.Height);
if (scale < 1)
{
stamp.Width *= scale;
stamp.Height *= scale;
}
page.AddStamp(stamp);
}
// Save updated document
pdfDocument.Save(@"C:\\Temp\\out_pdf.pdf");
}
@Sandhya_Rani The question is related to Aspose.PDF, so you should ask in the Aspose.PDF forum. My colleagues will help you shortly.