maven引入组件itextpdf
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.13.3</version>
</dependency>
java将图片转pdf
public static File Pdf(String imageUrl, String mOutputPdfFileName) {
Document doc = new Document(PageSize.A4, 0, 0, 0, 0);
doc.setMargins(0f, 0f, 12f, 0f);
try {
PdfWriter.getInstance(doc, new FileOutputStream(mOutputPdfFileName));
doc.open();
doc.newPage();
Image png1 = Image.getInstance(imageUrl);
float heigth = png1.getHeight();
float width = png1.getWidth();
int percent = getPercent2(heigth, width)+2;
png1.setAlignment(Image.MIDDLE);
png1.scalePercent(percent);
doc.add(png1);
doc.close();
System.out.println("生成pdf "+mOutputPdfFileName);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
File mOutputPdfFile = new File(mOutputPdfFileName);
if (!mOutputPdfFile.exists()) {
mOutputPdfFile.deleteOnExit();
return null;
}
return mOutputPdfFile;
}
public static int getPercent2(float h, float w) {
int p = 0;
float p2 = 0.0f;
p2 = 530 / w * 100;
p = Math.round(p2);
return p;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)