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(PdfAttachmentattachment:pdfContent.getAttachments()){System.out.println("Name: "+attachment.getName());System.out.println("Description: "+attachment.getDescription());System.out.println("File type: "+attachment.getDocumentInfo().getFileType());// Save the attached file on disk
FileOutputStreamoutputStream=newFileOutputStream("SampleFiles\\Output"+"\\"+attachment.getName());outputStream.write(attachment.getContent());outputStream.close();}watermarker.close();
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"
Watermarkerwatermarker=newWatermarker("document.pdf",loadOptions);PdfContentpdfContent=watermarker.getContent(PdfContent.class);Filefile=newFile("sample.docx");byte[]attachmentBytes=newbyte[(int)file.length()];InputStreaminputStream=newFileInputStream(file);inputStream.read(attachmentBytes);inputStream.close();// Add the attachment
pdfContent.getAttachments().add(attachmentBytes,"sample doc","sample doc as attachment");// Save changes
watermarker.save("document.pdf");watermarker.close();
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"
Watermarkerwatermarker=newWatermarker("document.pdf",loadOptions);PdfContentpdfContent=watermarker.getContent(PdfContent.class);for(inti=pdfContent.getAttachments().getCount()-1;i>=0;i--){PdfAttachmentattachment=pdfContent.getAttachments().get_Item(i);// Remove all attached pdf files with a particular name
if(attachment.getName().contains("sample")&&attachment.getDocumentInfo().getFileType()==FileType.DOCX){pdfContent.getAttachments().removeAt(i);}}watermarker.save("document.pdf");watermarker.close();
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"
Watermarkerwatermarker=newWatermarker("document.pdf",loadOptions);// Consider only the attached images
watermarker.getSearchableObjects().setPdfSearchableObjects(PdfSearchableObjects.AttachedImages);// Search for similar images
WatermarkableImageCollectionpossibleWatermarks=watermarker.getImages();System.out.println("Found "+possibleWatermarks.getCount()+" image(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.