Java操作pdf添加附件

2018-02-27  本文已影响0人  bc5744e32546
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();
}
}
上一篇下一篇

猜你喜欢

热点阅读