Existing objects in word processing document
Leave feedback
Watermarks in Word documents are usually represented by shapes. Using GroupDocs.Watermark API you can easily remove shape of any type from any level of document structure.
Removing watermark from a particular section
Removing watermark from a particular section of a Word document using GroupDocs.Watermark consists of following steps.
Load the document
Create and initialize image or text search criteria
WordProcessingLoadOptionsloadOptions=newWordProcessingLoadOptions();// Specify an absolute or relative path to your document. Ex: @"C:\Docs\document.docx"using(Watermarkerwatermarker=newWatermarker("document.docx",loadOptions)){// Initialize search criteriaImageSearchCriteriaimageSearchCriteria=newImageDctHashSearchCriteria("logo.png");TextSearchCriteriatextSearchCriteria=newTextSearchCriteria("Company Name");// Call Search method for the sectionWordProcessingContentcontent=watermarker.GetContent<WordProcessingContent>();PossibleWatermarkCollectionpossibleWatermarks=content.Sections[0].Search(textSearchCriteria.Or(imageSearchCriteria));// Remove all found watermarksfor(inti=possibleWatermarks.Count-1;i>=0;i--){possibleWatermarks.RemoveAt(i);}watermarker.Save("document.docx");}
Search for particular header or footer
You can also call Search method for a particular header or footer as shown in the below code sample.
WordProcessingLoadOptionsloadOptions=newWordProcessingLoadOptions();// Specify an absolute or relative path to your document. Ex: @"C:\Docs\document.docx"using(Watermarkerwatermarker=newWatermarker("document.docx",loadOptions)){// Initialize search criteriaImageSearchCriteriaimageSearchCriteria=newImageDctHashSearchCriteria("logo.png");TextSearchCriteriatextSearchCriteria=newTextSearchCriteria("Company Name");WordProcessingContentcontent=watermarker.GetContent<WordProcessingContent>();PossibleWatermarkCollectionpossibleWatermarks=content.Sections[0].HeadersFooters[OfficeHeaderFooterType.HeaderPrimary].Search(textSearchCriteria.Or(imageSearchCriteria));// Remove all found watermarksfor(inti=possibleWatermarks.Count-1;i>=0;i--){possibleWatermarks.RemoveAt(i);}watermarker.Save("document.docx");}
Extracting information about all shapes in a word document
Search method returns a collection of*PossibleWatermark* instances for all document types. But in some cases, it’s necessary to get more information about Word shapes than common API offers. GroupDocs.Watermark enables you to extract the information about all the shapes as shown in the below code sample.
WordProcessingLoadOptionsloadOptions=newWordProcessingLoadOptions();// Specify an absolute or relative path to your document. Ex: @"C:\Docs\document.docx"using(Watermarkerwatermarker=newWatermarker("document.docx",loadOptions)){WordProcessingContentcontent=watermarker.GetContent<WordProcessingContent>();foreach(WordProcessingSectionsectionincontent.Sections){foreach(WordProcessingShapeshapeinsection.Shapes){if(shape.HeaderFooter!=null){Console.WriteLine("In header/footer");}Console.WriteLine(shape.ShapeType);Console.WriteLine(shape.Width);Console.WriteLine(shape.Height);Console.WriteLine(shape.IsWordArt);Console.WriteLine(shape.RotateAngle);Console.WriteLine(shape.AlternativeText);Console.WriteLine(shape.Name);Console.WriteLine(shape.X);Console.WriteLine(shape.Y);Console.WriteLine(shape.Text);if(shape.Image!=null){Console.WriteLine(shape.Image.Width);Console.WriteLine(shape.Image.Height);Console.WriteLine(shape.Image.GetBytes().Length);}Console.WriteLine(shape.HorizontalAlignment);Console.WriteLine(shape.VerticalAlignment);Console.WriteLine(shape.RelativeHorizontalPosition);Console.WriteLine(shape.RelativeVerticalPosition);}}}
WordProcessingLoadOptionsloadOptions=newWordProcessingLoadOptions();// Specify an absolute or relative path to your document. Ex: @"C:\Docs\document.docx"using(Watermarkerwatermarker=newWatermarker("document.docx",loadOptions)){WordProcessingContentcontent=watermarker.GetContent<WordProcessingContent>();foreach(WordProcessingSectionsectionincontent.Sections){foreach(WordProcessingShapeshapeinsection.Shapes){//Check for Diagonal Corners Rounded shapesif(shape.ShapeType==WordProcessingShapeType.DiagonalCornersRounded){Console.WriteLine("Diagonal Corners Rounded shape found");//Write text on all Diagonal Corners Rounded shapesshape.FormattedTextFragments.Add("I am Diagonal Corner Rounded",newFont("Calibri",8,FontStyle.Bold),Color.Red,Color.Aqua);}}}watermarker.Save("document.docx");}
Removing a particular shape
You can also remove a particular shape from a Word document as shown in the below code sample.
WordProcessingLoadOptionsloadOptions=newWordProcessingLoadOptions();// Specify an absolute or relative path to your document. Ex: @"C:\Docs\document.docx"using(Watermarkerwatermarker=newWatermarker("document.docx",loadOptions)){WordProcessingContentcontent=watermarker.GetContent<WordProcessingContent>();// Remove shape by indexcontent.Sections[0].Shapes.RemoveAt(0);// Remove shape by referencecontent.Sections[0].Shapes.Remove(content.Sections[0].Shapes[0]);watermarker.Save("document.docx");}
Removing shapes with particular text formatting
You can also find and remove the shapes with a particular text formatting from a Word document as shown in the below code sample.
WordProcessingLoadOptionsloadOptions=newWordProcessingLoadOptions();// Specify an absolute or relative path to your document. Ex: @"C:\Docs\document.docx"using(Watermarkerwatermarker=newWatermarker("document.docx",loadOptions)){WordProcessingContentcontent=watermarker.GetContent<WordProcessingContent>();foreach(WordProcessingSectionsectionincontent.Sections){for(inti=section.Shapes.Count-1;i>=0;i--){foreach(FormattedTextFragmentfragmentinsection.Shapes[i].FormattedTextFragments){if(fragment.ForegroundColor==Color.Red&&fragment.Font.FamilyName=="Arial"){section.Shapes.RemoveAt(i);break;}}}}watermarker.Save("document.docx");}
Removing or replacing hyperlink associated with a particular shape
Using GroupDocs.Watermark for .NET, you can also remove or replace hyperlink associated with a particular shape inside a Word document. Use following code sample to achieve this functionality.
WordProcessingLoadOptionsloadOptions=newWordProcessingLoadOptions();// Specify an absolute or relative path to your document. Ex: @"C:\Docs\document.docx"using(Watermarkerwatermarker=newWatermarker("document.docx",loadOptions)){WordProcessingContentcontent=watermarker.GetContent<WordProcessingContent>();// Replace hyperlinkcontent.Sections[0].Shapes[0].Hyperlink="https://www.groupdocs.com/";// Remove hyperlinkcontent.Sections[0].Shapes[1].Hyperlink=null;watermarker.Save("document.docx");}
Replacing text for particular shapes
Replacing shape’s text
GroupDocs.Watermark supports replacing text for particular shapes in a Word document. Following code sample shows the usage of this feature.
WordProcessingLoadOptionsloadOptions=newWordProcessingLoadOptions();// Specify an absolute or relative path to your document. Ex: @"C:\Docs\document.docx"using(Watermarkerwatermarker=newWatermarker("document.docx",loadOptions)){WordProcessingContentcontent=watermarker.GetContent<WordProcessingContent>();// Set shape's textforeach(WordProcessingShapeshapeincontent.Sections[0].Shapes){if(shape.Text.Contains("Some text")){shape.Text="Another text";}}// Save documentwatermarker.Save("document.docx");}
Replacing shape’s text with formatted text
You can also replace the text of the shapes with formatted text as shown in the following code sample.
WordProcessingLoadOptionsloadOptions=newWordProcessingLoadOptions();// Specify an absolute or relative path to your document. Ex: @"C:\Docs\document.docx"using(Watermarkerwatermarker=newWatermarker("document.docx",loadOptions)){WordProcessingContentcontent=watermarker.GetContent<WordProcessingContent>();// Set shape's textforeach(WordProcessingShapeshapeincontent.Sections[0].Shapes){if(shape.Text.Contains("Some text")){shape.FormattedTextFragments.Clear();shape.FormattedTextFragments.Add("Another text",newFont("Calibri",19,FontStyle.Bold),Color.Red,Color.Aqua);}}// Save documentwatermarker.Save("document.docx");}
Replacing shape’s image
GroupDocs.Watermark also allows you to replace the image of the particular shapes in a Word document as shown in the following code sample.
WordProcessingLoadOptionsloadOptions=newWordProcessingLoadOptions();// Specify an absolute or relative path to your document. Ex: @"C:\Docs\document.docx"using(Watermarkerwatermarker=newWatermarker("document.docx",loadOptions)){WordProcessingContentcontent=watermarker.GetContent<WordProcessingContent>();// Set shape imageforeach(WordProcessingShapeshapeincontent.Sections[0].Shapes){if(shape.Image!=null){shape.Image=newWordProcessingWatermarkableImage(File.ReadAllBytes("test.png"));}}// Save documentwatermarker.Save("document.docx");}
Modifying shape properties
GroupDocs.Watermark also provides the feature of modifying properties (AlternativeText, RotateAngle, X, Y, Height, Width or BehindText) of particular shapes in a Word document. Following code sample shows how to use this feature.
WordProcessingLoadOptionsloadOptions=newWordProcessingLoadOptions();// Specify an absolute or relative path to your document. Ex: @"C:\Docs\document.docx"using(Watermarkerwatermarker=newWatermarker("document.docx",loadOptions)){WordProcessingContentcontent=watermarker.GetContent<WordProcessingContent>();// Change shape propertiesforeach(WordProcessingShapeshapeincontent.Sections[0].Shapes){if(shape.Text.Contains("Some text")){shape.AlternativeText="watermark";shape.RotateAngle=30;shape.X=200;shape.Y=200;shape.Height=100;shape.Width=400;shape.BehindText=false;}}// Save documentwatermarker.Save("document.docx");}
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.