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.
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:
- First, the PDF document named “input.pdf” is loaded using the Document class.
- 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.
- 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.
- The watermark artifact is added to the first page of the PDF using the getArtifacts().add(artifact) method.
- 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:
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:
- The first step is to load the input PDF document using the Document class.
- A new BackgroundArtifact is created to serve as the watermark.
- 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”).
- The opacity of the watermark is set to 0.5, making it semi-transparent using setOpacity().
- The horizontal and vertical alignment of the watermark artifact is adjusted using setArtifactHorizontalAlignment() and setArtifactVerticalAlignment() methods to center the watermark in the page.
- The background artifact is added to the first page of the PDF document using doc.getPages().get_Item(1).getArtifacts().add(background).
- 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:
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