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
Find possible watermarks
Remove found watermarks
Save the document
Following code sample shows how to remove watermark from a particular section.
WordProcessingLoadOptionsloadOptions=newWordProcessingLoadOptions();// Specify an absolute or relative path to your document. Ex: "C:\\Docs\\document.docx"
Watermarkerwatermarker=newWatermarker("document.docx",loadOptions);// Initialize search criteria
ImageSearchCriteriaimageSearchCriteria=newImageDctHashSearchCriteria("logo.png");TextSearchCriteriatextSearchCriteria=newTextSearchCriteria("Company Name");// Call Search method for the section
WordProcessingContentcontent=watermarker.getContent(WordProcessingContent.class);PossibleWatermarkCollectionpossibleWatermarks=content.getSections().get_Item(0).search(textSearchCriteria.or(imageSearchCriteria));// Remove all found watermarks
for(inti=possibleWatermarks.getCount()-1;i>=0;i--){possibleWatermarks.removeAt(i);}watermarker.save("document.docx");watermarker.close();
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"
Watermarkerwatermarker=newWatermarker("document.docx",loadOptions);// Initialize search criteria
ImageSearchCriteriaimageSearchCriteria=newImageDctHashSearchCriteria("logo.png");TextSearchCriteriatextSearchCriteria=newTextSearchCriteria("Company Name");WordProcessingContentcontent=watermarker.getContent(WordProcessingContent.class);PossibleWatermarkCollectionpossibleWatermarks=content.getSections().get_Item(0).getHeadersFooters().getByOfficeHeaderFooterType(OfficeHeaderFooterType.HeaderPrimary).search(textSearchCriteria.or(imageSearchCriteria));// Remove all found watermarks
for(inti=possibleWatermarks.getCount()-1;i>=0;i--){possibleWatermarks.removeAt(i);}watermarker.save("document.docx");watermarker.close();
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"
Watermarkerwatermarker=newWatermarker("document.docx",loadOptions);WordProcessingContentcontent=watermarker.getContent(WordProcessingContent.class);for(WordProcessingSectionsection:content.getSections()){for(WordProcessingShapeshape:section.getShapes()){if(shape.getHeaderFooter()!=null){System.out.println("In header/footer");}System.out.println(shape.getShapeType());System.out.println(shape.getWidth());System.out.println(shape.getHeight());System.out.println(shape.isWordArt());System.out.println(shape.getRotateAngle());System.out.println(shape.getAlternativeText());System.out.println(shape.getName());System.out.println(shape.getX());System.out.println(shape.getY());System.out.println(shape.getText());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.getHorizontalAlignment());System.out.println(shape.getVerticalAlignment());System.out.println(shape.getRelativeHorizontalPosition());System.out.println(shape.getRelativeVerticalPosition());}}watermarker.close();
Working with shape types
You can extract information about the existing shapes of particular types. The WordProcessingShapeType enum is available to check the types possibly available shapes.
WordProcessingLoadOptionsloadOptions=newWordProcessingLoadOptions();// Specify an absolute or relative path to your document. Ex: "C:\\Docs\\document.docx"
Watermarkerwatermarker=newWatermarker("document.docx",loadOptions);WordProcessingContentcontent=watermarker.getContent(WordProcessingContent.class);for(WordProcessingSectionsection:content.getSections()){for(WordProcessingShapeshape:section.getShapes()){//Check for Diagonal Corners Rounded shapes
if(shape.getShapeType()==WordProcessingShapeType.DiagonalCornersRounded){System.out.println("Diagonal Corners Rounded shape found");//Write text on all Diagonal Corners Rounded shapes
shape.getFormattedTextFragments().add("I am Diagonal Corner Rounded",newFont("Calibri",8,FontStyle.Bold),Color.getRed(),Color.getAqua());}}}watermarker.save("document.docx");watermarker.close();
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"
Watermarkerwatermarker=newWatermarker("document.docx",loadOptions);WordProcessingContentcontent=watermarker.getContent(WordProcessingContent.class);// Remove shape by index
content.getSections().get_Item(0).getShapes().removeAt(0);// Remove shape by reference
content.getSections().get_Item(0).getShapes().remove(content.getSections().get_Item(0).getShapes().get_Item(0));watermarker.save("document.docx");watermarker.close();
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"
Watermarkerwatermarker=newWatermarker("document.docx",loadOptions);WordProcessingContentcontent=watermarker.getContent(WordProcessingContent.class);for(WordProcessingSectionsection:content.getSections()){for(inti=section.getShapes().getCount()-1;i>=0;i--){for(FormattedTextFragmentfragment:section.getShapes().get_Item(i).getFormattedTextFragments()){if(fragment.getForegroundColor().equals(Color.getRed())&&fragment.getFont().getFamilyName()=="Arial"){section.getShapes().removeAt(i);break;}}}}watermarker.save("document.docx");watermarker.close();
Removing or replacing hyperlink associated with a particular shape
Using GroupDocs.Watermark for Java, 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"
Watermarkerwatermarker=newWatermarker("document.docx",loadOptions);WordProcessingContentcontent=watermarker.getContent(WordProcessingContent.class);// Replace hyperlink
content.getSections().get_Item(0).getShapes().get_Item(0).setHyperlink("https://www.groupdocs.com/");// Remove hyperlink
content.getSections().get_Item(0).getShapes().get_Item(1).setHyperlink(null);watermarker.save("document.docx");watermarker.close();
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"
Watermarkerwatermarker=newWatermarker("document.docx",loadOptions);WordProcessingContentcontent=watermarker.getContent(WordProcessingContent.class);// Set shape's text
for(WordProcessingShapeshape:content.getSections().get_Item(0).getShapes()){if(shape.getText().contains("Some text")){shape.setText("Another text");}}// Save document
watermarker.save("document.docx");watermarker.close();
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"
Watermarkerwatermarker=newWatermarker("document.docx",loadOptions);WordProcessingContentcontent=watermarker.getContent(WordProcessingContent.class);// Set shape's text
for(WordProcessingShapeshape:content.getSections().get_Item(0).getShapes()){if(shape.getText().contains("Some text")){shape.getFormattedTextFragments().clear();shape.getFormattedTextFragments().add("Another text",newFont("Calibri",19,FontStyle.Bold),Color.getRed(),Color.getAqua());}}// Save document
watermarker.save("document.docx");watermarker.close();
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"
Watermarkerwatermarker=newWatermarker("document.docx",loadOptions);WordProcessingContentcontent=watermarker.getContent(WordProcessingContent.class);FileimageFile=newFile("test.png");byte[]imageBytes=newbyte[(int)imageFile.length()];InputStreamimageInputStream=newFileInputStream(imageFile);imageInputStream.read(imageBytes);imageInputStream.close();// Set shape image
for(WordProcessingShapeshape:content.getSections().get_Item(0).getShapes()){if(shape.getImage()!=null){shape.setImage(newWordProcessingWatermarkableImage(imageBytes));}}// Save document
watermarker.save("document.docx");watermarker.close();
WordProcessingLoadOptionsloadOptions=newWordProcessingLoadOptions();// Specify an absolute or relative path to your document. Ex: "C:\\Docs\\document.docx"
Watermarkerwatermarker=newWatermarker("document.docx",loadOptions);WordProcessingContentcontent=watermarker.getContent(WordProcessingContent.class);// Change shape properties
for(WordProcessingShapeshape:content.getSections().get_Item(0).getShapes()){if(shape.getText().contains("Some text")){shape.setAlternativeText("watermark");shape.setRotateAngle(30);shape.setX(200);shape.setY(200);shape.setHeight(100);shape.setWidth(400);shape.setBehindText(false);}}// Save document
watermarker.save("document.docx");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.