Add watermarks to presentation documents
Using GroupDocs.Watermark, you can add watermark to a particular slide of a PowerPoint presentation in a simplified way. Adding watermark to a particular PowerPoint slide using GroupDocs.Watermark consists of following steps.
- Load the document
- Create and initialize watermark object
- Set watermark properties
- Call setSlideIndex() of PresentationWatermarkSlideOptions
- Add watermark to the document
- Save the document
Following code shows how to add TextWatermark to the first slide and ImageWatermark to the second slide.
advanced_usage.add_watermarks_to_presentations.PresentationAddWatermarkToSlide
PresentationLoadOptions loadOptions = new PresentationLoadOptions();
// Specify an absolute or relative path to your document. Ex: "C:\\Docs\\presentation.pptx"
Watermarker watermarker = new Watermarker("presentation.pptx", loadOptions);
// Add text watermark to the first slide
TextWatermark textWatermark = new TextWatermark("Test watermark", new Font("Arial", 8));
PresentationWatermarkSlideOptions textWatermarkOptions = new PresentationWatermarkSlideOptions();
textWatermarkOptions.setSlideIndex(0);
watermarker.add(textWatermark, textWatermarkOptions);
// Add image watermark to the second slide
ImageWatermark imageWatermark = new ImageWatermark("logo.jpg");
PresentationWatermarkSlideOptions imageWatermarkOptions = new PresentationWatermarkSlideOptions();
imageWatermarkOptions.setSlideIndex(1);
watermarker.add(imageWatermark, imageWatermarkOptions);
watermarker.save("presentation.pptx");
watermarker.close();
imageWatermark.close();
This feature allows strengthening the protection of text watermark. Using unreadable characters in the watermark text forbids the modification using Find and Replace dialog. The following code sample shows how to include unreadable characters in watermark text using methods setLocked() and setProtectWithUnreadableCharacters() of PresentationWatermarkSlideOptions.
advanced_usage.add_watermarks_to_presentations.PresentationProtectWatermarkUsingUnreadableCharacters
PresentationLoadOptions loadOptions = new PresentationLoadOptions();
// Specify an absolute or relative path to your document. Ex: "C:\\Docs\\presentation.pptx"
Watermarker watermarker = new Watermarker("presentation.pptx", loadOptions);
TextWatermark watermark = new TextWatermark("Watermark text", new Font("Arial", 19));
PresentationWatermarkSlideOptions options = new PresentationWatermarkSlideOptions();
options.setLocked(true);
options.setProtectWithUnreadableCharacters(true);
// Add watermark
watermarker.add(watermark, options);
// Save document
watermarker.save("presentation.pptx");
watermarker.close();
If for some reasons you want to use absolute sizing and positioning, you may also need to get the width and height of the slide. Use below code to get the dimensions of a particular slide.
advanced_usage.add_watermarks_to_presentations.PresentationGetSlideDimensions
PresentationLoadOptions loadOptions = new PresentationLoadOptions();
// Specify an absolute or relative path to your document. Ex: "C:\\Docs\\presentation.pptx"
Watermarker watermarker = new Watermarker("presentation.pptx", loadOptions);
PresentationContent content = watermarker.getContent(PresentationContent.class);
System.out.println(content.getSlideWidth());
System.out.println(content.getSlideHeight());
watermarker.close();
GroupDocs.Watermark allows you to add watermark to the images inside a particular PowerPoint slide using add() method as shown in below example.
advanced_usage.add_watermarks_to_presentations.PresentationAddWatermarkToSlideImages
PresentationLoadOptions loadOptions = new PresentationLoadOptions();
// Specify an absolute or relative path to your document. Ex: "C:\\Docs\\presentation.pptx"
Watermarker watermarker = new Watermarker("presentation.pptx", loadOptions);
TextWatermark watermark = new TextWatermark("Protected image", new Font("Arial", 8));
watermark.setHorizontalAlignment(HorizontalAlignment.Center);
watermark.setVerticalAlignment(VerticalAlignment.Center);
watermark.setRotateAngle(45);
watermark.setSizingType(SizingType.ScaleToParentDimensions);
watermark.setScaleFactor(1);
// Get all images from the first slide
PresentationContent content = watermarker.getContent(PresentationContent.class);
WatermarkableImageCollection images = content.getSlides().get_Item(0).findImages();
// Add watermark to all found images
for (WatermarkableImage image : images)
{
image.add(watermark);
}
watermarker.save("presentation.pptx");
watermarker.close();
GroupDocs.Watermark enables you to access all types of the service slides in a PowerPoint presentation. Following methods of PresentationContent allows access to the coresponding slide types using the API
- getMasterSlides()
- getLayoutSlides()
- getMasterHandoutSlide()
- getMasterNotesSlide()
- getNotesSlide() (the method of PresentationSlide)
Following code shows how to access each type of the slides in a PowerPoint presentation.
advanced_usage.add_watermarks_to_presentations.PresentationAddWatermarkToAllSlideTypes
PresentationLoadOptions loadOptions = new PresentationLoadOptions();
// Specify an absolute or relative path to your document. Ex: "C:\\Docs\\presentation.pptx"
Watermarker watermarker = new Watermarker("presentation.pptx", loadOptions);
TextWatermark watermark = new TextWatermark("Test watermark", new Font("Arial", 8));
PresentationContent content = watermarker.getContent(PresentationContent.class);
// Add watermark to all master slides
PresentationWatermarkMasterSlideOptions masterSlideOptions = new PresentationWatermarkMasterSlideOptions();
masterSlideOptions.setMasterSlideIndex(-1);
watermarker.add(watermark, masterSlideOptions);
// Add watermark to all layout slides
if (content.getLayoutSlides() != null)
{
PresentationWatermarkLayoutSlideOptions layoutSlideOptions = new PresentationWatermarkLayoutSlideOptions();
layoutSlideOptions.setLayoutSlideIndex(-1);
watermarker.add(watermark, masterSlideOptions);
}
// Add watermark to all notes slides
for (int i = 0; i < content.getSlides().getCount(); i++)
{
if (content.getSlides().get_Item(i).getNotesSlide() != null)
{
PresentationWatermarkNoteSlideOptions noteSlideOptions = new PresentationWatermarkNoteSlideOptions();
noteSlideOptions.setSlideIndex(i);
watermarker.add(watermark, noteSlideOptions);
}
}
// Add watermark to handout master
if (content.getMasterHandoutSlide() != null)
{
PresentationWatermarkMasterHandoutSlideOptions handoutSlideOptions = new PresentationWatermarkMasterHandoutSlideOptions();
watermarker.add(watermark, handoutSlideOptions);
}
// Add watermark to notes master
if (content.getMasterNotesSlide() != null)
{
PresentationWatermarkMasterNotesSlideOptions masterNotesSlideOptions = new PresentationWatermarkMasterNotesSlideOptions();
watermarker.add(watermark, masterNotesSlideOptions);
}
watermarker.save("presentation.pptx");
watermarker.close();
When you’re calling add() method of Watermarker class with loaded presentation document, simple shape is added to a PowerPoint document. GroupDocs.Watermark provides some additional options when adding a shape watermark. Use PresentationWatermarkBaseSlideOptions descendant classes to set these options as shown in below example.
advanced_usage.add_watermarks_to_presentations.PresentationAddWatermarkWithSlidesShapeSettings
PresentationLoadOptions loadOptions = new PresentationLoadOptions();
// Specify an absolute or relative path to your document. Ex: "C:\\Docs\\presentation.pptx"
Watermarker watermarker = new Watermarker("presentation.pptx", loadOptions);
TextWatermark watermark = new TextWatermark("Test watermark", new Font("Arial", 19));
watermark.setBackground(true);
PresentationWatermarkSlideOptions options = new PresentationWatermarkSlideOptions();
// Set the shape name
options.setName("Shape 1");
// Set the descriptive (alternative) text that will be associated with the shape
options.setAlternativeText("Test watermark");
// Editing of the shape in PowerPoint is forbidden
options.setLocked(true);
watermarker.add(watermark, options);
watermarker.save("presentation.pptx");
watermarker.close();
You can also apply text effects when adding shape watermark to a PowerPoint slide.
advanced_usage.add_watermarks_to_presentations.PresentationAddWatermarkWithTextEffects
PresentationLoadOptions loadOptions = new PresentationLoadOptions();
// Specify an absolute or relative path to your document. Ex: "C:\\Docs\\presentation.pptx"
Watermarker watermarker = new Watermarker("presentation.pptx", loadOptions);
TextWatermark watermark = new TextWatermark("Test watermark", new Font("Segoe UI", 19));
PresentationTextEffects effects = new PresentationTextEffects();
effects.getLineFormat().setEnabled(true);
effects.getLineFormat().setColor(Color.getRed());
effects.getLineFormat().setDashStyle(OfficeDashStyle.DashDotDot);
effects.getLineFormat().setLineStyle(OfficeLineStyle.Triple);
effects.getLineFormat().setWeight(1);
PresentationWatermarkSlideOptions options = new PresentationWatermarkSlideOptions();
options.setEffects(effects);
watermarker.add(watermark, options);
watermarker.save("presentation.pptx");
watermarker.close();
WarningLine format settings are not supported for Ppt presentations at this moment.
The API also allows you to apply image effects to the shape watermark using below code.
advanced_usage.add_watermarks_to_presentations.PresentationAddWatermarkWithImageEffects
PresentationLoadOptions loadOptions = new PresentationLoadOptions();
// Specify an absolute or relative path to your document. Ex: "C:\\Docs\\presentation.pptx"
Watermarker watermarker = new Watermarker("presentation.pptx", loadOptions);
ImageWatermark watermark = new ImageWatermark("logo.png");
PresentationImageEffects effects = new PresentationImageEffects();
effects.setBrightness(0.7);
effects.setContrast(0.6);
effects.setChromaKey(Color.getRed());
effects.getBorderLineFormat().setEnabled(true);
effects.getBorderLineFormat().setWeight(1);
PresentationWatermarkSlideOptions options = new PresentationWatermarkSlideOptions();
options.setEffects(effects);
watermarker.add(watermark, options);
watermarker.save("presentation.pptx");
watermarker.close();
watermark.close();