1、Aspose组件下载
Aspose下载地址:https://products.aspose.com/pdf/java
破解版下载地址:https://download.csdn.net/download/ahgaoyong/9615804
官方文档地址:https://docs.aspose.com/display/pdfjava/Home
官方Demo代码:https://github.com/aspose-pdf/Aspose.pdf-for-Java
2、多张图片转成pdf
1) 验证license
/**
* 获取license
*
* @return
*/
public static boolean getLicense() {
boolean result = false;
try {
InputStream is = Test.class.getClassLoader().getResourceAsStream("\\license.xml");
License aposeLic = new License();
aposeLic.setLicense(is);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
2) 实现代码
public static void convertImageToPdf(ArrayListinputImgPaths, String outputFileName) throws Exception {// Instantiate Document Objecttry {com.aspose.pdf.Document doc = new com.aspose.pdf.Document();for (int i = 0; i < inputImgPaths.size(); i++)//{// Add a page to pages collection of documentPage page = doc.getPages().add();// Load the source image file to Stream objectjava.io.FileInputStream fs = new java.io.FileInputStream(inputImgPaths.get(i));// Set margins so image will fit, etc.page.getPageInfo().getMargin().setBottom(0);page.getPageInfo().getMargin().setTop(0);page.getPageInfo().getMargin().setLeft(0);page.getPageInfo().getMargin().setRight(0);page.setCropBox(new Rectangle(0, 0, 400, 400));// Create an image objectImage image1 = new Image();// Add the image into paragraphs collection of the sectionpage.getParagraphs().add(image1);// Set the image file streamimage1.setImageStream(fs);}doc.save(outputFileName);} finally {}}
或者
public static void convertImageToPdf(ArrayListinputImgPaths, String outputFileName) throws Exception {// Instantiate Document Objecttry {com.aspose.pdf.Document doc = new com.aspose.pdf.Document();for (int i = 0; i < inputImgPaths.size(); i++) {// Add a page to pages collection of documentPage page = doc.getPages().add();// create image instanceImage image1 = new Image();// create BufferedImage instancejava.awt.image.BufferedImage bufferedImage = ImageIO.read(new File(inputImgPaths.get(i)));ByteArrayOutputStream baos = new ByteArrayOutputStream();// write buffered Image to OutputStream instanceImageIO.write(bufferedImage, "gif", baos);baos.flush();ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());// add image to paragraphs collection of first pagepage.getParagraphs().add(image1);// set image stream as OutputStream holding Buffered imageimage1.setImageStream(bais);}doc.save(outputFileName);} finally {}}
相关文档:
java aspose.cells Excel(.xls,.xlsx)文件转成csv文件和html文件
java利用aspose组件将word转成pdf 中文乱码问题