PdfLoadOptionsloadOptions=newPdfLoadOptions();// Specify an absolute or relative path to your document. Ex: "C:\\Docs\\document.pdf"
Watermarkerwatermarker=newWatermarker("document.pdf",loadOptions);// Initialize search criteria
ImageSearchCriteriaimageSearchCriteria=newImageDctHashSearchCriteria("logo.png");TextSearchCriteriatextSearchCriteria=newTextSearchCriteria("Company Name");PdfContentpdfContent=watermarker.getContent(PdfContent.class);PossibleWatermarkCollectionpossibleWatermarks=pdfContent.getPages().get_Item(0).search(imageSearchCriteria.or(textSearchCriteria));// Remove all found watermarks
for(inti=possibleWatermarks.getCount()-1;i>=0;i--){possibleWatermarks.removeAt(i);}watermarker.save("document.pdf");watermarker.close();
Search() method searches watermarks of all mentioned types, but in some cases, it’s necessary to analyze only one class of PDF entities. The following articles specifically deal with different types of the watermark objects in a PDF document.
Working with XObjects
Extracting information about all XObjects in PDF document
Using GroupDocs.Watermark for .NET, you can extract information about all the XObjects in a PDF document. Following code sample performs this functionality.
PdfLoadOptionsloadOptions=newPdfLoadOptions();// Specify an absolute or relative path to your document. Ex: "C:\\Docs\\document.pdf"
Watermarkerwatermarker=newWatermarker("document.pdf",loadOptions);PdfContentpdfContent=watermarker.getContent(PdfContent.class);for(PdfPagepage:pdfContent.getPages()){for(PdfXObjectxObject:page.getXObjects()){if(xObject.getImage()!=null){System.out.println(xObject.getImage().getWidth());System.out.println(xObject.getImage().getHeight());System.out.println(xObject.getImage().getBytes().length);}System.out.println(xObject.getText());System.out.println(xObject.getX());System.out.println(xObject.getY());System.out.println(xObject.getWidth());System.out.println(xObject.getHeight());System.out.println(xObject.getRotateAngle());}}watermarker.close();
Removing a particular XObject
You can also remove an XObject from a page using GroupDocs.Watermark. Following code sample removes an XObject from a particular page.
PdfLoadOptionsloadOptions=newPdfLoadOptions();// Specify an absolute or relative path to your document. Ex: "C:\\Docs\\document.pdf"
Watermarkerwatermarker=newWatermarker("document.pdf",loadOptions);PdfContentpdfContent=watermarker.getContent(PdfContent.class);// Remove XObject by index
pdfContent.getPages().get_Item(0).getXObjects().removeAt(0);// Remove XObject by reference
pdfContent.getPages().get_Item(0).getXObjects().remove(pdfContent.getPages().get_Item(0).getXObjects().get_Item(0));watermarker.save("document.pdf");watermarker.close();
Removing XObjects containing text with particular text formatting
You can also find and remove all XObjects containing text with a particular formatting from a PDF document as shown in the below code sample.
PdfLoadOptionsloadOptions=newPdfLoadOptions();// Specify an absolute or relative path to your document. Ex: "C:\\Docs\\document.pdf"
Watermarkerwatermarker=newWatermarker("document.pdf",loadOptions);PdfContentpdfContent=watermarker.getContent(PdfContent.class);for(PdfPagepage:pdfContent.getPages()){for(inti=page.getXObjects().getCount()-1;i>=0;i--){for(FormattedTextFragmentfragment:page.getXObjects().get_Item(i).getFormattedTextFragments()){if(fragment.getForegroundColor().equals(Color.getRed())){page.getXObjects().removeAt(i);break;}}}}watermarker.save("document.pdf");watermarker.close();
Adding watermark to all image XObjects
GroupDocs.Watermark API allows you to add watermark to all image XObjects in a PDF document. Following code sample serves this purpose.
PdfLoadOptionsloadOptions=newPdfLoadOptions();// Specify an absolute or relative path to your document. Ex: "C:\\Docs\\document.pdf"
Watermarkerwatermarker=newWatermarker("document.pdf",loadOptions);PdfContentpdfContent=watermarker.getContent(PdfContent.class);// Initialize image or text watermark
TextWatermarkwatermark=newTextWatermark("Protected image",newFont("Arial",8));watermark.setHorizontalAlignment(HorizontalAlignment.Center);watermark.setVerticalAlignment(VerticalAlignment.Center);watermark.setRotateAngle(45);watermark.setSizingType(SizingType.ScaleToParentDimensions);watermark.setScaleFactor(1);for(PdfPagepage:pdfContent.getPages()){for(PdfXObjectxObject:page.getXObjects()){if(xObject.getImage()!=null){// Add watermark to the image
xObject.getImage().add(watermark);}}}watermarker.save("document.pdf");watermarker.close();
Replacing text for particular XObjects
GroupDocs.Watermark allows you to edit and replace the text of the particular XObject. You can also replace XObject’s text with formatting as shown in the below code samples.
PdfLoadOptionsloadOptions=newPdfLoadOptions();// Specify an absolute or relative path to your document. Ex: "C:\\Docs\\document.pdf"
Watermarkerwatermarker=newWatermarker("document.pdf",loadOptions);PdfContentpdfContent=watermarker.getContent(PdfContent.class);for(PdfXObjectxObject:pdfContent.getPages().get_Item(0).getXObjects()){// Replace text
if(xObject.getText().contains("Test")){xObject.setText("Passed");}}// Save document
watermarker.save("document.pdf");watermarker.close();
PdfLoadOptionsloadOptions=newPdfLoadOptions();// Specify an absolute or relative path to your document. Ex: "C:\\Docs\\document.pdf"
Watermarkerwatermarker=newWatermarker("document.pdf",loadOptions);PdfContentpdfContent=watermarker.getContent(PdfContent.class);for(PdfXObjectxObject:pdfContent.getPages().get_Item(0).getXObjects()){// Replace text
if(xObject.getText().contains("Test")){xObject.getFormattedTextFragments().clear();xObject.getFormattedTextFragments().add("Passed",newFont("Calibri",19,FontStyle.Bold),Color.getRed(),Color.getAqua());}}// Save document
watermarker.save("document.pdf");watermarker.close();
Replacing image for particular XObjects
Using GroupDocs.Watermark, you can also replace the image of a particular XObject. GroupDocs.Watermark allows you to loop through all the XObjects of a particular page and you can replace the image of particular XObjects using some condition as shown in the below code sample.
PdfLoadOptionsloadOptions=newPdfLoadOptions();// Specify an absolute or relative path to your document. Ex: "C:\\Docs\\document.pdf"
Watermarkerwatermarker=newWatermarker("document.pdf",loadOptions);PdfContentpdfContent=watermarker.getContent(PdfContent.class);FileimageFile=newFile("test.png");byte[]imageBytes=newbyte[(int)imageFile.length()];InputStreamimageStream=newFileInputStream(imageFile);imageStream.read(imageBytes);imageStream.close();// Replace image
for(PdfXObjectxObject:pdfContent.getPages().get_Item(0).getXObjects()){if(xObject.getImage()!=null){xObject.setImage(newPdfWatermarkableImage(imageBytes));}}// Save document
watermarker.save("document.pdf");watermarker.close();
Working with artifacts
Extracting information about all artifacts in PDF document
GroupDocs.Watermark enables you to extract the information about the artifacts in a PDF document as shown in the below code sample.
PdfLoadOptionsloadOptions=newPdfLoadOptions();// Specify an absolute or relative path to your document. Ex: "C:\\Docs\\document.pdf"
Watermarkerwatermarker=newWatermarker("document.pdf",loadOptions);PdfContentpdfContent=watermarker.getContent(PdfContent.class);for(PdfPagepage:pdfContent.getPages()){for(PdfArtifactartifact:page.getArtifacts()){System.out.println(artifact.getArtifactType());System.out.println(artifact.getArtifactSubtype());if(artifact.getImage()!=null){System.out.println(artifact.getImage().getWidth());System.out.println(artifact.getImage().getHeight());System.out.println(artifact.getImage().getBytes().length);}System.out.println(artifact.getText());System.out.println(artifact.getOpacity());System.out.println(artifact.getX());System.out.println(artifact.getY());System.out.println(artifact.getWidth());System.out.println(artifact.getHeight());System.out.println(artifact.getRotateAngle());}}watermarker.close();
Removing a particular artifact
Following code sample shows how to remove an artifact from a particular page of the PDF document.
PdfLoadOptionsloadOptions=newPdfLoadOptions();// Specify an absolute or relative path to your document. Ex: "C:\\Docs\\document.pdf"
Watermarkerwatermarker=newWatermarker("document.pdf",loadOptions);PdfContentpdfContent=watermarker.getContent(PdfContent.class);// Remove Artifact by index
pdfContent.getPages().get_Item(0).getArtifacts().removeAt(0);// Remove Artifact by reference
pdfContent.getPages().get_Item(0).getArtifacts().remove(pdfContent.getPages().get_Item(0).getArtifacts().get_Item(0));watermarker.save("document.pdf");watermarker.close();
Removing artifacts containing text with particular text formatting
You can also find and remove all artifacts containing text with a particular formatting from a PDF document as shown in the below code sample.
PdfLoadOptionsloadOptions=newPdfLoadOptions();// Specify an absolute or relative path to your document. Ex: "C:\\Docs\\document.pdf"
Watermarkerwatermarker=newWatermarker("document.pdf",loadOptions);PdfContentpdfContent=watermarker.getContent(PdfContent.class);for(PdfPagepage:pdfContent.getPages()){for(inti=page.getArtifacts().getCount()-1;i>=0;i--){for(FormattedTextFragmentfragment:page.getArtifacts().get_Item(i).getFormattedTextFragments()){if(fragment.getFont().getSize()>42){page.getArtifacts().removeAt(i);break;}}}}watermarker.save("document.pdf");watermarker.close();
Adding watermark to all image artifacts
GroupDocs.Watermark API also provides the feature of adding watermark to all image artifacts in a PDF document. Following code sample adds watermark to all image artifacts.
PdfLoadOptionsloadOptions=newPdfLoadOptions();// Specify an absolute or relative path to your document. Ex: "C:\\Docs\\document.pdf"
Watermarkerwatermarker=newWatermarker("document.pdf",loadOptions);PdfContentpdfContent=watermarker.getContent(PdfContent.class);// Initialize image or text watermark
TextWatermarkwatermark=newTextWatermark("Protected image",newFont("Arial",8));watermark.setHorizontalAlignment(HorizontalAlignment.Center);watermark.setVerticalAlignment(VerticalAlignment.Center);watermark.setRotateAngle(45);watermark.setSizingType(SizingType.ScaleToParentDimensions);watermark.setScaleFactor(1);for(PdfPagepage:pdfContent.getPages()){for(PdfArtifactartifact:page.getArtifacts()){if(artifact.getImage()!=null){// Add watermark to the image
artifact.getImage().add(watermark);}}}watermarker.save("document.pdf");watermarker.close();
Replacing text for particular artifacts
GroupDocs.Watermark allows you to edit and replace the text of the particular artifacts. You can also replace artifact’s text with formatting as shown in the below code samples.
PdfLoadOptionsloadOptions=newPdfLoadOptions();// Specify an absolute or relative path to your document. Ex: "C:\\Docs\\document.pdf"
Watermarkerwatermarker=newWatermarker("document.pdf",loadOptions);PdfContentpdfContent=watermarker.getContent(PdfContent.class);for(PdfArtifactartifact:pdfContent.getPages().get_Item(0).getArtifacts()){// Replace text
if(artifact.getText().contains("Test")){artifact.setText("Passed");}}// Save document
watermarker.save("document.pdf");watermarker.close();
PdfLoadOptionsloadOptions=newPdfLoadOptions();// Specify an absolute or relative path to your document. Ex: "C:\\Docs\\document.pdf"
Watermarkerwatermarker=newWatermarker("document.pdf",loadOptions);PdfContentpdfContent=watermarker.getContent(PdfContent.class);for(PdfArtifactartifact:pdfContent.getPages().get_Item(0).getArtifacts()){// Replace text
if(artifact.getText().contains("Test")){artifact.getFormattedTextFragments().clear();artifact.getFormattedTextFragments().add("Passed",newFont("Calibri",19,FontStyle.Bold),Color.getRed(),Color.getAqua());}}// Save document
watermarker.save("document.pdf");watermarker.close();
Replacing image for particular artifacts
Using GroupDocs.Watermark, you can also replace the image of a particular artifact. GroupDocs.Watermark allows you to loop through all the artifacts of a particular page and you can replace the image of particular artifacts using some condition as shown in the below code sample.
PdfLoadOptionsloadOptions=newPdfLoadOptions();// Specify an absolute or relative path to your document. Ex: "C:\\Docs\\document.pdf"
Watermarkerwatermarker=newWatermarker("document.pdf",loadOptions);PdfContentpdfContent=watermarker.getContent(PdfContent.class);FileimageFile=newFile("test.png");byte[]imageBytes=newbyte[(int)imageFile.length()];InputStreamimageStream=newFileInputStream(imageFile);imageStream.read(imageBytes);imageStream.close();// Replace image
for(PdfArtifactartifact:pdfContent.getPages().get_Item(0).getArtifacts()){if(artifact.getImage()!=null){artifact.setImage(newPdfWatermarkableImage(imageBytes));}}// Save document
watermarker.save("document.pdf");watermarker.close();
Working with annotations
Extracting information about all annotations in PDF document
You can also extract information about all the annotations in a PDF document using GroupDocs.Watermark as shown in below code sample.
PdfLoadOptionsloadOptions=newPdfLoadOptions();// Specify an absolute or relative path to your document. Ex: "C:\\Docs\\document.pdf"
Watermarkerwatermarker=newWatermarker("document.pdf",loadOptions);PdfContentpdfContent=watermarker.getContent(PdfContent.class);for(PdfPagepage:pdfContent.getPages()){for(PdfAnnotationannotation:page.getAnnotations()){System.out.println(annotation.getAnnotationType());if(annotation.getImage()!=null){System.out.println(annotation.getImage().getWidth());System.out.println(annotation.getImage().getHeight());System.out.println(annotation.getImage().getBytes().length);}System.out.println(annotation.getText());System.out.println(annotation.getX());System.out.println(annotation.getY());System.out.println(annotation.getWidth());System.out.println(annotation.getHeight());System.out.println(annotation.getRotateAngle());}}watermarker.close();
Removing a particular annotation
Following code sample can be used to remove a particular annotation from a PDF document.
PdfLoadOptionsloadOptions=newPdfLoadOptions();// Specify an absolute or relative path to your document. Ex: "C:\\Docs\\document.pdf"
Watermarkerwatermarker=newWatermarker("document.pdf",loadOptions);PdfContentpdfContent=watermarker.getContent(PdfContent.class);// Remove Annotation by index
pdfContent.getPages().get_Item(0).getAnnotations().removeAt(0);// Remove Annotation by reference
pdfContent.getPages().get_Item(0).getAnnotations().remove(pdfContent.getPages().get_Item(0).getAnnotations().get_Item(0));watermarker.save("document.pdf");watermarker.close();
Removing annotations containing text with particular text formatting
You can also find and remove all annotations containing text with a particular formatting from a PDF document as shown in the below code sample.
PdfLoadOptionsloadOptions=newPdfLoadOptions();// Specify an absolute or relative path to your document. Ex: "C:\\Docs\\document.pdf"
Watermarkerwatermarker=newWatermarker("document.pdf",loadOptions);PdfContentpdfContent=watermarker.getContent(PdfContent.class);for(PdfPagepage:pdfContent.getPages()){for(inti=page.getAnnotations().getCount()-1;i>=0;i--){for(FormattedTextFragmentfragment:page.getAnnotations().get_Item(i).getFormattedTextFragments()){if(fragment.getFont().getFamilyName()=="Verdana"){page.getAnnotations().removeAt(i);break;}}}}watermarker.save("document.pdf");watermarker.close();
Adding watermark to all image annotations
Similar to the other types, the watermark can be added to image annotations in PDF documents as shown in the below code sample.
PdfLoadOptionsloadOptions=newPdfLoadOptions();// Specify an absolute or relative path to your document. Ex: "C:\\Docs\\document.pdf"
Watermarkerwatermarker=newWatermarker("document.pdf",loadOptions);PdfContentpdfContent=watermarker.getContent(PdfContent.class);// Initialize image or text watermark
TextWatermarkwatermark=newTextWatermark("Protected image",newFont("Arial",8));watermark.setHorizontalAlignment(HorizontalAlignment.Center);watermark.setVerticalAlignment(VerticalAlignment.Center);watermark.setRotateAngle(45);watermark.setSizingType(SizingType.ScaleToParentDimensions);watermark.setScaleFactor(1);for(PdfPagepage:pdfContent.getPages()){for(PdfAnnotationannotation:page.getAnnotations()){if(annotation.getImage()!=null){// Add watermark to the image
annotation.getImage().add(watermark);}}}watermarker.save("document.pdf");watermarker.close();
Replacing text for particular annotations
GroupDocs.Watermark allows you to edit and replace the text of the particular annotations. You can also replace annotation’s text with formatting as shown in the below code samples.
PdfLoadOptionsloadOptions=newPdfLoadOptions();// Specify an absolute or relative path to your document. Ex: "C:\\Docs\\document.pdf"
Watermarkerwatermarker=newWatermarker("document.pdf",loadOptions);PdfContentpdfContent=watermarker.getContent(PdfContent.class);for(PdfAnnotationannotation:pdfContent.getPages().get_Item(0).getAnnotations()){// Replace text
if(annotation.getText().contains("Test")){annotation.setText("Passed");}}// Save document
watermarker.save("document.pdf");watermarker.close();
PdfLoadOptionsloadOptions=newPdfLoadOptions();// Specify an absolute or relative path to your document. Ex: "C:\\Docs\\document.pdf"
Watermarkerwatermarker=newWatermarker("document.pdf",loadOptions);PdfContentpdfContent=watermarker.getContent(PdfContent.class);for(PdfAnnotationannotation:pdfContent.getPages().get_Item(0).getAnnotations()){// Replace text
if(annotation.getText().contains("Test")){annotation.getFormattedTextFragments().clear();annotation.getFormattedTextFragments().add("Passed",newFont("Calibri",19,FontStyle.Bold),Color.getRed(),Color.getAqua());}}// Save document
watermarker.save("document.pdf");watermarker.close();
Replacing image for particular annotations
Using GroupDocs.Watermark, you can also replace the image of a particular annotation. GroupDocs.Watermark allows you to loop through all the annotations of a particular page and you can replace the image of particular annotations using some condition as shown in the below code sample.
PdfLoadOptionsloadOptions=newPdfLoadOptions();// Specify an absolute or relative path to your document. Ex: "C:\\Docs\\document.pdf"
Watermarkerwatermarker=newWatermarker("document.pdf",loadOptions);PdfContentpdfContent=watermarker.getContent(PdfContent.class);FileimageFile=newFile("test.png");byte[]imageBytes=newbyte[(int)imageFile.length()];InputStreamimageStream=newFileInputStream(imageFile);imageStream.read(imageBytes);imageStream.close();// Replace image
for(PdfAnnotationannotation:pdfContent.getPages().get_Item(0).getAnnotations()){if(annotation.getImage()!=null){annotation.setImage(newPdfWatermarkableImage(imageBytes));}}// Save document
watermarker.save("document.pdf");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.