List of usage examples for com.lowagie.text.pdf PdfWriter addFileAttachment
public void addFileAttachment(PdfFileSpecification fs) throws IOException
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;/*from w w w . ja v a 2 s.com*/ 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); }