public class Pdftest { public static void main(String[] args) throws Exception { PdfReader reader = new PdfReader("E:\\src.pdf"); int n = reader.getNumberOfPages(); //pdf有几页 Document document = new Document(reader.getPageSize(n)); float width = document.getPageSize().getWidth(); float height = document.getPageSize().getHeight(); // 输出文件 PdfStamper stamp = new PdfStamper(reader, new FileOutputStream("E:\\outputFile.pdf")); PdfContentByte over; Image img = Image.getInstance("E:\\test.jpg"); width = width-img.getWidth(); height = height-img.getHeight(); img.setAbsolutePosition(width, height); //以左下角为坐标原点 img.setAlignment(Image.ALIGN_RIGHT); if(n > 0) { over = stamp.getOverContent(1); //在第几页放图片 (第一页为1) over.addImage(img); } stamp.close(); } }
注:需要itextpdf-5.4.5.jar包
<dependency> <groupId>com.itextpdf</groupId> <artifactId>itextpdf</artifactId> <version>5.4.5</version> </dependency>