How to Add Watermark

Learn how easily Add Watermark to PDF with high quality using Java Aspose.PDF library

How to Add Watermark with Java

WThis article delves into the usage of watermarks for safeguarding PDF documents or asserting ownership. Additionally, watermarks are employed to indicate the status of a PDF, such as draft or manuscript. To streamline the process of watermarking, this tutorial demonstrates how to programmatically add text or image watermarks to PDF using Java.

Add Watermark using Java API

To implement watermarking in PDF documents, we’ll utilize Aspose.PDF for Java— a comprehensive API enabling PDF file creation, processing, and conversion within Java applications. Access the API by either downloading the JAR file or installing it through the provided Maven configurations.

<repository> 
	<id>AsposeJavaAPI</id> 
	<name>Aspose Java API</name> 
	<url>https://repository.aspose.com/repo/</url> 
</repository> 

<dependency> 
	<groupId>com.aspose</groupId> 
	<artifactId>aspose-pdf</artifactId> 
	<version>21.6</version> 
</dependency> 

Add Text Watermark to PDF files

The provided Java code demonstrates how to add a text watermark to a PDF document using the Aspose.PDF library:

  1. First, the PDF document named “input.pdf” is loaded using the Document class.
  2. A FormattedText object is created to hold the watermark text “Confidential Document” with specific formatting settings, such as red color, Courier font style, and 40.0F font size.
  3. A WatermarkArtifact is created to represent the watermark on the PDF. The watermark’s text and properties, such as alignment, rotation, opacity, and background, are set.
  4. The watermark artifact is added to the first page of the PDF using the getArtifacts().add(artifact) method.
  5. Finally, the watermarked PDF is saved as “watermark.pdf” using the save() method of the Document class.

The following code snippet shows how to add watermarks from PDF using Java:

    // Load PDF document 
    Document doc = new Document("input.pdf"); 
    // Create a formatted text 
    FormattedText formattedText = new FormattedText("Confidential Document", java.awt.Color.RED, FontStyle.Courier, EncodingType.Identity_h, true, 40.0F); 
    // Create watermark artifact and set its properties 
    WatermarkArtifact artifact = new WatermarkArtifact();         
    artifact.setText(formattedText);         
    artifact.setArtifactHorizontalAlignment (HorizontalAlignment.Center); 
    artifact.setArtifactVerticalAlignment (VerticalAlignment.Center); 
    artifact.setRotation (25); 
    artifact.setOpacity (0.5); 
    artifact.setBackground (false); 
    // Add watermark to the first page of PDF 
    doc.getPages().get_Item(1).getArtifacts().add(artifact); 
    // Save watermarked PDF document 
    doc.save("watermark.pdf"); 

Add Image Watermark to PDF files

The provided code snippet demonstrates how to add a watermark as a background artifact to a PDF document using the Aspose.PDF for Java library:

  1. The first step is to load the input PDF document using the Document class.
  2. A new BackgroundArtifact is created to serve as the watermark.
  3. The image for the watermark is specified using setBackgroundImage(), where you need to provide the file path to the image (in this case, “logo.png”).
  4. The opacity of the watermark is set to 0.5, making it semi-transparent using setOpacity().
  5. The horizontal and vertical alignment of the watermark artifact is adjusted using setArtifactHorizontalAlignment() and setArtifactVerticalAlignment() methods to center the watermark in the page.
  6. The background artifact is added to the first page of the PDF document using doc.getPages().get_Item(1).getArtifacts().add(background).
  7. Finally, the watermarked PDF document is saved to the file “watermark.pdf” using doc.save(“watermark.pdf”).

The following code snippet shows how to add watermarks from PDF using Java:

    // Load PDF document 
    Document doc = new Document("input.pdf"); 
    // Create a background artifact 
    BackgroundArtifact background = new BackgroundArtifact(); 
    // Specify the image for background artifact object 
    background.setBackgroundImage(new FileInputStream("logo.png")); 
    background.setOpacity(0.5); 
    background.setArtifactHorizontalAlignment(HorizontalAlignment.Center); 
    background.setArtifactVerticalAlignment(VerticalAlignment.Center); 
    // Add watermark to the first page of PDF 
    doc.getPages().get_Item(1).getArtifacts().add(background); 
    // Save watermarked PDF document 
    doc.save("watermark.pdf") 

Try to Work with Watermarks in PDF online

Utilize this free app Add Watermark to effortlessly add watermarks to your PDF documents. With a wide array of customizable settings, you can achieve an outstanding result that precisely aligns with your requirements. Whether you opt for text or image watermarks, you have full control over font settings, including type, size, color, and other parameters. Additionally, you can manage rotation angles, layers, transparency, page numbers, and more. Best of all, this online tool operates seamlessly across various platforms, such as Windows, Linux, macOS, and Android, ensuring swift and accurate results without the need for local installations.

Documentation Aspose.PDF for Java Library

Explore the Java PDF API further through our comprehensive documentation. Should you have any queries or require assistance, feel free to reach out to us on our Aspose forum. We are here to support you and provide guidance on using the Java PDF API effectively.

Conclusion

Throughout this article, you have acquired the knowledge of adding text or image watermarks to a PDF using Java. Additionally, you have discovered the versatility of customizing the watermark’s appearance through various properties. With this newfound understanding, you can confidently enhance your PDF with personalized watermarks, tailoring them to suit your specific requirements with ease. If you want more details about Aspose.PDF libraries - visit our landings pages