Monday, 11 October 2021

Add Multiple Text Watermarks to PowerPoint slides in Java

In previous article, I introduced how to add/remove image watermarks or text watermarks to PowerPoint slides using Java codes with the help of a free third-party library called Free Spire.Presentation for Java. Actually, the library also supports adding multiple text watermarks to PowerPoint slides. Today this tutorial will demonstrate how to do it.

DEPENDENCY

You can get the API’s JAR from the link or install it using the following Maven configurations.

<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.presentation.free</artifactId>
<version>3.9.0</version>
</dependency>
</dependencies>

Add Multiple Text Watermarks to PowerPoint Slides

You can add multiple text watermarks to the specified slide in a PowerPoint document by following the

steps below.

l  Load a PowerPoint document using Presentation class

l  Set the content of text watermark and get its size using PdfTrueTypeFont. measureString(watermarkText) method

l  Add a shape using presentation.getSlides().get(0).getShapes().appendShape(ShapeType.RECTANGLE,Rectangle2D) method

l  Set line color, rotation and other styles of the shape

l  Add watermark text to the shape using shape.getTextFrame().getTextRange() method and set styles of the text range.

l  Save the resulting document to a specified path using presentation.saveToFile(string,FileFormat.PPTX_2013) method

The full sample code can be shown as below.

import com.spire.pdf.graphics.PdfTrueTypeFont;
import com.spire.presentation.*;
import com.spire.presentation.drawing.*;
import java.awt.*;
import java.awt.geom.Dimension2D;
import java.awt.geom.Rectangle2D;

public class MultipleTextWatermarks {
public static void main(String[] args) throws Exception {
//Create a Presentation object
Presentation presentation = new Presentation();

//Load the sample PowerPoint file
presentation.loadFromFile("C:\\Users\\Test1\\Desktop\\Sample.pptx");

//Specify watermark text
String watermarkText = "DRAFT";

//Get the size of the watermark text
Font font = new java.awt.Font("Arial", java.awt.Font.BOLD, 20);
PdfTrueTypeFont trueTypeFont = new PdfTrueTypeFont(font);
Dimension2D strSize = trueTypeFont.measureString(watermarkText);

//Initialize x and y coordinate
float x = 30;
float y = 80;

for (int rowNum = 0; rowNum < 4; rowNum++) {
for (int colNum = 0; colNum < 5; colNum++) {

//Add a rectangle shape
Rectangle2D rect = new Rectangle2D.Float(x, y, (float) strSize.getWidth() + 10, (float) strSize.getHeight());
IAutoShape shape = presentation.getSlides().get(0).getShapes().appendShape(ShapeType.RECTANGLE, rect);

//Set the style of the shape
shape.getFill().setFillType(FillFormatType.NONE);
shape.getShapeStyle().getLineColor().setColor(new Color(1, 1, 1, 0));
shape.setRotation(-45);
shape.getLocking().setSelectionProtection(true);
shape.getLine().setFillType(FillFormatType.NONE);

//Add watermark text to the shape
shape.getTextFrame().setText(watermarkText);
PortionEx textRange = shape.getTextFrame().getTextRange();

//Set the style of the text range
textRange.getFill().setFillType(FillFormatType.SOLID);
textRange.getFill().getSolidColor().setColor(Color.pink);
textRange.setLatinFont(new TextFont(trueTypeFont.getName()));
textRange.setFontMinSize(trueTypeFont.getSize());

x += (100 + strSize.getWidth());

}
x = 30;
y += (100 + strSize.getHeight());
}

//Save the document
presentation.saveToFile("output/MultipleWatermark.pptx", FileFormat.PPTX_2013);
}
}

The screenshot below shows the output generated by the sample code



No comments:

Post a Comment

Change PDF Versions in Java

In daily work, you might need to change the version of a PDF document you have in order to ensure compatibility with another version which a...