SpreadsheetLoadOptionsloadOptions=newSpreadsheetLoadOptions();// Specify an absolute or relative path to your document. Ex: @"C:\Docs\spreadsheet.xlsx"using(Watermarkerwatermarker=newWatermarker("spreadsheet.xlsx",loadOptions)){SpreadsheetContentcontent=watermarker.GetContent<SpreadsheetContent>();foreach(SpreadsheetWorksheetworksheetincontent.Worksheets){foreach(SpreadsheetAttachmentattachmentinworksheet.Attachments){Console.WriteLine("Alternative text: {0}",attachment.AlternativeText);Console.WriteLine("Attachment frame x-coordinate: {0}",attachment.X);Console.WriteLine("Attachment frame y-coordinate: {0}",attachment.Y);Console.WriteLine("Attachment frame width: {0}",attachment.Width);Console.WriteLine("Attachment frame height: {0}",attachment.Height);Console.WriteLine("Preview image size: {0}",attachment.PreviewImageContent!=null?attachment.PreviewImageContent.Length:0);if(attachment.IsLink){// The document contains only a link to the attached fileConsole.WriteLine("Full path to the attached file: {0}",attachment.SourceFullName);}else{// The attached file is stored in the documentConsole.WriteLine("File type: {0}",attachment.GetDocumentInfo().FileType);Console.WriteLine("Name of the source file: {0}",attachment.SourceFullName);Console.WriteLine("File size: {0}",attachment.Content.Length);}}}
Add an attachment to excel document
GroupDocs.Watermark API allows you to add 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"using(Watermarkerwatermarker=newWatermarker("spreadsheet.xlsx",loadOptions)){SpreadsheetContentcontent=watermarker.GetContent<SpreadsheetContent>();SpreadsheetWorksheetworksheet=content.Worksheets[0];// Add the attachmentworksheet.Attachments.AddAttachment(File.ReadAllBytes("document.docx"),// File content"sample document.docx",// Source file full name (the extension is used// to determine appropriate application to open// the file) File.ReadAllBytes("document_preview.png"),// Preview image content50,// X-coordinate of the attachment frame100,// Y-coordinate of the attachment frame200,// Attachment frame width400);// Attachment frame height// Save changeswatermarker.Save("spreadsheet.xlsx");}
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"using(Watermarkerwatermarker=newWatermarker("spreadsheet.xlsx",loadOptions)){SpreadsheetContentcontent=watermarker.GetContent<SpreadsheetContent>();SpreadsheetWorksheetworksheet=content.Worksheets[0];// Add the attachmentworksheet.Attachments.AddLink("document.docx",// Source file pathFile.ReadAllBytes("document_preview.png"),// Preview image content50,// X-coordinate of the attachment frame100,// Y-coordinate of the attachment frame200,// Attachment frame width400);// Attachment frame height// Save changeswatermarker.Save("spreadsheet.xlsx");}
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"using(Watermarkerwatermarker=newWatermarker("spreadsheet.xlsx",loadOptions)){SpreadsheetContentcontent=watermarker.GetContent<SpreadsheetContent>();foreach(SpreadsheetWorksheetworksheetincontent.Worksheets){for(inti=worksheet.Attachments.Count-1;i>=0;i--){SpreadsheetAttachmentattachment=worksheet.Attachments[i];if(attachment.IsLink&&!File.Exists(attachment.SourceFullName)||// Linked file that is not available at this momentattachment.GetDocumentInfo().IsEncrypted)// Attached file protected with a password{// Remove the file if it meets at least one of the conditions aboveworksheet.Attachments.RemoveAt(i);}}}// Save changeswatermarker.Save("spreadsheet.xlsx");}
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"using(Watermarkerwatermarker=newWatermarker("spreadsheet.xlsx",loadOptions)){SpreadsheetContentcontent=watermarker.GetContent<SpreadsheetContent>();foreach(SpreadsheetWorksheetworksheetincontent.Worksheets){foreach(SpreadsheetAttachmentattachmentinworksheet.Attachments){// Check if the attached file is supported by GroupDocs.WatermarkIDocumentInfoinfo=attachment.GetDocumentInfo();if(info.FileType!=FileType.Unknown&&!info.IsEncrypted){// Load the attached documentusing(WatermarkerattachedWatermarker=attachment.CreateWatermarker()){// Add wateramrkattachedWatermarker.Add(watermark);// Save changes in the attached fileattachedWatermarker.Save();}}}}// Save changeswatermarker.Save("spreadsheet.xlsx");}
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 imagesWatermarkerSettingssettings=newWatermarkerSettings();settings.SearchableObjects.SpreadsheetSearchableObjects=SpreadsheetSearchableObjects.AttachedImages;SpreadsheetLoadOptionsloadOptions=newSpreadsheetLoadOptions();// Specify an absolute or relative path to your document. Ex: @"C:\Docs\spreadsheet.xlsx"using(Watermarkerwatermarker=newWatermarker("spreadsheet.xlsx",loadOptions,settings)){// Specify sample image to compare document images withImageSearchCriteriacriteria=newImageDctHashSearchCriteria("attachment.png");// Search for similar imagesPossibleWatermarkCollectionpossibleWatermarks=watermarker.Search(criteria);// Remove or modify found image watermarks// ...Console.WriteLine("Found {0} possible watermark(s).",possibleWatermarks.Count);}
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.