Working with slide backgrounds

Extracting information about all slide backgrounds

The API allows you to extract information about all the slide backgrounds in a PowerPoint document as shown in the following code sample using method getBackgroundImage() of PresentationSlide.getImageFillFormat().

advanced_usage.add_watermarks_to_presentations.PresentationGetSlideBackgroundsInformation

PresentationLoadOptions loadOptions = new PresentationLoadOptions();                                               
// Constants.InPresentationPptx is an absolute or relative path to your document. Ex: "C:\\Docs\\presentation.pptx"
Watermarker watermarker = new Watermarker(Constants.InPresentationPptx, loadOptions);                              
                                                                                                                   
PresentationContent content = watermarker.getContent(PresentationContent.class);                                   
for (PresentationSlide slide : content.getSlides())                                                                
{                                                                                                                  
    if (slide.getImageFillFormat().getBackgroundImage() != null)                                                   
    {                                                                                                              
        System.out.println(slide.getImageFillFormat().getBackgroundImage().getWidth());                            
        System.out.println(slide.getImageFillFormat().getBackgroundImage().getHeight());                           
        System.out.println(slide.getImageFillFormat().getBackgroundImage().getBytes().length);                     
    }                                                                                                              
}                                                                                                                  
                                                                                                                   
watermarker.close();                                                                                               

Removing a particular background

Following code sample shows how to remove the background image of a particular slide calling the method setBackgroundImage() with null.

advanced_usage.add_watermarks_to_presentations.PresentationRemoveSlideBackground

PresentationLoadOptions loadOptions = new PresentationLoadOptions();                                               
// Constants.InPresentationPptx is an absolute or relative path to your document. Ex: "C:\\Docs\\presentation.pptx"
Watermarker watermarker = new Watermarker(Constants.InPresentationPptx, loadOptions);                              
                                                                                                                   
PresentationContent content = watermarker.getContent(PresentationContent.class);                                   
content.getSlides().get_Item(0).getImageFillFormat().setBackgroundImage(null);                                     
                                                                                                                   
watermarker.save(Constants.OutPresentationPptx);                                                                   
                                                                                                                   
watermarker.close();                                                                                               

Adding watermark to all background Images

Using GroupDocs.Watermark, you can also add watermark to the background images that belong to a PowerPoint document as shown in the following code sample.

advanced_usage.add_watermarks_to_presentations.PresentationAddWatermarkToSlideBackgroundImages

PresentationLoadOptions loadOptions = new PresentationLoadOptions();                                               
// Constants.InPresentationPptx is an absolute or relative path to your document. Ex: "C:\\Docs\\presentation.pptx"
Watermarker watermarker = new Watermarker(Constants.InPresentationPptx, loadOptions);                              
                                                                                                                   
// Initialize image or text watermark                                                                              
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);                                                                                       
                                                                                                                   
PresentationContent content = watermarker.getContent(PresentationContent.class);                                   
for (PresentationSlide slide : content.getSlides())                                                                
{                                                                                                                  
    if (slide.getImageFillFormat().getBackgroundImage() != null)                                                   
    {                                                                                                              
        // Add watermark to the image                                                                              
        slide.getImageFillFormat().getBackgroundImage().add(watermark);                                            
    }                                                                                                              
}                                                                                                                  
                                                                                                                   
watermarker.save(Constants.OutPresentationPptx);                                                                   
                                                                                                                   
watermarker.close();                                                                                               

Additional settings for slide background image

GroupDocs.Watermark for Java also provides the feature that allows you to tile the picture across slide’s background. You can also make the image semi-transparent. Following code sample serves this purpose.

advanced_usage.add_watermarks_to_presentations.PresentationSetTiledSemitransparentBackground

PresentationLoadOptions loadOptions = new PresentationLoadOptions();                                               
// Constants.InPresentationPptx is an absolute or relative path to your document. Ex: "C:\\Docs\\presentation.pptx"
Watermarker watermarker = new Watermarker(Constants.InPresentationPptx, loadOptions);                              
                                                                                                                   
PresentationContent content = watermarker.getContent(PresentationContent.class);                                   
PresentationSlide slide = content.getSlides().get_Item(0);                                                         
                                                                                                                   
File imageFile = new File(Constants.BackgroundPng);                                                                
byte[] imageBytes = new byte[(int) imageFile.length()];                                                            
InputStream imageInputStream = new FileInputStream(imageFile);                                                     
imageInputStream.read(imageBytes);                                                                                 
imageInputStream.close();                                                                                           
                                                                                                                   
slide.getImageFillFormat().setBackgroundImage(new PresentationWatermarkableImage(imageBytes));                     
slide.getImageFillFormat().setTileAsTexture(true);                                                                 
slide.getImageFillFormat().setTransparency(0.5);                                                                   
                                                                                                                   
watermarker.save(Constants.OutPresentationPptx);                                                                   
                                                                                                                   
watermarker.close();                                                                                               

Settings background image for charts

GroupDocs.Watermark for Java also allows you to set the background image for a chart inside PowerPoint document using getCharts() method. You can use following code sample to achieve this functionality.

advanced_usage.add_watermarks_to_presentations.PresentationSetBackgroundImageForChart

PresentationLoadOptions loadOptions = new PresentationLoadOptions();                                                                             
// Constants.InPresentationPptx is an absolute or relative path to your document. Ex: "C:\\Docs\\presentation.pptx"                              
Watermarker watermarker = new Watermarker(Constants.InPresentationPptx, loadOptions);                                                            
                                                                                                                                                 
PresentationContent content = watermarker.getContent(PresentationContent.class);                                                                 
                                                                                                                                                 
File imageFile = new File(Constants.TestPng);                                                                                                    
byte[] imageBytes = new byte[(int) imageFile.length()];                                                                                          
InputStream imageInputStream = new FileInputStream(imageFile);                                                                                   
imageInputStream.read(imageBytes);                                                                                                               
imageInputStream.close();                                                                                                                         
                                                                                                                                                 
                                                                                                                                                 
content.getSlides().get_Item(0).getCharts().get_Item(0).getImageFillFormat().setBackgroundImage(new PresentationWatermarkableImage(imageBytes)); 
content.getSlides().get_Item(0).getCharts().get_Item(0).getImageFillFormat().setTransparency(0.5);                                               
content.getSlides().get_Item(0).getCharts().get_Item(0).getImageFillFormat().setTileAsTexture(true);                                             
                                                                                                                                                 
watermarker.save(Constants.OutPresentationPptx);                                                                                                 
                                                                                                                                                 
watermarker.close();                                                                                                                             

More resources

GitHub examples

You may easily run the code above and see the feature in action in our GitHub examples:

Free online document watermarking App

Along with full featured Java library we provide simple, but powerful free Apps.

You are welcome to add watermark to PDF, DOC, DOCX, PPT, PPTX, XLS, XLSX, Emails and more with our free online Free Online Document Watermarking App.