Tab 1
此演示向您展示如何将文本水印和图像水印添加到 PowerPoint 文档。
Set text watermark
Text: | |
Font: | |
Font Size: | |
Color: | |
Rotate: |
e-iceblue
|
downloads
|
Set image watermark
Image: |
Click here to browse files
|
![]() |
|
downloads
|
如果这不是您想要的 Demo,您可以通过填写表格获取免费定制 Demo。
如您有与我们产品相关的其他技术问题,请联系 该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。;销售相关的问题,请联系 该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。。
Tab 2
package ppt;
import com.spire.presentation.*;
import com.spire.presentation.drawing.BackgroundType;
import com.spire.presentation.drawing.FillFormatType;
import com.spire.presentation.drawing.IImageData;
import com.spire.presentation.drawing.PictureFillType;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
public class WatermarkDemo {
public void watermark(String filePath, String watermarkType, String text, String imageFile, String resultFilePath) throws Exception {
Presentation presentation = new Presentation();
presentation.loadFromFile(filePath);
switch (watermarkType){
case "TEXT":
addTextWatermark(presentation,text);
break;
case "IMAGE":
File file = new File(imageFile);
BufferedImage bufferedImage = ImageIO.read(file);
addImageWatermark(presentation,bufferedImage);
break;
}
presentation.saveToFile(resultFilePath,FileFormat.PPTX_2013);
}
private void addTextWatermark(Presentation presentation, String text) throws Exception {
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);
for (int i = 0; i< presentation.getSlides().getCount();i++){
IAutoShape shape = presentation.getSlides().get(i).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(text);
PortionEx textRange = shape.getTextFrame().getTextRange();
TextFont font = new TextFont("Arial Rounded MT Bold");
textRange.setLatinFont(font);
textRange.getFill().setFillType(FillFormatType.SOLID);
textRange.getFill().getSolidColor().setColor(Color.RED);
textRange.setFontHeight(22);
}
}
private void addImageWatermark(Presentation presentation, BufferedImage bufferedImage) throws DocumentEditException {
IImageData image = presentation.getImages().append(bufferedImage);
for (int i = 0; i< presentation.getSlides().getCount();i++) {
ISlide slide = presentation.getSlides().get(i);
slide.getSlideBackground().setType(BackgroundType.CUSTOM);
slide.getSlideBackground().getFill().setFillType(FillFormatType.PICTURE);
slide.getSlideBackground().getFill().getPictureFill().setFillType(PictureFillType.STRETCH);
slide.getSlideBackground().getFill().getPictureFill().getPicture().setEmbedImage(image);
}
}
}
Tab 3
using Spire.Presentation;
using Spire.Presentation.Drawing;
using System.Drawing;
using System.Windows.Forms;
namespace DemoOnlineCode
{
class AddWatermark
{
public void AddTextOrImageWatermarkDemo(string filePath,string format,string text, string imagePath, string resultFileName)
{
Presentation presentation = new Presentation();
presentation.LoadFromFile(filePath);
switch (format)
{
case "TEXT":
AddTextWatermark(presentation,text,resultFileName);
break;
case "IMAGE":
AddImageWatermark(presentation,imagePath,resultFileName);
break;
}
}
private static void AddTextWatermark(Presentation presentation, string text, string resultFileName)
{
Form frm = new Form();
Graphics gc = frm.CreateGraphics();
SizeF size = gc.MeasureString(text, new Font("Lucida Sans Unicode", 50));
//Define a rectangle range
RectangleF rect = new RectangleF((presentation.SlideSize.Size.Width - size.Width) / 2, (presentation.SlideSize.Size.Height - size.Height) / 2, size.Width, size.Height);
foreach (ISlide slide in presentation.Slides)
{
IAutoShape shape =slide.Shapes.AppendShape(ShapeType.Rectangle, rect);
//Set the style of the shape
shape.Fill.FillType = FillFormatType.None;
shape.ShapeStyle.LineColor.Color = Color.White;
shape.Rotation = -45;
shape.Locking.SelectionProtection = true;
shape.Line.FillType = FillFormatType.None;
//Add text to the shape
shape.TextFrame.Text = text;
TextRange textRange = shape.TextFrame.TextRange;
//Set the style of the text range
textRange.Fill.FillType = FillFormatType.Solid;
textRange.Fill.SolidColor.Color = Color.FromArgb(120, Color.HotPink);
textRange.FontHeight = 50;
}
presentation.SaveToFile(resultFileName + ".pptx", FileFormat.Pptx2013);
}
private static void AddImageWatermark(Presentation presentation, string imageFile, string resultFileName)
{
IImageData image = presentation.Images.Append(Image.FromFile(imageFile));
foreach (ISlide slide in presentation.Slides)
{
slide.SlideBackground.Type = BackgroundType.Custom;
slide.SlideBackground.Fill.FillType = FillFormatType.Picture;
slide.SlideBackground.Fill.PictureFill.FillType = PictureFillType.Stretch;
slide.SlideBackground.Fill.PictureFill.Picture.EmbedImage = image;
}
presentation.SaveToFile(resultFileName + ".pptx", FileFormat.Pptx2013);
}
}
}