You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@pdfbox.apache.org by Gilad Denneboom <gi...@gmail.com> on 2017/06/13 13:26:31 UTC

Re: How to create embeded attachment i.e different MIME types in PDF

You have an error in at least one of the commented-out lines in your code:
//ef.setSubtype( "test/plain" );

It should be "text/plain"...

On Tue, Jun 13, 2017 at 9:26 AM, developer mania <developermania10@gmail.com
> wrote:

> Hi,
>
> I'm new to Apache PDFBox.jar
>
> Try to creating PDF which contains embedded attachment as (PDF, images, doc
> files, etc...)
>
> Successfully created the plan/text embedded file(s) but failure for other
> types.
>
> /*** Sample code tried which add embedded attachment into PDF ***********/
>
> public void doIt( String file) throws IOException
>     {
>         // the document
>         PDDocument doc = null;
>         try
>         {
>             doc = new PDDocument();
>
>             PDPage page = new PDPage();
>             doc.addPage( page );
>             PDFont font = PDType1Font.HELVETICA_BOLD;
>
>             PDPageContentStream contentStream = new
> PDPageContentStream(doc, page);
>             contentStream.beginText();
>             contentStream.setFont( font, 12 );
>             contentStream.newLineAtOffset(100, 700);
>             contentStream.showText("Go to Document->File Attachments to
> View Embedded Files");
>             contentStream.endText();
>             contentStream.close();
>
>             //embedded files are stored in a named tree
>             PDEmbeddedFilesNameTreeNode efTree = new
> PDEmbeddedFilesNameTreeNode();
>
>             try{
>             File file1 = new File("/resources/"
>                     + "AboutUSHeader.jpg");
>             }
>             catch (Exception ex){
>
>             }
>             //file.
>
>             //first create the file specification, which holds the embedded
> file
>             PDComplexFileSpecification fs = new
> PDComplexFileSpecification();
>             fs.setFile( "AboutUSHeader.jpg" );
>             //create a dummy file stream, this would probably normally be a
> FileInputStream
>             //byte[] data = "This is the contents of the embedded
> file".getBytes("ISO-8859-1");
>             ByteArrayInputStream fakeFile =
>                 new ByteArrayInputStream( file.getBytes() );
>             PDEmbeddedFile ef = new PDEmbeddedFile(doc, fakeFile );
>             //now lets some of the optional parameters
>             //ef.setSubtype( "test/plain" );
>             //ef.setSubtype( "application/pdf" );
>             ef.setSubtype( "image/jpg");
>             ef.setSize( file.getBytes().length );
>             ef.setCreationDate( new GregorianCalendar() );
>             fs.setEmbeddedFile( ef );
>
>             // create a new tree node and add the embedded file
>             PDEmbeddedFilesNameTreeNode treeNode = new
> PDEmbeddedFilesNameTreeNode();
>             treeNode.setNames( Collections.singletonMap( "My first
> attachment",  fs ) );
>             // add the new node as kid to the root node
>             List<PDEmbeddedFilesNameTreeNode> kids = new
> ArrayList<PDEmbeddedFilesNameTreeNode>();
>             kids.add(treeNode);
>             efTree.setKids(kids);
>             // add the tree to the document catalog
>             PDDocumentNameDictionary names = new PDDocumentNameDictionary(
> doc.getDocumentCatalog() );
>             names.setEmbeddedFiles( efTree );
>             doc.getDocumentCatalog().setNames( names );
>
>
>             doc.save( file );
>         }
>         finally
>         {
>             if( doc != null )
>             {
>                 doc.close();
>             }
>         }
>     }
>
>
> Any suggestion will be helpfull or any alternate way to add embedded
> attachments.
>
> Thanks in advance
>