ITeye博客

利用itext往pdf追加图片

apchy · 2010-09-29 17:39 · java · 编程语言 · Markdown

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(); 
}

}

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。