Set text watermark
Text: | |
Font: | |
Font Size: | The font size must be a valid number. The font size must be a valid number. |
Color: | The format of color value must be rrggbb. |
Rotate: | |
downloads
|
Set image watermark
Image: |
Click here to browse files
CustomValidator |
downloads
|
- Demo
- Java
- C# source
This demo shows you how to add text watermark and image watermark to a PowerPoint document.
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); } } }
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); } } }
No Matter How Big or Small Your Project is,
Any technical question related to our product, contact us at support@e-iceblue.com.
Any question related to the purchase of product, contact us at sales@e-iceblue.com.
If you don't find the function you want, please request a free demo from us.