pdf 生成文件工具类

PDFUtils

public class PDFTempletUtil {
	/** 模板文件路径 **/
	private String templatePdfPath;
	/** **/
	private String ttcPath;
	/** 目标文件路径 **/
	private String targetPdfpath;
	
    private static Logger logger=Logger.getLogger(PDFTempletUtil.class);

	public PDFTempletUtil() {
		super();
	}

	public PDFTempletUtil(String templatePdfPath, String ttcPath, String targetPdfpath) {
		this.templatePdfPath = templatePdfPath;
		this.ttcPath = ttcPath;
		this.targetPdfpath = targetPdfpath;
	}

	/**
	 * 
	 * @param bf
	 *            字体 ,若为null,则使用默认的宋体
	 * @param file
	 *            目的文件
	 * @param signPicPath
	 *            签名表单域名图片路径,多个路径以,号分开
	 * @param fileds
	 *            表单域名
	 * @param mapValues
	 *            表单域名对应值
	 * @param picFileds
	 *            签名表单域名
	 * @throws Exception
	 */
	public void templetTicket(BaseFont bf, File file, String[] picFileds, String signPicPath, String[] fileds,
			Map<String, String> mapValues) throws Exception {

		PdfReader reader = new PdfReader(templatePdfPath);
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		PdfStamper ps = new PdfStamper(reader, bos);
		FileOutputStream fos = new FileOutputStream(file);
		try {
		/* 使用中文字体 */
		if (bf == null) {
			try {
				bf= BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED);

				//bf = BaseFont.createFont("Fonts/simsun.ttc,1", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
			} catch(Exception e) {
				// TODO: handle finally clause
				bf= BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED);
			}
		}
		ArrayList<BaseFont> fontList = new ArrayList<BaseFont>();
		fontList.add(bf);

		AcroFields s = ps.getAcroFields();
		s.setSubstitutionFonts(fontList);
		String[] picPaths = signPicPath.split(",");
		int len = picPaths.length - 1;
		for (int i = 0; i < picFileds.length; i++) {
			if (i <= len && !picPaths[i].equals("") && !picFileds[i].equals("")) {
				insertImage(ps, s, picPaths[i], picFileds[i]);
			}
			if (i >= len)
				break;

		}

		for (int i = 0; i < fileds.length; i++) {
			if (mapValues.containsKey(fileds[i])) {
				s.setField(fileds[i], mapValues.get(fileds[i]));
			}
		}

		ps.setFormFlattening(true);
		ps.close();

		
		fos.write(bos.toByteArray());
		fos.close();
		} catch (Exception e) {
			// TODO: handle exception
			logger.info(e.getMessage());
		}finally {
			ps.close();
			//bos.close();
			fos.close();
		}
	}

	public static void insertImage(PdfStamper ps, AcroFields s, String picPath, String picField) {

		try {

			List<AcroFields.FieldPosition> list = s.getFieldPositions(picField);
			Rectangle signRect = list.get(0).position;

			Image image = Image.getInstance(picPath);
			PdfContentByte under = ps.getOverContent(list.get(0).page);
			float x = signRect.getLeft();
			float y = signRect.getBottom();
			image.setAbsolutePosition(x, y);

			image.scaleToFit(signRect.getWidth(), signRect.getHeight());

			under.addImage(image);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	/**
	 * @return the templatePdfPath
	 */
	public String getTemplatePdfPath() {
		return templatePdfPath;
	}

	/**
	 * @param templatePdfPath
	 *            the templatePdfPathto set
	 */
	public void setTemplatePdfPath(String templatePdfPath) {
		this.templatePdfPath = templatePdfPath;
	}

	/**
	 * @return the ttcPath
	 */
	public String getTtcPath() {
		return ttcPath;
	}

	/**
	 * @param ttcPath
	 *            the ttcPath to set
	 */
	public void setTtcPath(String ttcPath) {
		this.ttcPath = ttcPath;
	}

	/**
	 * @return the targetPdfpath
	 */
	public String getTargetPdfpath() {
		return targetPdfpath;
	}

	/**
	 * @param targetPdfpath
	 *            the targetPdfpath toset
	 */
	public void setTargetPdfpath(String targetPdfpath) {
		this.targetPdfpath = targetPdfpath;
	}

	
}

猜你喜欢

转载自blog.csdn.net/m0_37948170/article/details/80559689
今日推荐