“水印”的含义有不同的概念,但是通常我们需要在文档中添加水印是为了防止他人进行未经授权的复制,或者是在文档中添加背景以显示公司徽标。 本节将向您展示如何在Java应用程序的PowerPoint文档中添加文本水印和图像水印。
添加图像水印
图像watermakr用于使演示幻灯片更具吸引力,并显示演示幻灯片的版权信息。 演示幻灯片上的图像水印实际上是使其看起来像水印的图像背景。
import com.spire.presentation.* ;
import com.spire.presentation.drawing.FillFormatType ;
import com.spire.presentation.SlideBackground ;
import com.spire.presentation.drawing.* ;
import javax.imageio.ImageIO ;
import java.awt.image.BufferedImage ;
import java.io.FileInputStream ;
public class ImageWatermark {
public static void main ( String [] args ) throws Exception {
//Create a PPT document and load file
Presentation presentation = new Presentation ();
presentation . loadFromFile ( "Sample.pptx" );
//presentation.getSlides().get(0).getSlideBackground().getType()= presentation.;
BufferedImage bufferedImage = ImageIO . read ( new FileInputStream ( "ima.png" ));
IImageData imageData = presentation . getImages (). append ( bufferedImage );
//apply the image to the specific slide as background
SlideBackground background = presentation . getSlides (). get ( 1 ). getSlideBackground ();
background . setType ( BackgroundType . CUSTOM );
background . getFill (). setFillType ( FillFormatType . PICTURE );
background . getFill (). getPictureFill (). setFillType ( PictureFillType . STRETCH );
background . getFill (). getPictureFill (). getPicture (). setEmbedImage ( imageData );
//save the file
presentation . saveToFile ( "output/ImageWatermark.pptx" , FileFormat . PPTX_2013 );
presentation . dispose ();
}
}
演示幻灯片上图像水印的输出:
添加文字水印
文本水印基于形状,并且使用形状的锁定属性来保护形状不被编辑或复制。
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 {
//Create a PPT document and load file
Presentation presentation = new Presentation ();
presentation . loadFromFile ( "Sample.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 ) / 2 ,
( presentation . getSlideSize (). getSize (). getHeight () - height ) / 2 , width , height );
//Add a rectangle shape with a defined range
IAutoShape shape = presentation . getSlides (). get ( 0 ). 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 ( "E-iceblue" );
PortionEx textRange = shape . getTextFrame (). getTextRange ();
//Set the style of the text range
textRange . getFill (). setFillType ( FillFormatType . SOLID );
textRange . getFill (). getSolidColor (). setColor ( Color . pink );
textRange . setFontHeight ( 50 );
//Save the document
presentation . saveToFile ( "output/TextWatermark.pptx" , FileFormat . PPTX_2010 );
}
}
From: https://dev.to/eiceblue/how-to-add-image-and-text-watermark-to-presentation-slides-bib