美文网首页
Java操作pdf添加附件

Java操作pdf添加附件

作者: bc5744e32546 | 来源:发表于2018-02-27 16:55 被阅读0次
    package pdf;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import com.itextpdf.text.*
    import com.itextpdf.text.pdf.PdfReader;
    import com.itextpdf.text.pdf.PdfStamper;
    
    public class CreatePdf {
      public static void main(String[] args) throws DocumentException,IOException{
      String in = args[0];
      String out =args[1];
      int n = args.length - 2;
      String[] arr = new String[n];
      for(int i =2;i<args.length;i++){
        arr[i-2] = args[i]
      }
      addAttachment(in,out,arr);
    }
    
    public static void addAttachment(String in,String out,String[] attachs) throws DocumentException,IOException{
      PdfReader reader = new PdfReader(in);
      PdfStamper stamp = new PdfStamper(reader,new FileOutputStream(out));
      for (int i=0;i<attachs.length;i++){
        String attach = attachs[i];
        String[] arr = attach.split("/");
        String attachName = arr[arr.length - 1];
         stamp.addFileAttachment(attachName,null,attach,attachName);
      }
      reader.close();
      pstamp.close();
    }
    }
    

    相关文章

      网友评论

          本文标题:Java操作pdf添加附件

          本文链接:https://www.haomeiwen.com/subject/yqymxftx.html