Requires
In the following document, I want
to add an image watermark to the first slide, and add a text watermark to the
second slide. Meanwhile, I would like to know whether the watermarks can be
removed at one time or not.
Note:
I need to finish the functions in Java programmatically without using Microsoft
PowerPoint.
Solution
Through researching many kinds of
third-party libraries, I found a free API called Free Spire.Presentation for Java. It is a professional API that
enables developers to create, read, write, convert and print PowerPoint
documents in Java Applications without installing Microsoft PowerPoint.
Therefore, I’ll use the API to
demonstrate the following demo samples. Before running codes, you need to
download the product’s package and then insert Spire.Presentation.jar to IDEA. Or
directly reference it by writing the following configurations in the Maven’s
pom.xml file.
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>http://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>
Using the code
Add Image Watermark
import com.spire.presentation.*;
import com.spire.presentation.drawing.*;
import javax.imageio.ImageIO;
import java.io.File;
public class ImageWatermark {
public static void main(String[] args) throws Exception {
//Create a PowerPoint document.
Presentation presentation = new Presentation();
//Load the file from disk.
presentation.loadFromFile("C:\\Users\\Test1\\Desktop\\Test.pptx");
//Get the image you want to add as image watermark.
File file =new File("C:\\Users\\Test1\\Desktop\\pic.jpg");
IImageData image = presentation.getImages().append(ImageIO.read(file));
//Set the properties of SlideBackground, and then fill the image as watermark.
presentation.getSlides().get(0).getSlideBackground().setType(BackgroundType.CUSTOM);
presentation.getSlides().get(0).getSlideBackground().getFill().setFillType(FillFormatType.PICTURE);
presentation.getSlides().get(0).getSlideBackground().getFill().getPictureFill().setFillType(PictureFillType.STRETCH);
presentation.getSlides().get(0).getSlideBackground().getFill().getPictureFill().getPicture().setEmbedImage(image);
String result = "output/addImageWatermark.pptx";
//Save to file.
presentation.saveToFile(result, FileFormat.PPTX_2013);
}
}Output
Add Text WatermarkIn the process of adding text watermark, Free Spire.Presentation for Java supports setting the width,
height, color and rotation of the text watermark.
import com.spire.presentation.*;
import com.spire.presentation.drawing.FillFormatType;
import java.awt.*;
import java.awt.geom.Rectangle2D;
public class AddWatermark {
public static void main(String[] args) throws Exception {
//Load a PDF document for testing
Presentation presentation = new Presentation();
presentation.loadFromFile("C:\\Users\\Test1\\Desktop\\AddTextWatermark.pptx");
//Set the width and height of watermark string
int width= 400;
int height= 300;
//Define a rectangle range
Rectangle2D.Double rect = new Rectangle2D.Double((presentation.getSlideSize().getSize().getWidth() - width) / 4,
(presentation.getSlideSize().getSize().getHeight() - height) / 2, width, height);
//Add a rectangle shape with a defined range
IAutoShape shape = presentation.getSlides().get(1).getShapes().appendShape(ShapeType.RECTANGLE, rect);
//Set the style of shape
shape.getFill().setFillType(FillFormatType.NONE);
shape.getShapeStyle().getLineColor().setColor(Color.white);
shape.setRotation(-45);
shape.getLocking().setSelectionProtection(true);
shape.getLine().setFillType(FillFormatType.NONE);
//Add text to shape
shape.getTextFrame().setText("Draft");
PortionEx textRange = shape.getTextFrame().getTextRange();
//Set the style of the text range
textRange.getFill().setFillType(FillFormatType.SOLID);
textRange.getFill().getSolidColor().setColor(Color.red);
textRange.setFontHeight(50);
//Save the document
presentation.saveToFile("output/addWatermark.pptx", FileFormat.PPTX_2010);
}
}Output
Delete text or image watermarkimport com.spire.presentation.*;
import com.spire.presentation.drawing.*;
public class DeleteWatermark {
public static void main(String[] args) throws Exception {
//Load the sample document
Presentation presentation = new Presentation();
presentation.loadFromFile("C:\\Users\\Test1\\Desktop\\DeleteAllWatermark.pptx");
//Remove text watermark by removing the shape which contains the string "Draft".
for (int i = 0; i < presentation.getSlides().getCount(); i++)
{
for (int j = 0; j < presentation.getSlides().get(i).getShapes().getCount(); j++)
{
if (presentation.getSlides().get(i).getShapes().get(j) instanceof IAutoShape)
{
IAutoShape shape = (IAutoShape)presentation.getSlides().get(i).getShapes().get(j);
if (shape.getTextFrame().getText().contains("Draft"))
{
presentation.getSlides().get(i).getShapes().remove(shape);
}
}
}
}
//Remove image watermark
for (int i = 0; i < presentation.getSlides().getCount(); i++)
{
presentation.getSlides().get(i).getSlideBackground().getFill().setFillType(FillFormatType.NONE);
}
//Save to file.
presentation.saveToFile("output/deleteWatermark.pptx", FileFormat.PPTX_2013);
}
}Conclusion
After running the code snippets below, we can successfully add or delete text or image watermarks in
PowerPoint documents. If there is any problem while installing the jar file or running codes, you can take notes on Forum.
No comments:
Post a Comment