PdfLoadOptionsloadOptions=newPdfLoadOptions();// Specify an absolute or relative path to your document. Ex: @"C:\Docs\document.pdf"using(Watermarkerwatermarker=newWatermarker("document.pdf",loadOptions)){PdfContentpdfContent=watermarker.GetContent<PdfContent>();foreach(PdfAttachmentattachmentinpdfContent.Attachments){Console.WriteLine("Name: {0}",attachment.Name);Console.WriteLine("Description: {0}",attachment.Description);Console.WriteLine("File type: {0}",attachment.GetDocumentInfo().FileType);// Save the attached file on diskFile.WriteAllBytes(Path.Combine("SampleFiles\Output",attachment.Name),attachment.Content);}}
Add an attachment to PDF document
The API also allows you to add attachments to the PDF document. Following code snippet shows how to remove an attachment
PdfLoadOptionsloadOptions=newPdfLoadOptions();// Specify an absolute or relative path to your document. Ex: @"C:\Docs\document.pdf"using(Watermarkerwatermarker=newWatermarker("document.pdf",loadOptions)){PdfContentpdfContent=watermarker.GetContent<PdfContent>();// Add the attachmentpdfContent.Attachments.Add(File.ReadAllBytes("sample.docx"),"sample doc","sample doc as attachment");// Save changeswatermarker.Save("document.pdf");}
Remove attachment from PDF document
The API also allows you to remove attachments from the PDF document. Following code snippet shows how to remove an attachment.
PdfLoadOptionsloadOptions=newPdfLoadOptions();// Specify an absolute or relative path to your document. Ex: @"C:\Docs\document.pdf"using(Watermarkerwatermarker=newWatermarker("document.pdf",loadOptions)){PdfContentpdfContent=watermarker.GetContent<PdfContent>();for(inti=pdfContent.Attachments.Count-1;i>=0;i--){PdfAttachmentattachment=pdfContent.Attachments[i];// Remove all attached pdf files with a particular nameif(attachment.Name.Contains("sample")&&attachment.GetDocumentInfo().FileType==FileType.DOCX){pdfContent.Attachments.RemoveAt(i);}}watermarker.Save("document.pdf");}
Search for images attachments
In case you want to search for all the images attachments in a PDF document, you can use GroupDocs.Watermark. Following code sample shows how to search images attachments of PDF document.
PdfLoadOptionsloadOptions=newPdfLoadOptions();// Specify an absolute or relative path to your document. Ex: @"C:\Docs\document.pdf"using(Watermarkerwatermarker=newWatermarker("document.pdf",loadOptions)){// Consider only the attached imageswatermarker.SearchableObjects.PdfSearchableObjects=PdfSearchableObjects.AttachedImages;// Search for similar imagesWatermarkableImageCollectionpossibleWatermarks=watermarker.GetImages();Console.WriteLine("Found {0} image(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.