/**
* Copyright (c) 2013-Now http://jeesite.com All rights reserved.
*/
package com.jeesite.test;
import org.apache.shiro.crypto.AesCipherService;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Iterator;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
public class RememberMeKeyGen {
static String gouimg = "D:\\工作文件\\donghao\\开户\\gou.png";
static int y = 790;
public static void main(String[] args) throws Exception{
genPdf();
}
public static void genPdf() throws Exception {
PdfReader reader = new PdfReader("D:\\工作文件\\donghao\\开户\\kh.pdf");
PdfStamper stamper = new PdfStamper(reader,
new FileOutputStream("D:\\工作文件\\donghao\\开户\\111.pdf"));
BaseFont mingliu = BaseFont.createFont("D:\\工作文件\\donghao\\开户\\mingliu.ttc,0",
BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
BaseFont air = BaseFont.createFont("D:\\工作文件\\donghao\\开户\\calibri.ttf", BaseFont.IDENTITY_H,
BaseFont.NOT_EMBEDDED);
Font fontMingliu = new Font(mingliu, 10);
Font fontair = new Font(air, 10);
String gou = "";
// "/Users/heyanchao/Desktop/hyc/img/font/gou.jpg";
String goukuang = "";
// "/Users/heyanchao/Desktop/hyc/img/font/gou_kuang.jpg";
for (int i = 1; i <= reader.getNumberOfPages(); i++) {
PdfContentByte over = stamper.getOverContent(i);
if(i ==1) {
setPage1(over,fontMingliu);
}
}
stamper.close();
}
public static void setPage1(PdfContentByte over, Font fontMingliu) throws IOException, DocumentException {
// 財務狀況
addImg(over, gouimg, 44, y-93);
addImg(over, gouimg, 153, y-93);
}
public static void addtext(PdfContentByte over, Font font, String content, float w, float h) {
if (content == null) {
content = "";
}
over.beginText();
over.setFontAndSize(font.getBaseFont(), font.getSize());
over.setColorFill(BaseColor.BLACK);
over.setTextMatrix(w, h);
over.showText(content);
over.endText();
}
public static void addImg(PdfContentByte over, String gou, float w, float h)
throws MalformedURLException, IOException, DocumentException {
Image tongxun = Image.getInstance(gou);
tongxun.setAbsolutePosition(w, h);// 同上 123, 481.7f
tongxun.scaleToFit(6, 6);// 大小
over.addImage(tongxun);
}
public static void addImg3(PdfContentByte over, String gou, float w, float h)
throws MalformedURLException, IOException, DocumentException {
Image tongxun = Image.getInstance(gou);
tongxun.setAbsolutePosition(w, h);// 同上 123, 481.7f
tongxun.scaleToFit(50, 30);// 大小
over.addImage(tongxun);
}
public static void addImg2(PdfContentByte over, String gou, float w, float h)
throws MalformedURLException, IOException, DocumentException {
Image tongxun = Image.getInstance(gou);
tongxun.setAbsolutePosition(w, h);// 同上 123, 481.7f
tongxun.scaleToFit(8, 8);// 大小
over.addImage(tongxun);
}
}
java 根据pdf坐标填入内容
最新推荐文章于 2024-07-27 13:31:57 发布