Free Spire.Presentation for Java 2.2.3(免费版)
方式一:从官网获取Free Spire.Presentation for Java之后解压,在IDEA或者Eclipse中Shift+Ctrl+Alt+S添加导入Spire.Pdf.jar包到项目中,jar文件可在解压路径下的lib文件夹中获取。
方式二:使用Maven配置导包。可以参考官方帮助文档。(http://www.e-iceblue.cn/licensing/install-spirepdf-for-java-from-maven-repository.html)
参考的PPT源文件截图。
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 { String inputFile = 'data/WaterMarked.pptx'; String outputFile = 'output/addWatermark_result.pptx'; //创建一个PPT文档实例并加载目标文档 Presentation presentation = new Presentation(); presentation.loadFromFile(inputFile); //设置文本水印的宽和高 int width= 400; int height= 300; //定义一个长方形区域 Rectangle2D.Double rect = new Rectangle2D.Double((presentation.getSlideSize().getSize().getWidth() - width) / 2, (presentation.getSlideSize().getSize().getHeight() - height) / 2, width, height); //添加一个shape到定义区域 IAutoShape shape = presentation.getSlides().get(0).getShapes().appendShape(ShapeType.RECTANGLE, rect); //设置shape样式 shape.getFill().setFillType(FillFormatType.NONE); shape.getShapeStyle().getLineColor().setColor(Color.white); shape.setRotation(-45); shape.getLocking().setSelectionProtection(true); shape.getLine().setFillType(FillFormatType.NONE); //添加文本到shape shape.getTextFrame().setText('内部参考'); PortionEx textRange = shape.getTextFrame().getTextRange(); //设置文本水印样式 textRange.getFill().setFillType(FillFormatType.SOLID); textRange.getFill().getSolidColor().setColor(Color.pink); textRange.setFontHeight(50); //保存文档 presentation.saveToFile(outputFile, FileFormat.PPTX_2010); }}
结果截图。