Created
May 22, 2026 05:32
-
-
Save aspose-com-gists/4892e89ceddf68b4cf772bc4ebe15938 to your computer and use it in GitHub Desktop.
Add Insert or Draw Text on PNG JPEG TIFF Icon GIF Image Java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Add Insert or Draw Text on PNG JPEG TIFF Icon GIF Image Java | |
| // Read the full guide here: https://blog.aspose.com/slides/add-insert-or-draw-text-on-png-jpeg-tiff-icon-gif-image-java/ | |
| import com.aspose.slides.*; | |
| import java.awt.*; | |
| import java.awt.image.*; | |
| public class TextOverlayExample { | |
| public static void main(String[] args) throws Exception { | |
| // Load source image (PNG, JPEG, TIFF, ICON, or GIF) | |
| String inputPath = "src/main/resources/sample.png"; | |
| Image image = Image.load(inputPath); | |
| // Create graphics context | |
| Graphics2D graphics = image.createGraphics(); | |
| // Set rendering hints for high quality | |
| graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, | |
| RenderingHints.VALUE_ANTIALIAS_ON); | |
| graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, | |
| RenderingHints.VALUE_TEXT_ANTIALIAS_ON); | |
| // Define font and brush | |
| Font font = new Font("Arial", Font.BOLD, 48); | |
| Color textColor = new Color(255, 0, 0, 128); // Semi‑transparent red | |
| graphics.setFont(font); | |
| graphics.setColor(textColor); | |
| // Position and draw the text | |
| String watermark = "Confidential"; | |
| int x = image.getWidth() / 4; | |
| int y = image.getHeight() / 2; | |
| graphics.drawString(watermark, x, y); | |
| // Release graphics resources | |
| graphics.dispose(); | |
| // Save the result in the same format as the input | |
| String outputPath = "output/sample_watermarked.png"; | |
| image.save(outputPath, ImageFormat.PNG); | |
| System.out.println("Watermark added successfully."); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment