DEV Community

Atir Tahir
Atir Tahir

Posted on

3

Add watermark to the converted document in C# or Java

Document conversion is one of the most frequent process that endures across a lot of industries. Sometimes, its the business need to put a watermark on the resultant document. For example, you want to convert a PPTX to PDF with a watermark text or image on all the PDF pages.

GroupDocs.Conversion for .NET gives you such a option. It possesses a class WatermarkOptions with rich properties such as:

  • Text/Font
  • Color
  • Width
  • Height
  • Background
  • Transparency
  • Rotation angle

Lets have a look at its implementation in C#:

using (Converter converter = new Converter("sample.docx"))
{
WatermarkOptions watermark = new WatermarkOptions
{
Text = "Sample watermark",
Color = Color.Red,
Width = 100,
Height = 100,
Background = true
};
PdfConvertOptions options = new PdfConvertOptions
{
Watermark = watermark
};
converter.Convert("converted.pdf", options);
}

Document conversion in Java:

ConversionConfig conversionConfig = new ConversionConfig();
conversionConfig.setStoragePath("source file path");
conversionConfig.setOutputPath("output path");
ConversionHandler conversionHandler = new ConversionHandler(conversionConfig);
String sourceFileName = "source.pptx";
PdfSaveOptions saveOptions = new PdfSaveOptions();
WatermarkOptions watermarkOptions = new WatermarkOptions();
watermarkOptions.setText("Watermark text");
watermarkOptions.setColor(Color.blue);
watermarkOptions.setFont(new Font("Arial", 40, 12));
watermarkOptions.setRotationAngle(45);
watermarkOptions.setTransparency(0.1);
watermarkOptions.setLeft(200);
watermarkOptions.setTop(400);
saveOptions.getWatermarkOptions().setBackground(true);
saveOptions.setWatermarkOptions(watermarkOptions);
ConvertedDocument result = conversionHandler.<String> convert(sourceFileName, saveOptions);
result.save(sourceFileName + "." + result.getFileType());

Below is the screenshot, you can see conversion of a PPTX to PDF along with watermark text.

Alt Text

Go through the API features. If there’s any issue, you can post on forum.

Top comments (0)