---
title: "利用itext往pdf追加图片"
author: "apchy"
date: "2010-09-29T17:39:14Z"
tags: []
categories: ["编程语言"]
slug: "itext-pdf"
draft: false
lastmod: "2010-09-29T17:39:14Z"
id: 775337
url: "https://apchy.iteye.com/blog/775337"
---

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