`
apchy
  • 浏览: 59590 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

利用itext往pdf追加图片

    博客分类:
  • java
阅读更多
import java.io.FileOutputStream;

import com.lowagie.text.Document;
import com.lowagie.text.Image;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfStamper;


public class PdfTest {

/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
PdfReader reader = new PdfReader("C:\\1.pdf");
  int n = reader.getNumberOfPages();
  Document document = new Document(reader.getPageSize(n));
  float width = document.getPageSize().width();
  // Create a stamper that will copy the document to a new file
  PdfStamper stamp = new PdfStamper(reader, 
    new FileOutputStream("c:\\text1.pdf"));
  PdfContentByte over;
  Image img = Image.getInstance("c:\\view.png");
  width = width - img.width();
  System.out.println("width:" + width);
  img.setAbsolutePosition(width, 0);
  img.setAlignment(Image.ALIGN_RIGHT);
  if(n > 0)
  {
    // Text over the existing page
    over = stamp.getOverContent(n);
    over.addImage(img);
  }
  stamp.close();
}
}


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics