PDFボックスを使用して、ドキュメントをPDFに添付できます。
PDComplexFileSpecification fs = new PDComplexFileSpecification();
fs.setFile( "Test.txt" );
InputStream is = ...;
PDEmbeddedFile ef = new PDEmbeddedFile(doc, is );
//set some of the attributes of the embedded file
ef.setSubtype( "test/plain" );
ef.setSize( data.length );
ef.setCreationDate( new GregorianCalendar() );
fs.setEmbeddedFile( ef );
Map efMap = new HashMap();
efMap.put( "My first attachment", fs );
efTree.setNames( efMap );
PDDocumentNameDictionary names = new PDDocumentNameDictionary( doc.getDocumentCatalog() );
names.setEmbeddedFiles( efTree );
doc.getDocumentCatalog().setNames( names );
しかし、そのファイルを圧縮する方法は? PDStream にメソッド addCompression() があることは知っています。私はそれを使うべきですか?
私は解決策を見つけました:
PDEmbeddedFile embeddedFile = new PDEmbeddedFile(doc, attachedFileInputStream );
List<COSName> list = new ArrayList<COSName>();
COSName filter = COSName.LZW_DECODE;
list.add(filter);
embeddedFile.setFilters(list);
embeddedFile.setFileFilters(null);
embeddedFile.addCompression();