SpreadsheetLoadOptionsloadOptions=newSpreadsheetLoadOptions();// Specify an absolute or relative path to your document. Ex: "C:\\Docs\\spreadsheet.xlsx"
Watermarkerwatermarker=newWatermarker("spreadsheet.xlsx",loadOptions);SpreadsheetContentcontent=watermarker.getContent(SpreadsheetContent.class);for(SpreadsheetWorksheetworksheet:content.getWorksheets()){for(SpreadsheetAttachmentattachment:worksheet.getAttachments()){System.out.println("Alternative text: "+attachment.getAlternativeText());System.out.println("Attachment frame x-coordinate: "+attachment.getX());System.out.println("Attachment frame y-coordinate: "+attachment.getY());System.out.println("Attachment frame width: "+attachment.getWidth());System.out.println("Attachment frame height: "+attachment.getHeight());System.out.println("Preview image size: "+attachment.getPreviewImageContent()!=null?attachment.getPreviewImageContent().length:0);if(attachment.isLink()){// The document contains only a link to the attached file
System.out.println("Full path to the attached file: "+attachment.getSourceFullName());}else{// The attached file is stored in the document
System.out.println("File type: "+attachment.getDocumentInfo().getFileType());System.out.println("Name of the source file: "+attachment.getSourceFullName());System.out.println("File size: "+attachment.getContent().length);}}}watermarker.close();
Add an attachment to Excel document
GroupDocs.Watermark API allows you to add attachments in Excel document. Following code performs this functionality.
// Specify an absolute or relative path to your document. Ex: "C:\\Docs\\spreadsheet.xlsx"
SpreadsheetLoadOptionsloadOptions=newSpreadsheetLoadOptions();Watermarkerwatermarker=newWatermarker("spreadsheet.xlsx",loadOptions);Filefile=newFile("document.docx");byte[]attachmentBytes=newbyte[(int)file.length()];InputStreaminputStream=newFileInputStream(file);inputStream.read(attachmentBytes);inputStream.close();file=newFile("document_preview.png");byte[]previewImageBytes=newbyte[(int)file.length()];inputStream=newFileInputStream(file);inputStream.read(previewImageBytes);inputStream.close();SpreadsheetContentcontent=watermarker.getContent(SpreadsheetContent.class);SpreadsheetWorksheetworksheet=content.getWorksheets().get_Item(0);// Add the attachment
worksheet.getAttachments().addAttachment(attachmentBytes,// File content
"sample document.docx",// Source file full name (the extension is used
// to determine appropriate application to open
// the file)
previewImageBytes,// Preview image content
50,// X-coordinate of the attachment frame
100,// Y-coordinate of the attachment frame
200,// Attachment frame width
400);// Attachment frame height
// Save changes
watermarker.save("spreadsheet.xlsx");watermarker.close();
Add linked attachment to Excel document
GroupDocs.Watermark API allows you to add linked attachments in Excel document. Following code performs this functionality.
SpreadsheetLoadOptionsloadOptions=newSpreadsheetLoadOptions();// Specify an absolute or relative path to your document. Ex: "C:\\Docs\\spreadsheet.xlsx"
Watermarkerwatermarker=newWatermarker("spreadsheet.xlsx",loadOptions);SpreadsheetContentcontent=watermarker.getContent(SpreadsheetContent.class);SpreadsheetWorksheetworksheet=content.getWorksheets().get_Item(0);Filefile=newFile("document_preview.png");byte[]previewImageBytes=newbyte[(int)file.length()];FileInputStreaminputStream=newFileInputStream(file);inputStream.read(previewImageBytes);inputStream.close();// Add the attachment
worksheet.getAttachments().addLink("document.docx",// Source file path
previewImageBytes,// Preview image content
50,// X-coordinate of the attachment frame
100,// Y-coordinate of the attachment frame
200,// Attachment frame width
400);// Attachment frame height
// Save changes
watermarker.save("spreadsheet.xlsx");watermarker.close();
Remove attachment from Excel document
GroupDocs.Watermark API allows you to remove attachments in Excel document. Following code performs this functionality.
SpreadsheetLoadOptionsloadOptions=newSpreadsheetLoadOptions();// Specify an absolute or relative path to your document. Ex: "C:\\Docs\\spreadsheet.xlsx"
Watermarkerwatermarker=newWatermarker("spreadsheet.xlsx",loadOptions);SpreadsheetContentcontent=watermarker.getContent(SpreadsheetContent.class);for(SpreadsheetWorksheetworksheet:content.getWorksheets()){for(inti=worksheet.getAttachments().getCount()-1;i>=0;i--){SpreadsheetAttachmentattachment=worksheet.getAttachments().get_Item(i);if(attachment.isLink()&&!newFile(attachment.getSourceFullName()).exists()||// Linked file that is not available at this moment
attachment.getDocumentInfo().isEncrypted())// Attached file protected with a password
{// Remove the file if it meets at least one of the conditions above
worksheet.getAttachments().removeAt(i);}}}// Save changes
watermarker.save("spreadsheet.xlsx");watermarker.close();
Add watermark to all attachments
GroupDocs.Watermark API allows you to add watermark to all attachments in Excel document. Following code performs this functionality.
TextWatermarkwatermark=newTextWatermark("Test watermark",newFont("Arial",19));SpreadsheetLoadOptionsloadOptions=newSpreadsheetLoadOptions();// Specify an absolute or relative path to your document. Ex: "C:\\Docs\\spreadsheet.xlsx"
Watermarkerwatermarker=newWatermarker("spreadsheet.xlsx",loadOptions);SpreadsheetContentcontent=watermarker.getContent(SpreadsheetContent.class);for(SpreadsheetWorksheetworksheet:content.getWorksheets()){for(SpreadsheetAttachmentattachment:worksheet.getAttachments()){// Check if the attached file is supported by GroupDocs.Watermark
IDocumentInfoinfo=attachment.getDocumentInfo();if(info.getFileType()!=FileType.Unknown&&!info.isEncrypted()){// Load the attached document
WatermarkerattachedWatermarker=attachment.createWatermarker();// Add watermark
attachedWatermarker.add(watermark);// Save changes in the attached file
attachment.updateContent(attachedWatermarker);attachedWatermarker.close();}}}// Save changes
watermarker.save("spreadsheet.xlsx");watermarker.close();
Search for images in attached files
GroupDocs.Watermark API allows you to search for all the images and watermarkable attachments in Excel document. Following code performs this functionality.
// Consider only the attached images
WatermarkerSettingssettings=newWatermarkerSettings();settings.getSearchableObjects().setSpreadsheetSearchableObjects(SpreadsheetSearchableObjects.AttachedImages);SpreadsheetLoadOptionsloadOptions=newSpreadsheetLoadOptions();// Specify an absolute or relative path to your document. Ex: "C:\\Docs\\spreadsheet.xlsx"
Watermarkerwatermarker=newWatermarker("spreadsheet.xlsx",loadOptions,settings);// Specify sample image to compare document images with
ImageSearchCriteriacriteria=newImageDctHashSearchCriteria("attachment.png");// Search for similar images
PossibleWatermarkCollectionpossibleWatermarks=watermarker.search(criteria);// Remove or modify found image watermarks
// ...
System.out.println("Found "+possibleWatermarks.getCount()+" possible watermark(s).");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.