The watermarks in Visio documents are usually represented by shapes. But sometimes document headers&footers can also be used to display text that can be considered as the watermark. GroupDocs.Watermark API allows you to find and remove watermarks of both types in Visio document.
Removing watermark from a particular page
Removing watermark from a particular page of a Visio document using GroupDocs.Watermark consists of following steps.
Load the document
Create and initialize image/text search criteria
Find possible watermarks
Remove found watermarks
Save the document
Following code sample shows how to remove watermark from a particular page.
DiagramLoadOptionsloadOptions=newDiagramLoadOptions();// Specify an absolute or relative path to your document. Ex: "C:\\Docs\\diagram.vsdx"
Watermarkerwatermarker=newWatermarker("diagram.vsdx",loadOptions);DiagramContentcontent=watermarker.getContent(DiagramContent.class);// Initialize search criteria
ImageSearchCriteriaimageSearchCriteria=newImageDctHashSearchCriteria("logo.png");TextSearchCriteriatextSearchCriteria=newTextSearchCriteria("Company Name");// Call FindWatermarks method for the first page
PossibleWatermarkCollectionpossibleWatermarks=content.getPages().get_Item(0).search(textSearchCriteria.or(imageSearchCriteria));// Remove all found watermarks
possibleWatermarks.clear();watermarker.save("diagram.vsdx");watermarker.close();
Working with shapes
Extracting information about all shapes
Search() method searches watermarks of all mentioned types, but in some cases, it’s necessary to analyze only one type of Visio objects. Following code sample shows how to get information about all the shapes in a Visio document.
DiagramLoadOptionsloadOptions=newDiagramLoadOptions();// Specify an absolute or relative path to your document. Ex: "C:\\Docs\\diagram.vsdx"
Watermarkerwatermarker=newWatermarker("diagram.vsdx",loadOptions);DiagramContentcontent=watermarker.getContent(DiagramContent.class);for(DiagramPagepage:content.getPages()){for(DiagramShapeshape:page.getShapes()){if(shape.getImage()!=null){System.out.println(shape.getImage().getWidth());System.out.println(shape.getImage().getHeight());System.out.println(shape.getImage().getBytes().length);}System.out.println(shape.getName());System.out.println(shape.getX());System.out.println(shape.getY());System.out.println(shape.getWidth());System.out.println(shape.getHeight());System.out.println(shape.getRotateAngle());System.out.println(shape.getText());System.out.println(shape.getId());}}watermarker.close();
Removing a particular shape
You can also remove a particular shape from a page using GroupDocs.Watermark API (as shown in the sample code below).
DiagramLoadOptionsloadOptions=newDiagramLoadOptions();// Specify an absolute or relative path to your document. Ex: @"C:\Docs\diagram.vsdx"
Watermarkerwatermarker=newWatermarker("diagram.vsdx",loadOptions);DiagramContentcontent=watermarker.getContent(DiagramContent.class);// Remove shape by index
content.getPages().get_Item(0).getShapes().removeAt(0);// Remove shape by reference
content.getPages().get_Item(0).getShapes().remove(content.getPages().get_Item(0).getShapes().get_Item(0));watermarker.save("diagram.vsdx");watermarker.close();
Removing shapes with particular text formatting
You can also find and remove the shapes with a particular text formatting from a Visio document as shown in the below code sample.
DiagramLoadOptionsloadOptions=newDiagramLoadOptions();// Specify an absolute or relative path to your document. Ex: "C:\\Docs\\diagram.vsdx"
Watermarkerwatermarker=newWatermarker("diagram.vsdx",loadOptions);DiagramContentcontent=watermarker.getContent(DiagramContent.class);for(DiagramPagepage:content.getPages()){for(inti=page.getShapes().getCount()-1;i>=0;i--){for(FormattedTextFragmentfragment:page.getShapes().get_Item(i).getFormattedTextFragments()){if(fragment.getForegroundColor().equals(Color.getRed())&&fragment.getFont().getFamilyName()=="Arial"){page.getShapes().removeAt(i);break;}}}}watermarker.save("diagram.vsdx");watermarker.close();
Removing hyperlink associated with a particular shape
Using GroupDocs.Watermark for Java, you can also remove hyperlink associated with a particular shape inside a Visio document. Use following code sample to achieve this functionality.
DiagramLoadOptionsloadOptions=newDiagramLoadOptions();// Specify an absolute or relative path to your document. Ex: "C:\\Docs\\diagram.vsdx"
Watermarkerwatermarker=newWatermarker("diagram.vsdx",loadOptions);DiagramContentcontent=watermarker.getContent(DiagramContent.class);DiagramShapeshape=content.getPages().get_Item(0).getShapes().get_Item(0);for(inti=shape.getHyperlinks().getCount()-1;i>=0;i--){if(shape.getHyperlinks().get_Item(i).getAddress().contains("http://someurl.com")){shape.getHyperlinks().removeAt(i);}}watermarker.save("diagram.vsdx");watermarker.close();
Replacing text for particular shapes
Since version 18.1. GroupDocs.Watermark allows you to replace the text for particular shapes. Following code sample shows how to replace shapes’ text.
DiagramLoadOptionsloadOptions=newDiagramLoadOptions();// Specify an absolute or relative path to your document. Ex: "C:\\Docs\\diagram.vsdx"
Watermarkerwatermarker=newWatermarker("diagram.vsdx",loadOptions);DiagramContentcontent=watermarker.getContent(DiagramContent.class);for(DiagramShapeshape:content.getPages().get_Item(0).getShapes()){if(shape.getImage()!=null){FileimageFile=newFile("test.png");byte[]imageBytes=newbyte[(int)imageFile.length()];InputStreamimageInputStream=newFileInputStream(imageFile);imageInputStream.read(imageBytes);imageInputStream.close();shape.setImage(newDiagramWatermarkableImage(imageBytes));}}// Save changes
watermarker.save("diagram.vsdx");watermarker.close();
Working with headers and footers
Extracting information about all headers and footers
The API allows you to extract information about all the headers and footers in a Visio document using following code.
DiagramLoadOptionsloadOptions=newDiagramLoadOptions();// Specify an absolute or relative path to your document. Ex: "C:\\Docs\\diagram.vsdx"
Watermarkerwatermarker=newWatermarker("diagram.vsdx",loadOptions);DiagramContentcontent=watermarker.getContent(DiagramContent.class);// Get header&footer font settings
System.out.println(content.getHeaderFooter().getFont().getFamilyName());System.out.println(content.getHeaderFooter().getFont().getSize());System.out.println(content.getHeaderFooter().getFont().getBold());System.out.println(content.getHeaderFooter().getFont().getItalic());System.out.println(content.getHeaderFooter().getFont().getUnderline());System.out.println(content.getHeaderFooter().getFont().getStrikeout());// Get text contained in headers&footers
System.out.println(content.getHeaderFooter().getHeaderLeft());System.out.println(content.getHeaderFooter().getHeaderCenter());System.out.println(content.getHeaderFooter().getHeaderRight());System.out.println(content.getHeaderFooter().getFooterLeft());System.out.println(content.getHeaderFooter().getFooterCenter());System.out.println(content.getHeaderFooter().getFooterRight());// Get text color
System.out.println(content.getHeaderFooter().getTextColor().toArgb());// Get header&footer margins
System.out.println(content.getHeaderFooter().getFooterMargin());System.out.println(content.getHeaderFooter().getHeaderMargin());watermarker.close();
Removing or replacing a particular header and footer
Following code sample shows how to remove and replace a particular header and footer in a Visio document.
DiagramLoadOptionsloadOptions=newDiagramLoadOptions();// Specify an absolute or relative path to your document. Ex: "C:\\Docs\\diagram.vsdx"
Watermarkerwatermarker=newWatermarker("diagram.vsdx",loadOptions);DiagramContentcontent=watermarker.getContent(DiagramContent.class);// Remove header
content.getHeaderFooter().setHeaderCenter(null);// Replace footer
content.getHeaderFooter().setFooterCenter("Footer center");content.getHeaderFooter().getFont().setSize(19);content.getHeaderFooter().getFont().setFamilyName("Calibri");content.getHeaderFooter().setTextColor(Color.getRed());watermarker.save("diagram.vsdx");watermarker.close();
Was this page helpful?
Any additional feedback you'd like to share with us?
Please tell us how we can improve this page.
Thank you for your feedback!
We value your opinion. Your feedback will help us improve our documentation.