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.
DiagramLoadOptionsloadOptions=newDiagramLoadOptions();// Specify an absolute or relative path to your document. Ex: @"C:\Docs\diagram.vsdx"using(Watermarkerwatermarker=newWatermarker("diagram.vsdx",loadOptions)){DiagramContentcontent=watermarker.GetContent<DiagramContent>();// Initialize search criteriaImageSearchCriteriaimageSearchCriteria=newImageDctHashSearchCriteria("logo.png");TextSearchCriteriatextSearchCriteria=newTextSearchCriteria("Company Name");// Call FindWatermarks method for the first pagePossibleWatermarkCollectionpossibleWatermarks=content.Pages[0].Search(textSearchCriteria.Or(imageSearchCriteria));// Remove all found watermarkspossibleWatermarks.Clear();watermarker.Save("diagram.vsdx");}
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"using(Watermarkerwatermarker=newWatermarker("diagram.vsdx",loadOptions)){DiagramContentcontent=watermarker.GetContent<DiagramContent>();foreach(DiagramPagepageincontent.Pages){foreach(DiagramShapeshapeinpage.Shapes){if(shape.Image!=null){Console.WriteLine(shape.Image.Width);Console.WriteLine(shape.Image.Height);Console.WriteLine(shape.Image.GetBytes().Length);}Console.WriteLine(shape.Name);Console.WriteLine(shape.X);Console.WriteLine(shape.Y);Console.WriteLine(shape.Width);Console.WriteLine(shape.Height);Console.WriteLine(shape.RotateAngle);Console.WriteLine(shape.Text);Console.WriteLine(shape.Id);}}}
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"using(Watermarkerwatermarker=newWatermarker("diagram.vsdx",loadOptions)){DiagramContentcontent=watermarker.GetContent<DiagramContent>();// Remove shape by indexcontent.Pages[0].Shapes.RemoveAt(0);// Remove shape by referencecontent.Pages[0].Shapes.Remove(content.Pages[0].Shapes[0]);watermarker.Save("diagram.vsdx");}
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"using(Watermarkerwatermarker=newWatermarker("diagram.vsdx",loadOptions)){DiagramContentcontent=watermarker.GetContent<DiagramContent>();foreach(DiagramPagepageincontent.Pages){for(inti=page.Shapes.Count-1;i>=0;i--){foreach(FormattedTextFragmentfragmentinpage.Shapes[i].FormattedTextFragments){if(fragment.ForegroundColor==Color.Red&&fragment.Font.FamilyName=="Arial"){page.Shapes.RemoveAt(i);break;}}}}watermarker.Save("diagram.vsdx");}
Removing hyperlink associated with a particular shape
Using GroupDocs.Watermark for .NET, 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"using(Watermarkerwatermarker=newWatermarker("diagram.vsdx",loadOptions)){DiagramContentcontent=watermarker.GetContent<DiagramContent>();DiagramShapeshape=content.Pages[0].Shapes[0];for(inti=shape.Hyperlinks.Count-1;i>=0;i--){if(shape.Hyperlinks[i].Address.Contains("http://someurl.com")){shape.Hyperlinks.RemoveAt(i);}}watermarker.Save("diagram.vsdx");}
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"using(Watermarkerwatermarker=newWatermarker("diagram.vsdx",loadOptions)){DiagramContentcontent=watermarker.GetContent<DiagramContent>();foreach(DiagramShapeshapeincontent.Pages[0].Shapes){if(shape.Image!=null){shape.Image=newDiagramWatermarkableImage(File.ReadAllBytes("test.png"));}}// Save changeswatermarker.Save("diagram.vsdx");}
Working with headers and footers
Extracting information about all headers and footers
The API allows you to extract information about all the headers&footers in a Visio document using following code.
DiagramLoadOptionsloadOptions=newDiagramLoadOptions();// Specify an absolute or relative path to your document. Ex: @"C:\Docs\diagram.vsdx"using(Watermarkerwatermarker=newWatermarker("diagram.vsdx",loadOptions)){DiagramContentcontent=watermarker.GetContent<DiagramContent>();// Get header&footer font settingsConsole.WriteLine(content.HeaderFooter.Font.FamilyName);Console.WriteLine(content.HeaderFooter.Font.Size);Console.WriteLine(content.HeaderFooter.Font.Bold);Console.WriteLine(content.HeaderFooter.Font.Italic);Console.WriteLine(content.HeaderFooter.Font.Underline);Console.WriteLine(content.HeaderFooter.Font.Strikeout);// Get text contained in headers&footersConsole.WriteLine(content.HeaderFooter.HeaderLeft);Console.WriteLine(content.HeaderFooter.HeaderCenter);Console.WriteLine(content.HeaderFooter.HeaderRight);Console.WriteLine(content.HeaderFooter.FooterLeft);Console.WriteLine(content.HeaderFooter.FooterCenter);Console.WriteLine(content.HeaderFooter.FooterRight);// Get text colorConsole.WriteLine(content.HeaderFooter.TextColor.ToArgb());// Get header&footer marginsConsole.WriteLine(content.HeaderFooter.FooterMargin);Console.WriteLine(content.HeaderFooter.HeaderMargin);}
Removing or replacing a particular header and footer
Following code sample shows how to remove and replace a particular header&footer in a Visio document.
DiagramLoadOptionsloadOptions=newDiagramLoadOptions();// Specify an absolute or relative path to your document. Ex: @"C:\Docs\diagram.vsdx"using(Watermarkerwatermarker=newWatermarker("diagram.vsdx",loadOptions)){DiagramContentcontent=watermarker.GetContent<DiagramContent>();// Remove headercontent.HeaderFooter.HeaderCenter=null;// Replace footercontent.HeaderFooter.FooterCenter="Footer center";content.HeaderFooter.Font.Size=19;content.HeaderFooter.Font.FamilyName="Calibri";content.HeaderFooter.TextColor=Color.Red;watermarker.Save("diagram.vsdx");}
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.