Add attachment to pdf

Help
2011-11-09
2013-05-28
  • Stefan Heller
    Stefan Heller
    2011-11-09

    Could you please provide a sample on how to attach a file to a pdf?

     

  • Anonymous
    2011-11-11

    If you add this class as a subclass of the already existing CommonJPodExample it should do the job

    /*
     * Copyright (c) 2007, intarsys consulting GmbH
     *
     * Redistribution and use in source and binary forms, with or without
     * modification, are permitted provided that the following conditions are met:
     *
     * - Redistributions of source code must retain the above copyright notice,
     *   this list of conditions and the following disclaimer.
     *
     * - Redistributions in binary form must reproduce the above copyright notice,
     *   this list of conditions and the following disclaimer in the documentation
     *   and/or other materials provided with the distribution.
     *
     * - Neither the name of intarsys nor the names of its contributors may be used
     *   to endorse or promote products derived from this software without specific
     *   prior written permission.
     *
     * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
     * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
     * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     * POSSIBILITY OF SUCH DAMAGE.
     */
    package de.intarsys.pdf.example.annotation.attachment;
    import de.intarsys.pdf.example.common.CommonJPodExample;
    import de.intarsys.pdf.pd.PDEmbeddedFile;
    import de.intarsys.pdf.pd.PDFileAttachmentAnnotation;
    import de.intarsys.pdf.pd.PDFileSpecification;
    import de.intarsys.pdf.pd.PDPage;
    /**
     * Create a new document and a file attachment.
     */
    public class CreateAttachment extends CommonJPodExample {
        public static void main(String[] args) {
            CreateAttachment client = new CreateAttachment();
            try {
                client.run(args);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        public void addAttachment(PDPage page) {
            PDFileAttachmentAnnotation annot = (PDFileAttachmentAnnotation) PDFileAttachmentAnnotation.META
                    .createNew();
            page.addAnnotation(annot);
            annot.setPrint(false);
            annot.setColor(new float[] { 1, 1, 0 });
            PDFileAttachmentAnnotation faAnnot = annot;
            PDFileSpecification fileSpec = (PDFileSpecification) PDFileSpecification.META
                    .createNew();
            fileSpec.setFileSpecificationString(PDFileSpecification.DK_F,
                    "test.txt");
            faAnnot.setFileSpecification(fileSpec);
            faAnnot.setContents("test.txt");
            faAnnot.setSubject("File Attachment");
            PDEmbeddedFile embeddedFile = (PDEmbeddedFile) PDEmbeddedFile.META
                    .createNew();
            embeddedFile.setBytes("Hello, world!".getBytes());
            fileSpec.setEmbeddedFile(PDFileSpecification.DK_F, embeddedFile);
        }
        public PDPage addPage() {
            // Then create the first page.
            PDPage page = (PDPage) PDPage.META.createNew();
            // add page to the document.
            getDoc().addPageNode(page);
            return page;
        }
        public void run(String[] args) throws Exception {
            if (args.length < 1) {
                usage();
                return;
            }
            try {
                String outputFileName = args[0];
                create();
                PDPage page = addPage();
                addAttachment(page);
                save(outputFileName);
            } finally {
                close();
            }
        }
        /**
         * Help the user.
         */
        public void usage() {
            System.out.println("usage: java.exe " + getClass().getName() //$NON-NLS-1$
                    + " <output-filename>"); //$NON-NLS-1$
        }
    }
    
     

  • Anonymous
    2011-11-11

    B.t.w. - its all about the SPEC…..

     
  • Stefan Heller
    Stefan Heller
    2011-11-14

    Works fine, thanks for the example.
    I read in the pdf specs that an attachment description is a pdf 1.6 feature - is there a way to set it with jpod - together with some other parameters like creation date and attachment size?

     

  • Anonymous
    2011-11-14

    I don't know what you mean with "attachment description". THere's a /Content with annotation - just use the setter.

    There's also a /Desc with a FileDescriptor (which is not an annotation). If there's no getter/setter sounding like the corresponding object property in the spec, just use the generic "getField/setField" to access them.

    The same holds true for the other properties of annotations, while created  (setModified) and size (setRectangle) should be quite obvious.

     
  • Stefan Heller
    Stefan Heller
    2011-11-14

    In my PDF viewer there is an extra tab for PDF attachments, there are 4 columns in this grid: filename, description, creation date and attachment size. That was what I ment. By using your sample there is only the first column set with the filename, the other fields are set to "unknown".