KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pdfbox > examples > pdmodel > EmbeddedFiles


1 /**
2  * Copyright (c) 2004, www.pdfbox.org
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  * 3. Neither the name of pdfbox; nor the names of its
14  * contributors may be used to endorse or promote products derived from this
15  * software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
24  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * http://www.pdfbox.org
29  *
30  */

31 package org.pdfbox.examples.pdmodel;
32
33 import java.io.ByteArrayInputStream JavaDoc;
34 import java.io.IOException JavaDoc;
35 import java.util.GregorianCalendar JavaDoc;
36 import java.util.HashMap JavaDoc;
37 import java.util.Map JavaDoc;
38
39 import org.pdfbox.exceptions.COSVisitorException;
40
41 import org.pdfbox.pdmodel.PDDocument;
42 import org.pdfbox.pdmodel.PDDocumentNameDictionary;
43 import org.pdfbox.pdmodel.PDEmbeddedFilesNameTreeNode;
44 import org.pdfbox.pdmodel.PDPage;
45
46 import org.pdfbox.pdmodel.common.filespecification.PDComplexFileSpecification;
47 import org.pdfbox.pdmodel.common.filespecification.PDEmbeddedFile;
48 import org.pdfbox.pdmodel.edit.PDPageContentStream;
49
50 import org.pdfbox.pdmodel.font.PDFont;
51 import org.pdfbox.pdmodel.font.PDType1Font;
52
53
54 /**
55  * This is an example that creates a simple document and embeds a file into it..
56  *
57  * @author <a HREF="mailto:ben@benlitchfield.com">Ben Litchfield</a>
58  * @version $Revision: 1.2 $
59  */

60 public class EmbeddedFiles
61 {
62     /**
63      * Constructor.
64      */

65     public EmbeddedFiles()
66     {
67         super();
68     }
69
70     /**
71      * create the second sample document from the PDF file format specification.
72      *
73      * @param file The file to write the PDF to.
74      *
75      * @throws IOException If there is an error writing the data.
76      * @throws COSVisitorException If there is an error writing the PDF.
77      */

78     public void doIt( String JavaDoc file) throws IOException JavaDoc, COSVisitorException
79     {
80         // the document
81
PDDocument doc = null;
82         try
83         {
84             doc = new PDDocument();
85             
86             PDPage page = new PDPage();
87             doc.addPage( page );
88             PDFont font = PDType1Font.HELVETICA_BOLD;
89             
90             PDPageContentStream contentStream = new PDPageContentStream(doc, page);
91             contentStream.beginText();
92             contentStream.setFont( font, 12 );
93             contentStream.moveTextPositionByAmount( 100, 700 );
94             contentStream.drawString( "Go to Document->File Attachments to View Embedded Files" );
95             contentStream.endText();
96             contentStream.close();
97             
98             //embedded files are stored in a named tree
99
PDEmbeddedFilesNameTreeNode efTree = new PDEmbeddedFilesNameTreeNode();
100             
101             
102             //first create the file specification, which holds the embedded file
103
PDComplexFileSpecification fs = new PDComplexFileSpecification();
104             fs.setFile( "Test.txt" );
105             //create a dummy file stream, this would probably normally be a FileInputStream
106
byte[] data = "This is the contents of the embedded file".getBytes();
107             ByteArrayInputStream JavaDoc fakeFile =
108                 new ByteArrayInputStream JavaDoc( data );
109             PDEmbeddedFile ef = new PDEmbeddedFile(doc, fakeFile );
110             //now lets some of the optional parameters
111
ef.setSubtype( "test/plain" );
112             ef.setSize( data.length );
113             ef.setCreationDate( new GregorianCalendar JavaDoc() );
114             fs.setEmbeddedFile( ef );
115             
116             //now add the entry to the embedded file tree and set in the document.
117
Map JavaDoc efMap = new HashMap JavaDoc();
118             efMap.put( "My first attachment", fs );
119             efTree.setNames( efMap );
120             PDDocumentNameDictionary names = new PDDocumentNameDictionary( doc.getDocumentCatalog() );
121             names.setEmbeddedFiles( efTree );
122             doc.getDocumentCatalog().setNames( names );
123             
124             
125             doc.save( file );
126         }
127         finally
128         {
129             if( doc != null )
130             {
131                 doc.close();
132             }
133         }
134     }
135
136     /**
137      * This will create a hello world PDF document with an embedded file.
138      * <br />
139      * see usage() for commandline
140      *
141      * @param args Command line arguments.
142      */

143     public static void main(String JavaDoc[] args)
144     {
145         EmbeddedFiles app = new EmbeddedFiles();
146         try
147         {
148             if( args.length != 1 )
149             {
150                 app.usage();
151             }
152             else
153             {
154                 app.doIt( args[0] );
155             }
156         }
157         catch (Exception JavaDoc e)
158         {
159             e.printStackTrace();
160         }
161     }
162
163     /**
164      * This will print out a message telling how to use this example.
165      */

166     private void usage()
167     {
168         System.err.println( "usage: " + this.getClass().getName() + " <output-file>" );
169     }
170 }
Popular Tags