2012-05-16 73 views

回答

4

您可以使用此方法src路徑到原始文件,dest路徑到新創建的PDF,以及attachments是路徑的數組將文件要附加:

public void addAttachments(
    String src, String dest, String[] attachments) throws IOException, DocumentException { 
    PdfReader reader = new PdfReader(src); 
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest)); 
    for (int i = 0; i < attachments.length; i++) { 
    addAttachment(stamper.getWriter(), new File(attachments[i])); 
    } 
    stamper.close(); 
} 

protected void addAttachment(PdfWriter writer, File src) throws IOException { 
    PdfFileSpecification fs = 
    PdfFileSpecification.fileEmbedded(writer, src.getAbsolutePath(), src.getName(), null); 
    writer.addFileAttachment(src.getName().substring(0, src.getName().indexOf('.')), fs); 
} 
+0

太感謝你了它的工作... – ranjan

+0

@ranjan標記答案如果它可以接受的話。 – vikiiii

+0

我可以將PDF格式從PDF轉換爲pdf/a – ranjan