001: /**
002: * Copyright (c) 2004, www.pdfbox.org
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions are met:
007: *
008: * 1. Redistributions of source code must retain the above copyright notice,
009: * this list of conditions and the following disclaimer.
010: * 2. Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: * 3. Neither the name of pdfbox; nor the names of its
014: * contributors may be used to endorse or promote products derived from this
015: * software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
018: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
019: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
020: * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
021: * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
022: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
023: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
024: * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
026: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027: *
028: * http://www.pdfbox.org
029: *
030: */package org.pdfbox.examples.pdmodel;
031:
032: import java.io.ByteArrayInputStream;
033: import java.io.IOException;
034: import java.util.GregorianCalendar;
035: import java.util.HashMap;
036: import java.util.Map;
037:
038: import org.pdfbox.exceptions.COSVisitorException;
039:
040: import org.pdfbox.pdmodel.PDDocument;
041: import org.pdfbox.pdmodel.PDDocumentNameDictionary;
042: import org.pdfbox.pdmodel.PDEmbeddedFilesNameTreeNode;
043: import org.pdfbox.pdmodel.PDPage;
044:
045: import org.pdfbox.pdmodel.common.filespecification.PDComplexFileSpecification;
046: import org.pdfbox.pdmodel.common.filespecification.PDEmbeddedFile;
047: import org.pdfbox.pdmodel.edit.PDPageContentStream;
048:
049: import org.pdfbox.pdmodel.font.PDFont;
050: import org.pdfbox.pdmodel.font.PDType1Font;
051:
052: /**
053: * This is an example that creates a simple document and embeds a file into it..
054: *
055: * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
056: * @version $Revision: 1.2 $
057: */
058: public class EmbeddedFiles {
059: /**
060: * Constructor.
061: */
062: public EmbeddedFiles() {
063: super ();
064: }
065:
066: /**
067: * create the second sample document from the PDF file format specification.
068: *
069: * @param file The file to write the PDF to.
070: *
071: * @throws IOException If there is an error writing the data.
072: * @throws COSVisitorException If there is an error writing the PDF.
073: */
074: public void doIt(String file) throws IOException,
075: COSVisitorException {
076: // the document
077: PDDocument doc = null;
078: try {
079: doc = new PDDocument();
080:
081: PDPage page = new PDPage();
082: doc.addPage(page);
083: PDFont font = PDType1Font.HELVETICA_BOLD;
084:
085: PDPageContentStream contentStream = new PDPageContentStream(
086: doc, page);
087: contentStream.beginText();
088: contentStream.setFont(font, 12);
089: contentStream.moveTextPositionByAmount(100, 700);
090: contentStream
091: .drawString("Go to Document->File Attachments to View Embedded Files");
092: contentStream.endText();
093: contentStream.close();
094:
095: //embedded files are stored in a named tree
096: PDEmbeddedFilesNameTreeNode efTree = new PDEmbeddedFilesNameTreeNode();
097:
098: //first create the file specification, which holds the embedded file
099: PDComplexFileSpecification fs = new PDComplexFileSpecification();
100: fs.setFile("Test.txt");
101: //create a dummy file stream, this would probably normally be a FileInputStream
102: byte[] data = "This is the contents of the embedded file"
103: .getBytes();
104: ByteArrayInputStream fakeFile = new ByteArrayInputStream(
105: data);
106: PDEmbeddedFile ef = new PDEmbeddedFile(doc, fakeFile);
107: //now lets some of the optional parameters
108: ef.setSubtype("test/plain");
109: ef.setSize(data.length);
110: ef.setCreationDate(new GregorianCalendar());
111: fs.setEmbeddedFile(ef);
112:
113: //now add the entry to the embedded file tree and set in the document.
114: Map efMap = new HashMap();
115: efMap.put("My first attachment", fs);
116: efTree.setNames(efMap);
117: PDDocumentNameDictionary names = new PDDocumentNameDictionary(
118: doc.getDocumentCatalog());
119: names.setEmbeddedFiles(efTree);
120: doc.getDocumentCatalog().setNames(names);
121:
122: doc.save(file);
123: } finally {
124: if (doc != null) {
125: doc.close();
126: }
127: }
128: }
129:
130: /**
131: * This will create a hello world PDF document with an embedded file.
132: * <br />
133: * see usage() for commandline
134: *
135: * @param args Command line arguments.
136: */
137: public static void main(String[] args) {
138: EmbeddedFiles app = new EmbeddedFiles();
139: try {
140: if (args.length != 1) {
141: app.usage();
142: } else {
143: app.doIt(args[0]);
144: }
145: } catch (Exception e) {
146: e.printStackTrace();
147: }
148: }
149:
150: /**
151: * This will print out a message telling how to use this example.
152: */
153: private void usage() {
154: System.err.println("usage: " + this .getClass().getName()
155: + " <output-file>");
156: }
157: }
|