在PPT幻燈片中,可經過添加形狀的方式,來實現相似水印的效果,可添加單一文本水印效果,即在幻燈片中心位置水印以單個文本字樣顯示,但經過必定方法也能夠添加多行(平鋪)文本水印效果,即在幻燈片中以必定方式平鋪排列多個文本水印效果到頁面上。上篇文章中介紹了經過C# 程序來添加多行水印效果,本文以Java程序代碼爲例介紹如何實現水印添加,包括添加單一文本水印和平鋪文本內水印,代碼供參考。html
本次程序編譯環境爲IntelliJ IDEA,JDK版本1.8.0,並引入free spire.presentation.jar3.9.0版本文件。java
1. 添加單一文本水印app
import com.spire.presentation.*; import com.spire.presentation.drawing.FillFormatType; import java.awt.*; import java.awt.geom.Rectangle2D; public class TextWatermark { public static void main(String[] args) throws Exception { //加載示例文檔 Presentation ppt = new Presentation(); ppt.loadFromFile("sample.pptx"); //獲取指定幻燈片 ISlide slide = ppt.getSlides().get(0); //設置文本水印的寬和高 int width= 400; int height= 300; //定義一個長方形區域 Rectangle2D.Double rect = new Rectangle2D.Double((ppt.getSlideSize().getSize().getWidth() - width) / 2, (ppt.getSlideSize().getSize().getHeight() - height) / 2, width, height); //添加一個shape到定義區域 IAutoShape shape = slide.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.setShapeArrange(ShapeAlignmentEnum.ShapeArrange.SendToBack); //添加文本到shape shape.getTextFrame().setText("內部使用"); PortionEx textRange = shape.getTextFrame().getTextRange(); //設置文本水印樣式 textRange.getFill().setFillType(FillFormatType.SOLID); textRange.getFill().getSolidColor().setColor(new Color(211,211,211)); textRange.setFontHeight(50); //保存文檔 ppt.saveToFile("TextWatermark.pptx", FileFormat.PPTX_2013); ppt.dispose(); } }
單一水印效果:ide
2. 添加多行(平鋪)文本水印spa
import com.spire.presentation.*; import com.spire.presentation.drawing.FillFormatType; import java.awt.*; import java.awt.geom.Rectangle2D; public class TextWatermark2 { public static void main(String[] args) throws Exception{ //加載PPT源文檔 Presentation ppt = new Presentation(); ppt.loadFromFile("sample.pptx"); //獲取指定幻燈片 ISlide slide = ppt.getSlides().get(0); //設置文本水印文本寬和高 int width= 110; int height= 80; //起始座標 float x = 10; float y = 40; for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { //繪製文本,設置文本格式並將其添加到第一張幻燈片 Rectangle2D.Double rect = new Rectangle2D.Double(x,y,width, height); IAutoShape shape = slide.getShapes().appendShape(ShapeType.RECTANGLE, rect); shape.getFill().setFillType(FillFormatType.NONE); shape.getShapeStyle().getLineColor().setColor(Color.white); shape.setRotation(-45); shape.getLocking().setSelectionProtection(true); shape.getLine().setFillType(FillFormatType.NONE); shape.getTextFrame().setText("內部使用"); shape.setShapeArrange(ShapeAlignmentEnum.ShapeArrange.SendToBack); PortionEx textRange = shape.getTextFrame().getTextRange(); textRange.getFill().setFillType(FillFormatType.SOLID); textRange.getFill().getSolidColor().setColor(new Color(238,130,238)); textRange.setFontHeight(20); x += (100 + ppt.getSlideSize().getSize().getWidth()/6); } x = 30; y += (100 + ppt.getSlideSize().getSize().getHeight()/7) ; } //保存文檔 ppt.saveToFile("TextWatermark2.pptx", FileFormat.PPTX_2013); ppt.dispose(); } }
這裏經過設置文本寬度值int width的值可實現不一樣的水印文本字符排列樣式,以下width = 110時,水印字樣效果:code
width=60時,水印效果:orm
width=10時,水印效果:htm
以上是本次介紹Java添加PPT文本水印的所有內容,添加單一水印和平鋪水印效果的基本方法相差不大。blog
(本文完,如需轉載,請務必註明出處!!!)文檔