Adding a Watermark to Converted Documents

GroupDocs.Conversion provides a flexible way to apply watermarks to documents during the conversion process. This feature can be useful for branding, security, or informational purposes. You can customize various aspects of the watermark, such as its text, size, color, position, and transparency.

Tip
Looking for advanced watermarking capabilities? While GroupDocs.Conversion offers basic watermarking, you can explore GroupDocs.Watermark for a comprehensive solution with enhanced features.

WatermarkOptions

You can control how the watermark appears in the converted document using the WatermarkOptions class. Here are the key properties you can set:

OptionDescription
setText()The content of the watermark text.
setWatermarkFont()The font family of the watermark text.
setColor()The color of the watermark text.
setWidth()The width of the watermark.
setHeight()The height of the watermark.
setTop()The distance from the top edge of the document.
setLeft()The distance from the left edge of the document.
setRotationAngle()The angle at which the watermark is rotated.
setTransparency()The transparency level of the watermark (0 is fully transparent, 1 is fully opaque).
setBackground()Determines whether the watermark is stamped as a background. If set to true, the watermark appears behind the document content. By default, it is set to false, meaning the watermark will appear on top of the content.
setAutoAlign()Enables or disables automatic alignment of the watermark on the document. When set to true, the watermark will be automatically positioned to best fit the content layout, ensuring optimal visibility and aesthetics. When set to false, the watermark will retain its manually defined position.

Steps to Add a Watermark

  • Initialize the Converter: Create an instance of the Converter class, providing the path to the source document.
  • Set Conversion Options: Instantiate the appropriate ConvertOptions class (e.g., PdfConvertOptions, WordProcessingConvertOptions, SpreadsheetConvertOptions, etc.).
  • Configure Watermark Options: Create an instance of the WatermarkOptions class and configure the properties to specify the watermark’s appearance.
  • Apply the Watermark: Use the setWatermark method of the ConvertOptions instance to apply the watermark settings.
  • Convert the Document: Call the convert method on the Converter instance, passing the output file name and the configured ConvertOptions instance.

Example: Add a Text Watermark to Converted Document

The following Java code snippet demonstrates how to apply a text watermark to a document during conversion to PDF:

import com.groupdocs.conversion.Converter;
import com.groupdocs.conversion.options.convert.PdfConvertOptions;
import com.groupdocs.conversion.options.convert.WatermarkTextOptions;

public class AddWatermarkToConvertedDocument {
    public static void convert() {
        // Initialize the converter with the source document
        try(Converter converter = new Converter("annual-review.docx")){

            // Set conversion options for PDF
            PdfConvertOptions options = new PdfConvertOptions();

            // Configure watermark options
            WatermarkTextOptions watermark = new WatermarkTextOptions("Sample Watermark");
            watermark.setColor(Color.RED);        // Set watermark color to red
            watermark.setWidth(100);              // Set watermark width
            watermark.setHeight(100);             // Set watermark height
            watermark.setBackground(true);        // Set watermark as background
            
            // Apply the watermark to the conversion options
            options.setWatermark(watermark);

            // Convert the document and apply the watermark
            converter.convert("addTextWatermark.pdf", options);
        }
    }

    public static void main(String[] args){
        convert();
    }
}

annual-review.docx is sample file used in this example. Click here to download it.

addTextWatermark.pdf is converted PDF document. Click here to download it.

By following these steps and adjusting the settings, you can easily add customized watermarks to your converted documents using GroupDocs.Conversion.