Home » pdfbox-1.1.0-src » org.apache.pdfbox.examples.pdmodel » [javadoc | source]

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *      http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    * Unless required by applicable law or agreed to in writing, software
   12    * distributed under the License is distributed on an "AS IS" BASIS,
   13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    * See the License for the specific language governing permissions and
   15    * limitations under the License.
   16    */
   17   package org.apache.pdfbox.examples.pdmodel;
   18   
   19   import java.io.ByteArrayInputStream;
   20   import java.io.IOException;
   21   import java.util.GregorianCalendar;
   22   import java.util.HashMap;
   23   import java.util.Map;
   24   
   25   import org.apache.pdfbox.exceptions.COSVisitorException;
   26   
   27   import org.apache.pdfbox.pdmodel.PDDocument;
   28   import org.apache.pdfbox.pdmodel.PDDocumentNameDictionary;
   29   import org.apache.pdfbox.pdmodel.PDEmbeddedFilesNameTreeNode;
   30   import org.apache.pdfbox.pdmodel.PDPage;
   31   
   32   import org.apache.pdfbox.pdmodel.common.filespecification.PDComplexFileSpecification;
   33   import org.apache.pdfbox.pdmodel.common.filespecification.PDEmbeddedFile;
   34   import org.apache.pdfbox.pdmodel.edit.PDPageContentStream;
   35   
   36   import org.apache.pdfbox.pdmodel.font.PDFont;
   37   import org.apache.pdfbox.pdmodel.font.PDType1Font;
   38   
   39   
   40   /**
   41    * This is an example that creates a simple document and embeds a file into it..
   42    *
   43    * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
   44    * @version $Revision: 1.2 $
   45    */
   46   public class EmbeddedFiles
   47   {
   48       /**
   49        * Constructor.
   50        */
   51       public EmbeddedFiles()
   52       {
   53           super();
   54       }
   55   
   56       /**
   57        * create the second sample document from the PDF file format specification.
   58        *
   59        * @param file The file to write the PDF to.
   60        *
   61        * @throws IOException If there is an error writing the data.
   62        * @throws COSVisitorException If there is an error writing the PDF.
   63        */
   64       public void doIt( String file) throws IOException, COSVisitorException
   65       {
   66           // the document
   67           PDDocument doc = null;
   68           try
   69           {
   70               doc = new PDDocument();
   71   
   72               PDPage page = new PDPage();
   73               doc.addPage( page );
   74               PDFont font = PDType1Font.HELVETICA_BOLD;
   75   
   76               PDPageContentStream contentStream = new PDPageContentStream(doc, page);
   77               contentStream.beginText();
   78               contentStream.setFont( font, 12 );
   79               contentStream.moveTextPositionByAmount( 100, 700 );
   80               contentStream.drawString( "Go to Document->File Attachments to View Embedded Files" );
   81               contentStream.endText();
   82               contentStream.close();
   83   
   84               //embedded files are stored in a named tree
   85               PDEmbeddedFilesNameTreeNode efTree = new PDEmbeddedFilesNameTreeNode();
   86   
   87   
   88               //first create the file specification, which holds the embedded file
   89               PDComplexFileSpecification fs = new PDComplexFileSpecification();
   90               fs.setFile( "Test.txt" );
   91               //create a dummy file stream, this would probably normally be a FileInputStream
   92               byte[] data = "This is the contents of the embedded file".getBytes();
   93               ByteArrayInputStream fakeFile =
   94                   new ByteArrayInputStream( data );
   95               PDEmbeddedFile ef = new PDEmbeddedFile(doc, fakeFile );
   96               //now lets some of the optional parameters
   97               ef.setSubtype( "test/plain" );
   98               ef.setSize( data.length );
   99               ef.setCreationDate( new GregorianCalendar() );
  100               fs.setEmbeddedFile( ef );
  101   
  102               //now add the entry to the embedded file tree and set in the document.
  103               Map efMap = new HashMap();
  104               efMap.put( "My first attachment", fs );
  105               efTree.setNames( efMap );
  106               PDDocumentNameDictionary names = new PDDocumentNameDictionary( doc.getDocumentCatalog() );
  107               names.setEmbeddedFiles( efTree );
  108               doc.getDocumentCatalog().setNames( names );
  109   
  110   
  111               doc.save( file );
  112           }
  113           finally
  114           {
  115               if( doc != null )
  116               {
  117                   doc.close();
  118               }
  119           }
  120       }
  121   
  122       /**
  123        * This will create a hello world PDF document with an embedded file.
  124        * <br />
  125        * see usage() for commandline
  126        *
  127        * @param args Command line arguments.
  128        */
  129       public static void main(String[] args)
  130       {
  131           EmbeddedFiles app = new EmbeddedFiles();
  132           try
  133           {
  134               if( args.length != 1 )
  135               {
  136                   app.usage();
  137               }
  138               else
  139               {
  140                   app.doIt( args[0] );
  141               }
  142           }
  143           catch (Exception e)
  144           {
  145               e.printStackTrace();
  146           }
  147       }
  148   
  149       /**
  150        * This will print out a message telling how to use this example.
  151        */
  152       private void usage()
  153       {
  154           System.err.println( "usage: " + this.getClass().getName() + " <output-file>" );
  155       }
  156   }

Home » pdfbox-1.1.0-src » org.apache.pdfbox.examples.pdmodel » [javadoc | source]