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

Free Support Forum - aspose.com

ODT to PDF with watermark on All Images

Hello Aspose Team,
Is there any way that I can apply watermark on images as well while converting ODT to PDF, I am not talking about page watermarks, I have already done this. But I also watermarked all of my images in ODT when converted to PDF. Please tell me if this is possible.

Thanks

Best Regards

@aadi1295

Thanks for you inquiry. Please use the following code to get desired results. Hope, this helps.

Document doc = new Document("D:\\Temp\\input.odt");

NodeCollection shapes = doc.GetChildNodes(NodeType.Shape, true);
foreach(Shape shape in shapes) {

 if (shape.HasImage) {
  Shape watermark = new Shape(doc, ShapeType.TextPlainText);

  watermark.Name = "WaterMark";
  // Set up the text of the watermark.
  watermark.TextPath.Text = "CONFIDENTIAL";
  watermark.TextPath.FontFamily = "Arial";
  watermark.Width = shape.Width / 2;
  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

  // Create a new paragraph and append the watermark to this paragraph.
  Paragraph watermarkPara = new Paragraph(doc);
  watermarkPara.ParagraphFormat.Alignment = ParagraphAlignment.Center;
  watermarkPara.AppendChild(watermark);
  shape.AppendChild(watermarkPara);

 }

}

doc.Save("D:\\Temp\\output_18.12.pdf");

@mannanfazil
Thank you for your help

I am trying to add image watermark on all images in the document and using the following code

                NodeCollection shapes = doc.GetChildNodes(NodeType.Shape, true);
                foreach (Shape shape in shapes)
                {
                    if (shape.HasImage)
                    {
                        Shape watermarkimages = new Shape(doc, ShapeType.Image);
                        watermarkimages.ImageData.SetImage(MyDir + site_watermark);
                        watermarkimages.Width = 100;
                        watermarkimages.Height = 22;
                        watermarkimages.Fill.Opacity = 0.4;
                        watermarkimages.WrapType = WrapType.None;
                        watermarkimages.WrapSide = WrapSide.Default;
                        watermarkimages.BehindText = false;
                        watermarkimages.Rotation = -40;

                        // Create a new paragraph and append the watermark to this paragraph.
                        Paragraph watermarkPara = new Paragraph(doc);
                        watermarkPara.ParagraphFormat.Alignment = ParagraphAlignment.Center;
                        watermarkPara.AppendChild(watermarkimages);
                        shape.AppendChild(watermarkPara);
                    }
                }

but the issue is that I am getting an error ‘The maximum number of stack frames supported by Visual Studio has been exceeded.’

There are 385 images in this document, any help is appreciated

@aadi1295

Please use the updated code to get desired results. Hope, this helps.

NodeCollection shapeCollection = doc.GetChildNodes(NodeType.Shape, true);
Node[] shapes = shapeCollection.ToArray();
foreach(Shape shape in shapes.OfType < Shape > ()) {
 if (shape.HasImage) {
  Shape watermarkimages = new Shape(doc, ShapeType.Image);
  watermarkimages.ImageData.SetImage(MyDir + site_watermark);
  watermarkimages.Width = 100;
  watermarkimages.Height = 22;
  watermarkimages.Fill.Opacity = 0.4;
  watermarkimages.WrapType = WrapType.None;
  watermarkimages.WrapSide = WrapSide.Default;
  watermarkimages.BehindText = false;
  watermarkimages.Rotation = -40;

  // Create a new paragraph and append the watermark to this paragraph.
  Paragraph watermarkPara = new Paragraph(doc);
  watermarkPara.ParagraphFormat.Alignment = ParagraphAlignment.Center;
  watermarkPara.AppendChild(watermarkimages);
  shape.AppendChild(watermarkPara);
 }
}