Example usage for com.lowagie.text.pdf PdfFileSpecification fileEmbedded

List of usage examples for com.lowagie.text.pdf PdfFileSpecification fileEmbedded

Introduction

In this page you can find the example usage for com.lowagie.text.pdf PdfFileSpecification fileEmbedded.

Prototype

public static PdfFileSpecification fileEmbedded(PdfWriter writer, String filePath, String fileDisplay,
        byte fileStore[], String mimeType, PdfDictionary fileParameter, int compressionLevel)
        throws IOException 

Source Link

Document

Creates a file specification with the file embedded.

Usage

From source file:mitm.common.pdf.MessagePDFBuilder.java

License:Open Source License

private void addAttachment(final Part part, PdfWriter pdfWriter) throws IOException, MessagingException {
    byte[] content = IOUtils.toByteArray(part.getInputStream());

    String filename = StringUtils
            .defaultString(HeaderUtils.decodeTextQuietly(MimeUtils.getFilenameQuietly(part)), "attachment.bin");

    String baseType;/*  w  w w . j a  v  a  2 s .  c  o m*/

    String contentType = null;

    try {
        contentType = part.getContentType();

        MimeType mimeType = new MimeType(contentType);

        baseType = mimeType.getBaseType();
    } catch (MimeTypeParseException e) {
        /*
         * Can happen when the content-type is not correct. Example with missing ; between charset and name:
         * 
         * Content-Type: application/pdf;
         *      charset="Windows-1252" name="postcard2010.pdf"
         */
        logger.warn("Unable to infer MimeType from content type. Fallback to application/octet-stream. "
                + "Content-Type: " + contentType);

        baseType = MimeTypes.OCTET_STREAM;
    }

    PdfFileSpecification fileSpec = PdfFileSpecification.fileEmbedded(pdfWriter, null, filename, content,
            true /* compress */, baseType, null);

    pdfWriter.addFileAttachment(fileSpec);
}