com.groupdocs.watermark

Class Watermarker

  • All Implemented Interfaces:
    Closeable, AutoCloseable


    public class Watermarker
    extends Object
    implements Closeable
    Represents a class for watermark management in a document.

    The following example demonstrates how to load and save a content of any supported format.

    // Load a content from a file.
       Watermarker watermarker = new Watermarker("D:\\input.pdf");
    
       // Use methods of Watermarker class to add, search or remove watermarks.
    
       // Save changes.
       watermarker.save("D:\\output.pdf");
    
       // Free resources.
       watermarker.close();
     
    • Constructor Detail

      • Watermarker

        public Watermarker(String filePath)
        Initializes a new instance of the Watermarker class with the specified document path.

        Learn more about loading documents: Loading documents.

        The following example demonstrates how to load and save a content of any supported format.

        // Load a content from a file.
           Watermarker watermarker = new Watermarker("D:\\input.pdf");
        
           // Use methods of Watermarker class to add, search or remove watermarks.
        
           // Save changes.
           watermarker.save("D:\\output.pdf");
        
           // Free resources.
           watermarker.close();
         
        Parameters:
        filePath - The file path to load the document from.
        Throws:
        UnsupportedFileTypeException - When supplied document type is not supported.
        InvalidPasswordException - When supplied password is incorrect.
      • Watermarker

        public Watermarker(String filePath,
                   LoadOptions options)
        Initializes a new instance of the Watermarker class with the specified document path and load options.

        Learn more about loading documents: Loading documents.

        The following example demonstrates how to load encrypted PDF document using password.

        PdfLoadOptions loadOptions = new PdfLoadOptions();
           loadOptions.setPassword("123");
           Watermarker watermarker = new Watermarker("C:\\Documents\\test.pdf", loadOptions);
           // ...
           watermarker.close();
         
        Parameters:
        filePath - The file path to load document from.
        options - Additional options to use when loading a document.
        Throws:
        UnsupportedFileTypeException - When supplied document type is not supported.
        InvalidPasswordException - When supplied password is incorrect.
      • Watermarker

        public Watermarker(String filePath,
                   WatermarkerSettings settings)
        Initializes a new instance of the Watermarker class with the specified document path and settings.

        Learn more about loading documents: Loading documents.

        The following example demonstrates how to set searchable objects globally (for all documents that will be loaded after that).

        WatermarkerSettings settings = new WatermarkerSettings();
           settings.setSearchableObjects(new SearchableObjects());
           settings.getSearchableObjects().setWordProcessingSearchableObjects(WordProcessingSearchableObjects.Hyperlinks
                                                                            | WordProcessingSearchableObjects.Text);
           settings.getSearchableObjects().setSpreadsheetSearchableObjects(SpreadsheetSearchableObjects.HeadersFooters);
           settings.getSearchableObjects().setPresentationSearchableObjects(PresentationSearchableObjects.SlidesBackgrounds
                                                                          | PresentationSearchableObjects.Shapes);
           settings.getSearchableObjects().setDiagramSearchableObjects(DiagramSearchableObjects.None);
           settings.getSearchableObjects().setPdfSearchableObjects(PdfSearchableObjects.All);
        
           for (final File fileEntry : new File("D:\\files").listFiles())
           {
              if (fileEntry.isFile()) {
                  Watermarker watermarker = new Watermarker(fileEntry.getPath(), settings);
                  PossibleWatermarkCollection watermarks = watermarker.search();
                  // The code for working with found watermarks goes here.
                  watermarker.close();
              }
           }
         
        Parameters:
        filePath - The file path to load document from.
        settings - Additional settings to use when working with loaded document.
        Throws:
        UnsupportedFileTypeException - When supplied document type is not supported.
        InvalidPasswordException - When supplied password is incorrect.
      • Watermarker

        public Watermarker(String filePath,
                   LoadOptions options,
                   WatermarkerSettings settings)
        Initializes a new instance of the Watermarker class with the specified document path, load options and settings.

        Learn more about loading documents: Loading documents.

        The following example demonstrates how to find particular text fragments in email message body/subject.

        WatermarkerSettings settings = new WatermarkerSettings();
           settings.setSearchableObjects(new SearchableObjects());
           settings.getSearchableObjects().setEmailSearchableObjects(EmailSearchableObjects.Subject
                                                                   | EmailSearchableObjects.HtmlBody
                                                                   | EmailSearchableObjects.PlainTextBody);
           EmailLoadOptions loadOptions = new EmailLoadOptions(); 
           Watermarker watermarker = new Watermarker("D:\\test.msg", loadOptions, settings);
           SearchCriteria criteria = new TextSearchCriteria("test", false);
           // Note, search is performed only if you pass TextSearchCriteria instance to search() method
           PossibleWatermarkCollection watermarks = watermarker.search(criteria);
           // Remove found text fragments
           watermarks.clear();
           // Save changes
           watermarker.save("D:\\modified_test.msg");
           watermarker.close();
         
        Parameters:
        filePath - The file path to load document from.
        options - Additional options to use when loading a document.
        settings - Additional settings to use when working with loaded document.
        Throws:
        UnsupportedFileTypeException - When supplied document type is not supported.
        InvalidPasswordException - When supplied password is incorrect.
      • Watermarker

        public Watermarker(InputStream document)
        Initializes a new instance of the Watermarker class with the specified stream.

        Learn more about loading documents: Loading documents.

        The following example demonstrates how to load and save a document of any supported format.

        // Load a content from a stream.
           FileInputStream inputStream = new FileInputStream("D:\\input.pdf");
           FileOutputStream outputStream = new FileOutputStream("D:\\output.pdf");
           Watermarker watermarker = new Watermarker(inputStream);
           // Use methods of Watermarker class to add, search or remove watermarks.
        
           // Save changes.
           watermarker.save(outputStream);
           watermarker.close();
           outputStream.close();
           inputStream.close();
         
        Parameters:
        document - The stream to load document from.
        Throws:
        UnsupportedFileTypeException - When supplied document type is not supported.
        InvalidPasswordException - When supplied password is incorrect.
      • Watermarker

        public Watermarker(InputStream document,
                   LoadOptions options)
        Initializes a new instance of the Watermarker class with the specified stream and load options.

        Learn more about loading documents: Loading documents.

        The following example demonstrates how to load encrypted PDF document using password

        PdfLoadOptions loadOptions = new PdfLoadOptions();
           loadOptions.setPassword("123");
           FileInputStream inputStream = new FileInputStream("D:\\input.pdf");
           Watermarker watermarker = new Watermarker(inputStream, loadOptions);
           // ...
        
           watermarker.close();
           inputStream.close();
         
        Parameters:
        document - The stream to load document from.
        options - Additional options to use when loading a document.
        Throws:
        UnsupportedFileTypeException - When supplied document type is not supported.
        InvalidPasswordException - When supplied password is incorrect.
      • Watermarker

        public Watermarker(InputStream document,
                   WatermarkerSettings settings)
        Initializes a new instance of the Watermarker class with the specified stream and settings.

        Learn more about loading documents: Loading documents.

        The following example demonstrates how to set searchable objects globally (for all documents that will be loaded after that).

        WatermarkerSettings settings = new WatermarkerSettings();
           settings.setSearchableObjects(new SearchableObjects());
           settings.getSearchableObjects().setWordProcessingSearchableObjects(WordProcessingSearchableObjects.Hyperlinks
                                                                            | WordProcessingSearchableObjects.Text);
           settings.getSearchableObjects().setSpreadsheetSearchableObjects(SpreadsheetSearchableObjects.HeadersFooters);
           settings.getSearchableObjects().setPresentationSearchableObjects(PresentationSearchableObjects.SlidesBackgrounds
                                                                          | PresentationSearchableObjects.Shapes);
           settings.getSearchableObjects().setDiagramSearchableObjects(DiagramSearchableObjects.None);
           settings.getSearchableObjects().setPdfSearchableObjects(PdfSearchableObjects.All);
        
           for (final File fileEntry : new File("D:\\files").listFiles())
           {
              if (fileEntry.isFile()) {
                  FileInputStream inputStream = new FileInputStream(fileEntry.getPath());
                  Watermarker watermarker = new Watermarker(inputStream, settings);
                  PossibleWatermarkCollection watermarks = watermarker.Search();
                  // The code for working with found watermarks goes here.
                  watermarker.close();
                  inputStream.close();
              }
           }
         
        Parameters:
        document - The stream to load document from.
        settings - Additional settings to use when working with loaded document.
        Throws:
        UnsupportedFileTypeException - When supplied document type is not supported.
        InvalidPasswordException - When supplied password is incorrect.
      • Watermarker

        public Watermarker(InputStream document,
                   LoadOptions options,
                   WatermarkerSettings settings)
        Initializes a new instance of the class with the specified stream, load options and settings.

        Learn more about loading documents: Loading documents.

        The following example demonstrates how to set searchable objects globally (for all documents that will be loaded after that).

        WatermarkerSettings settings = new WatermarkerSettings();
           settings.setSearchableObjects(new SearchableObjects());
           settings.getSearchableObjects().setEmailSearchableObjects(EmailSearchableObjects.Subject
                                                                   | EmailSearchableObjects.HtmlBody
                                                                   | EmailSearchableObjects.PlainTextBody);
           FileInputStream inputStream = new FileInputStream("D:\\test.msg");
           FileOutputStream outputStream = new FileOutputStream("D:\\modified_test.msg");
           EmailLoadOptions loadOptions = new EmailLoadOptions(); 
           Watermarker watermarker = new Watermarker(inputStream, loadOptions, settings);
           SearchCriteria criteria = new TextSearchCriteria("test", false);
           // Note, search is performed only if you pass TextSearchCriteria instance to search() method
           PossibleWatermarkCollection watermarks = watermarker.search(criteria);
           // Remove found text fragments
           watermarks.clear();
           // Save changes
           watermarker.save(outputStream);
           watermarker.close();
           outputStream.close();
           inputStream.close();
         
        Parameters:
        document - The stream to load document from.
        options - Additional options to use when loading a document.
        settings - Additional settings to use when working with loaded document.
        Throws:
        UnsupportedFileTypeException - When supplied document type is not supported.
        InvalidPasswordException - When supplied password is incorrect.
    • Method Detail

      • getSearchableObjects

        public final SearchableObjects getSearchableObjects()
        Gets the content objects that are to be included in a watermark search.

        This mathod also specifies content objects which are used in image search. For more information see Watermarker.search(SearchCriteria) and Watermarker.getImages() methods.

        Learn more about searching watermarks: Searching watermarks.

        The following example demonstrates how to remove all XObjects and Artifacts from a pdf document.

        Watermarker watermarker = new Watermarker("D:\\test.pdf");
           watermarker.getSearchableObjects().setPdfSearchableObjects(PdfSearchableObjects.XObjects
                                                                    | PdfSearchableObjects.Artifacts);
           PossibleWatermarkCollection watermarks = watermarker.search();
           // Remove all found objects.
           watermarks.clear();
           // Save changes.
           watermarker.save("D:\\modified_test.pdf");
           watermarker.close();
         
        Returns:
        The objects that are to be included in a watermark search.
      • setSearchableObjects

        public final void setSearchableObjects(SearchableObjects value)
        Sets the content objects that are to be included in a watermark search.

        This mathod also specifies content objects which are used in image search. For more information see Watermarker.search(SearchCriteria) and Watermarker.getImages() methods. Learn more about searching watermarks: Searching watermarks.

        The following example demonstrates how to remove all XObjects and Artifacts from a pdf document.

        Watermarker watermarker = new Watermarker("D:\\test.pdf");
           watermarker.getSearchableObjects().setPdfSearchableObjects(PdfSearchableObjects.XObjects
                                                                    | PdfSearchableObjects.Artifacts);
           PossibleWatermarkCollection watermarks = watermarker.search();
           // Remove all found objects.
           watermarks.clear();
           // Save changes.
           watermarker.save("D:\\modified_test.pdf");
           watermarker.close();
         
        Parameters:
        value - The objects that are to be included in a watermark search.
      • close

        public final void close()

        Disposes the current instance.

        Specified by:
        close in interface Closeable
        Specified by:
        close in interface AutoCloseable
      • getDocumentInfo

        public final IDocumentInfo getDocumentInfo()
        Gets the information about the format of the loaded document.

        Learn more about getting the document information Get document info.

        The following example demonstrates how to get information about a document of any supported type.

        Watermarker watermarker = new Watermarker("D:\\test.ppt");
           IDocumentInfo info = watermarker.getDocumentInfo();
           System.out.println("File type: " + info.getFileType());
           System.out.println("Number of pages: " + info.getPageCount());
           System.out.println("Document size: " + info.getSize() + " bytes");
           watermarker.close();
         
        Returns:
        The IDocumentInfo instance that contains detected information.
      • add

        public final void add(Watermark watermark)
        Adds a watermark to the loaded document.

        Learn more about adding watermarks: Adding watermarks.

        The following example demonstrates how to add image and text watermark to a document of any supported type.

        Watermarker watermarker = new Watermarker("D:\\input.pdf");
        
           TextWatermark textWatermark = new TextWatermark("DRAFT", new Font("Arial", 19));
           textWatermark.setHorizontalAlignment(HorizontalAlignment.Center);
           textWatermark.setVerticalAlignment(VerticalAlignment.Top);
           textWatermark.setConsiderParentMargins(true);
           textWatermark.setForegroundColor(Color.getRed());
           textWatermark.setBackground(true);
           textWatermark.setOpacity(0.5);
           watermarker.add(textWatermark);
         
           ImageWatermark imageWatermark = new ImageWatermark("D:\\draft.png"));
           imageWatermark.setHorizontalAlignment(HorizontalAlignment.Center);
           imageWatermark.setVerticalAlignment(VerticalAlignment.Bottom);
           imageWatermark.setConsiderParentMargins(true);
           imageWatermark.setBackground(true);
           imageWatermark.setOpacity(0.5);
           watermarker.add(imageWatermark);
           imageWatermark.close();
        
           watermarker.save("D:\\output.pdf");
           watermarker.close();
         
        Parameters:
        watermark - The watermark to add to the document.
      • add

        public final void add(Watermark watermark,
               WatermarkOptions options)
        Adds a watermark to the loaded document using watermark options.

        Learn more about adding watermarks: Adding watermarks.

        The following example demonstrates how to add an image watermark to a particular page of a pdf document.

        PdfLoadOptions loadOptions = new PdfLoadOptions();
           Watermarker watermarker = new Watermarker("D:\\input.pdf", loadOptions);
        
           ImageWatermark imageWatermark = new ImageWatermark("D:\\draft.png"));
           PdfXObjectWatermarkOptions options = new PdfXObjectWatermarkOptions();
           options.setPageIndex(0);
           watermarker.add(imageWatermark, options);
           imageWatermark.close();
        
           watermarker.save("D:\\output.pdf");
           watermarker.close();
         
        Parameters:
        watermark - The watermark to add to the document.
        options - Additional options to use when adding the watermark.
      • remove

        public final void remove(PossibleWatermark possibleWatermark)
        Removes watermark from the document.

        Learn more about removing watermarks: Removing found watermarks.

        The following example demonstrates how to find and remove the first possible watermark containing particular text or image from a document of any supported type.

        Watermarker watermarker = new Watermarker("D:\\input.doc");
           ImageSearchCriteria imageSearchCriteria = new ImageDctHashSearchCriteria("D:\\logo.png");
           TextSearchCriteria textSearchCriteria = new TextSearchCriteria(Pattern.compile("^Company\\sName$"));
           PossibleWatermarkCollection watermarks = watermarker.search(textSearchCriteria.or(imageSearchCriteria));
           if (watermarks.getCount() > 0)
           {
               watermarker.remove(watermarks.get_Item(0));
           }
        
           watermarker.save("D:\\output.doc");
           watermarker.close();
         
        Parameters:
        possibleWatermark - The watermark to remove.
      • remove

        public final void remove(PossibleWatermarkCollection possibleWatermarks)
        Removes all watermarks in the collection from the document.

        Learn more about removing watermarks: Removing found watermarks.

        The following example demonstrates how to find and remove all possible watermarks containing particular text or image from a document of any supported type.

        Watermarker watermarker = new Watermarker("D:\\input.doc");
           ImageSearchCriteria imageSearchCriteria = new ImageDctHashSearchCriteria("D:\\logo.png");
           TextSearchCriteria textSearchCriteria = new TextSearchCriteria(Pattern.compile("^Company\\sName$"));
           PossibleWatermarkCollection watermarks = watermarker.search(textSearchCriteria.or(imageSearchCriteria));
           watermarker.remove(watermarks);
           watermarker.save("D:\\output.doc");
           watermarker.close();
         
        Parameters:
        possibleWatermarks - The collection of watermarks to remove.
      • save

        public final void save(String filePath)
        Saves the document to the specified file location.

        Learn more about saving the documents Saving documents.

        The following example demonstrates how to add the watermark and save the document to another file.

        Watermarker watermarker = new Watermarker("input.pdf");
           TextWatermark watermark = new TextWatermark("top secret", new Font("Arial", 36));
           watermarker.add(watermark);
           watermarker.save("output.pdf");
           watermarker.close();
         
        Parameters:
        filePath - The file path to save the document data to.
      • save

        public final void save(OutputStream document)
        Saves the document to the specified stream.

        Learn more about saving the documents Saving documents.

        The following example demonstrates how to add watermark and save the document to the memory stream.

        Watermarker watermarker = new Watermarker("input.pdf");
           TextWatermark watermark = new TextWatermark("top secret", new Font("Arial", 36));
           watermarker.add(watermark);
           ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
           watermarker.save(outputStream);
           // ...
           outputStream.close();
           watermarker.close();
         
        Parameters:
        document - The stream to save the document data to.
      • save

        public final void save(String filePath,
                SaveOptions options)
        Saves the document to the specified file location using save options.

        Learn more about saving the documents Saving documents.

        The following example demonstrates how to add the watermark and save the document to another file with default SaveOptions.

        Watermarker watermarker = new Watermarker("input.pdf");
           TextWatermark watermark = new TextWatermark("top secret", new Font("Arial", 36));
           watermarker.add(watermark);
           watermarker.save("output.pdf", new SaveOptions());
           watermarker.close();
         
        Parameters:
        filePath - The file path to save the document data to.
        options - Additional options to use when saving a document.
      • save

        public final void save(OutputStream document,
                SaveOptions options)
        Saves the document to the specified stream using save options.

        Learn more about saving the documents Saving documents.

        The following example demonstrates how to add watermark and save the document to the memory stream with default SaveOptions.

        Watermarker watermarker = new Watermarker("input.pdf");
           TextWatermark watermark = new TextWatermark("top secret", new Font("Arial", 36));
           watermarker.add(watermark);
           ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
           watermarker.save(outputStream, new SaveOptions());
           // ...
           outputStream.close();
           watermarker.close();
         
        Parameters:
        document - The stream to save the document data to.
        options - Additional options to use when saving a document.
      • generatePreview

        public final void generatePreview(PreviewOptions previewOptions)
        Generates preview images for the document.
        Parameters:
        previewOptions - Additional options to use when generating preview images.
      • search

        public final PossibleWatermarkCollection search()
        Searches all possible watermarks in the document.

        The search is conducted in objects specified with Watermarker.setSearchableObjects(SearchableObjects).

        Learn more about searching watermarks Searching watermarks.

        The following example demonstrates how to count the possible watermarks in a document of any supported type.

        Watermarker watermarker = new Watermarker("input.doc");
           PossibleWatermarkCollection watermarks = watermarker.search();
           System.out.println(watermarks.getCount());
           watermarker.close();
         
      • search

        public final PossibleWatermarkCollection search(SearchCriteria searchCriteria)
        Searches possible watermarks according to specified search criteria.

        The search is conducted in objects specified with Watermarker.setSearchableObjects(SearchableObjects).

        Learn more about searching watermarks Searching watermarks.

        The following example demonstrates how to find and remove all possible watermarks containing particular text or image from a document of any supported type.

        Watermarker watermarker = new Watermarker("input.doc");
           ImageSearchCriteria imageSearchCriteria = new ImageDctHashSearchCriteria("logo.png");
           TextSearchCriteria textSearchCriteria = new TextSearchCriteria(Pattern.compile("^Company\\sName$"));
           PossibleWatermarkCollection watermarks = watermarker.search(textSearchCriteria.or(imageSearchCriteria));
           watermarks.clear();
           watermarker.save("output.doc");
           watermarker.close();
         
        Parameters:
        searchCriteria - The search criteria to use.
      • getImages

        public final WatermarkableImageCollection getImages(ImageSearchCriteria searchCriteria)
        Finds images according to specified search criteria.

        The search is conducted in objects specified with Watermarker.setSearchableObjects(SearchableObjects).

        Learn more about searching watermarks Searching watermarks.

        The following example demonstrates how to remove all images that are similar to the reference from a document of any supported type.

        Watermarker watermarker = new Watermarker("input.doc");
           ImageThumbnailSearchCriteria criteria = new ImageThumbnailSearchCriteria("reference.png");
           WatermarkableImageCollection images = watermarker.getImages(criteria);
           images.clear();
           watermarker.save("output.doc");
           watermarker.close();
         
        Parameters:
        searchCriteria - The search criteria to use.
      • getImages

        public final WatermarkableImageCollection getImages()
        Finds all images in the document.

        The search is conducted in objects specified with Watermarker.setSearchableObjects(SearchableObjects).

        Learn more about searching watermarks Searching watermarks.

        The following example demonstrates how to add watermark to all images in a document of any supported type.

        Watermarker watermarker = new Watermarker("input.doc");
           // Initialize text or image watermark.
           TextWatermark watermark = new TextWatermark("DRAFT", new Font("Arial", 19));
        
           // Find all images in the document.
           WatermarkableImageCollection images = watermarker.getImages();
        
           // Add watermark.
           for (WatermarkableImage watermarkableImage : images)
           {
               watermarkableImage.addWatermark(watermark);
           }
        
           // Save changes.
           watermarker.save("output.doc");
           watermarker.close();
         
        Returns:
        The collection of found images.
      • getContent

        public final <T extends Content> T getContent(Class<T> contentType)
        Returns the Content object for the loaded document.

        The following example demonstrates how to rasterize pdf document page with added watermark.

        PdfLoadOptions loadOptions = new PdfLoadOptions();
           Watermarker watermarker = new Watermarker("input.pdf", loadOptions);
           ImageWatermark watermark = new ImageWatermark("watermark.png")
           watermarker.add(watermark);
           PdfContent content = watermarker.getContent(PdfContent.class);
           content.rasterize(300, 300, PdfImageConversionFormat.Png);
           watermarker.save("output.pdf");
           watermarker.close();
         
        Type Parameters:
        T - The requested type of a Content object.
        Parameters:
        contentType - The requested type of a Content object.
        Returns:
        The Content object for the loaded document.