Add Watermarks to Diagrams
Adding watermark to all pages of a particular type
Using GroupDocs.Watermark for Java which is a part of Conholdate.Total for Java, allows you to add watermark to all pages of a particular type in a document. It consists of following steps.
- Load the document
- Create and initialize watermark object
- Set watermark properties
- Add watermark by specifying page type using setPlacementType() method of DiagramShapeWatermarkOptions
- Save the document
Following code shows how to add watermark to a particular type of the pages:
DiagramLoadOptions loadOptions = new DiagramLoadOptions();
// Constants.InDiagramVsdx is an absolute or relative path to your document. Ex: "C:\\Docs\\diagram.vsdx"
Watermarker watermarker = new Watermarker(Constants.InDiagramVsdx, loadOptions);
// Initialize text watermark
TextWatermark textWatermark = new TextWatermark("Test watermark 1", new Font("Calibri", 19));
DiagramShapeWatermarkOptions textWatermarkOptions = new DiagramShapeWatermarkOptions();
textWatermarkOptions.setPlacementType(DiagramWatermarkPlacementType.BackgroundPages);
// Add text watermark to all background pages
watermarker.add(textWatermark, textWatermarkOptions);
// Initialize image watermark
ImageWatermark imageWatermark = new ImageWatermark(Constants.LogoJpg);
DiagramShapeWatermarkOptions imageWatermarkOptions = new DiagramShapeWatermarkOptions();
imageWatermarkOptions.setPlacementType(DiagramWatermarkPlacementType.ForegroundPages);
// Add image watermark to all foreground pages
watermarker.add(imageWatermark, imageWatermarkOptions);
watermarker.save(Constants.OutDiagramVsdx);
imageWatermark.close();
watermarker.close();
Adding watermark on separate background page
In some cases, you may want to place the watermark on separate newly created background pages. In this case, use below code:
DiagramLoadOptions loadOptions = new DiagramLoadOptions();
// Constants.InDiagramVsdx is an absolute or relative path to your document. Ex: "C:\\Docs\\diagram.vsdx"
Watermarker watermarker = new Watermarker(Constants.InDiagramVsdx, loadOptions);
// Initialize watermark of any supported type
TextWatermark textWatermark = new TextWatermark("Test watermark 1", new Font("Calibri", 19));
DiagramShapeWatermarkOptions options = new DiagramShapeWatermarkOptions();
options.setPlacementType(DiagramWatermarkPlacementType.SeparateBackgrounds);
// Create separate background for each page where it is not set. Add watermark to it.
watermarker.add(textWatermark, options);
watermarker.save(Constants.OutDiagramVsdx);
watermarker.close();
Add watermark to a particular page
GroupDocs.Watermark allows you to add watermark to a particular page of the document using setPageIndex() of DiagramPageWatermarkOptions as shown in below example:
DiagramLoadOptions loadOptions = new DiagramLoadOptions();
// Constants.InDiagramVsdx is an absolute or relative path to your document. Ex: "C:\\Docs\\diagram.vsdx"
Watermarker watermarker = new Watermarker(Constants.InDiagramVsdx, loadOptions);
TextWatermark textWatermark = new TextWatermark("Test watermark", new Font("Calibri", 19));
DiagramPageWatermarkOptions textWatermarkOptions = new DiagramPageWatermarkOptions();
textWatermarkOptions.setPageIndex(0);
// Add text watermark to the first page
watermarker.add(textWatermark, textWatermarkOptions);
ImageWatermark imageWatermark = new ImageWatermark(Constants.LogoJpg);
DiagramPageWatermarkOptions imageWatermarkOptions = new DiagramPageWatermarkOptions();
imageWatermarkOptions.setPageIndex(1);
// Add image watermark to the second page
watermarker.add(imageWatermark, imageWatermarkOptions);
watermarker.save(Constants.OutDiagramVsdx);
watermarker.close();
imageWatermark.close();
Lock watermark
When you’re calling add() method of Watermaker object created for the Diagram document, simple shape is added to the document. There is no difference between added watermark and Visio shapes that are used to create diagrams.
GroupDocs.Watermark allows you to protect watermark from editing in MS Visio by calling setLocked() method of DiagramShapeWatermarkOptions (as shown in the following example):
DiagramLoadOptions loadOptions = new DiagramLoadOptions();
// Constants.InDiagramVsdx is an absolute or relative path to your document. Ex: "C:\\Docs\\diagram.vsdx"
Watermarker watermarker = new Watermarker(Constants.InDiagramVsdx, loadOptions);
TextWatermark watermark = new TextWatermark("Test watermark", new Font("Arial", 19));
DiagramShapeWatermarkOptions options = new DiagramShapeWatermarkOptions();
options.setLocked(true);
// Editing of the shape in Visio is forbidden
watermarker.add(watermark, options);
watermarker.save(Constants.OutDiagramVsdx);
watermarker.close();