public class Watermarker extends Object implements Closeable
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 and Description |
---|
Watermarker(InputStream document)
Initializes a new instance of the
class with the specified stream. |
Watermarker(InputStream document,
LoadOptions options)
Initializes a new instance of the
class with the specified stream
and load options. |
Watermarker(InputStream document,
LoadOptions options,
WatermarkerSettings settings)
Initializes a new instance of the
|
Watermarker(InputStream document,
WatermarkerSettings settings)
Initializes a new instance of the
class with the specified stream
and settings. |
Watermarker(String filePath)
Initializes a new instance of the
class with the specified document path. |
Watermarker(String filePath,
LoadOptions options)
Initializes a new instance of the
class with the specified
document path and load options. |
Watermarker(String filePath,
LoadOptions options,
WatermarkerSettings settings)
Initializes a new instance of the
class with the specified
document path, load options and settings. |
Watermarker(String filePath,
WatermarkerSettings settings)
Initializes a new instance of the
class with the specified
document path and settings. |
Modifier and Type | Method and Description |
---|---|
void |
add(Watermark watermark)
Adds a watermark to the loaded document.
|
void |
add(Watermark watermark,
WatermarkOptions options)
Adds a watermark to the loaded document using watermark options.
|
void |
close()
Disposes the current instance.
|
void |
generatePreview(PreviewOptions previewOptions)
Generates preview images for the document.
|
<T extends Content> |
getContent(Class<T> contentType)
Returns the
object for the loaded document. |
IDocumentInfo |
getDocumentInfo()
Gets the information about the format of the loaded document.
|
WatermarkableImageCollection |
getImages()
Finds all images in the document.
|
WatermarkableImageCollection |
getImages(ImageSearchCriteria searchCriteria)
Finds images according to specified search criteria.
|
SearchableObjects |
getSearchableObjects()
Gets the content objects that are to be included in a watermark search.
|
void |
remove(PossibleWatermark possibleWatermark)
Removes watermark from the document.
|
void |
remove(PossibleWatermarkCollection possibleWatermarks)
Removes all watermarks in the collection from the document.
|
void |
save(OutputStream document)
Saves the document to the specified stream.
|
void |
save(OutputStream document,
SaveOptions options)
Saves the document to the specified stream using save options.
|
void |
save(String filePath)
Saves the document to the specified file location.
|
void |
save(String filePath,
SaveOptions options)
Saves the document to the specified file location using save options.
|
PossibleWatermarkCollection |
search()
Searches all possible watermarks in the document.
|
PossibleWatermarkCollection |
search(SearchCriteria searchCriteria)
Searches possible watermarks according to specified search criteria.
|
void |
setSearchableObjects(SearchableObjects value)
Sets the content objects that are to be included in a watermark search.
|
public Watermarker(String filePath)
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();
filePath
- The file path to load the document from.UnsupportedFileTypeException
- When supplied document type is not supported.InvalidPasswordException
- When supplied password is incorrect.public Watermarker(String filePath, LoadOptions options)
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();
filePath
- The file path to load document from.options
- Additional options to use when loading a document.UnsupportedFileTypeException
- When supplied document type is not supported.InvalidPasswordException
- When supplied password is incorrect.public Watermarker(String filePath, WatermarkerSettings settings)
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(); } }
filePath
- The file path to load document from.settings
- Additional settings to use when working with loaded document.UnsupportedFileTypeException
- When supplied document type is not supported.InvalidPasswordException
- When supplied password is incorrect.public Watermarker(String filePath, LoadOptions options, WatermarkerSettings settings)
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();
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.UnsupportedFileTypeException
- When supplied document type is not supported.InvalidPasswordException
- When supplied password is incorrect.public Watermarker(InputStream document)
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();
document
- The stream to load document from.UnsupportedFileTypeException
- When supplied document type is not supported.InvalidPasswordException
- When supplied password is incorrect.public Watermarker(InputStream document, LoadOptions options)
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();
document
- The stream to load document from.options
- Additional options to use when loading a document.UnsupportedFileTypeException
- When supplied document type is not supported.InvalidPasswordException
- When supplied password is incorrect.public Watermarker(InputStream document, WatermarkerSettings settings)
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(); } }
document
- The stream to load document from.settings
- Additional settings to use when working with loaded document.UnsupportedFileTypeException
- When supplied document type is not supported.InvalidPasswordException
- When supplied password is incorrect.public Watermarker(InputStream document, LoadOptions options, WatermarkerSettings 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();
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.UnsupportedFileTypeException
- When supplied document type is not supported.InvalidPasswordException
- When supplied password is incorrect.public final SearchableObjects getSearchableObjects()
This mathod also specifies content objects which are used in image search.
For more information see
and Watermarker.search(SearchCriteria)
methods.
Watermarker.getImages()
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();
public final void setSearchableObjects(SearchableObjects value)
This mathod also specifies content objects which are used in image search.
For more information see
and Watermarker.search(SearchCriteria)
methods.
Learn more about searching watermarks:
Searching watermarks.
Watermarker.getImages()
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();
value
- The objects that are to be included in a watermark search.public final void close()
Disposes the current instance.
close
in interface Closeable
close
in interface AutoCloseable
public final IDocumentInfo getDocumentInfo()
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();
IDocumentInfo
instance that contains detected information.public final void add(Watermark watermark)
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();
watermark
- The watermark to add to the document.public final void add(Watermark watermark, WatermarkOptions 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();
watermark
- The watermark to add to the document.options
- Additional options to use when adding the watermark.public final void remove(PossibleWatermark possibleWatermark)
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();
possibleWatermark
- The watermark to remove.public final void remove(PossibleWatermarkCollection possibleWatermarks)
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();
possibleWatermarks
- The collection of watermarks to remove.public final void save(String filePath)
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();
filePath
- The file path to save the document data to.public final void save(OutputStream document)
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();
document
- The stream to save the document data to.public final void save(String filePath, SaveOptions 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();
filePath
- The file path to save the document data to.options
- Additional options to use when saving a document.public final void save(OutputStream document, SaveOptions 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();
document
- The stream to save the document data to.options
- Additional options to use when saving a document.public final void generatePreview(PreviewOptions previewOptions)
previewOptions
- Additional options to use when generating preview images.public final PossibleWatermarkCollection search()
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();
public final PossibleWatermarkCollection search(SearchCriteria searchCriteria)
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();
searchCriteria
- The search criteria to use.public final WatermarkableImageCollection getImages(ImageSearchCriteria searchCriteria)
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();
searchCriteria
- The search criteria to use.public final WatermarkableImageCollection getImages()
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();
public final <T extends Content> T getContent(Class<T> contentType)
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();