java将图片写入pdf

需要的jar:
itext-2.1.7.jar

	public static void main(String[] args) {
		image2pdf(imgPath, pdfPath);
	}
	
	/***
	 * @param picturePath
	 *            图片地址
	 */
	private static void createPic(Document document, String picturePath) {
		try {
			Image image = Image.getInstance(picturePath);
			float documentWidth = document.getPageSize().getWidth() - document.leftMargin() - document.rightMargin();
			float documentHeight = documentWidth / 580 * 320;// 重新设置宽高
			image.scaleAbsolute(documentWidth, documentHeight);// 重新设置宽高
			document.add(image);
		} catch (Exception ex) {
		}
	}

	public static void image2pdf(String text, String pdf) throws DocumentException, IOException {
		Document document = new Document();
		OutputStream os = new FileOutputStream(new File(pdf));
		PdfWriter.getInstance(document, os);
		document.open();
		createPic(document, text);
		document.close();
	}

猜你喜欢

转载自blog.csdn.net/qq_38949960/article/details/94733409