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(ArrayList<String> inputImgPaths, String outputFileName) throws Exception { // Instantiate Document Object try { 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 document Page page = doc.getPages().add(); // Load the source image file to Stream object java.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 object Image image1 = new Image(); // Add the image into paragraphs collection of the section page.getParagraphs().add(image1); // Set the image file stream image1.setImageStream(fs); } doc.save(outputFileName); } finally { } }
或者
public static void convertImageToPdf(ArrayList<String> inputImgPaths, String outputFileName) throws Exception { // Instantiate Document Object try { 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 document Page page = doc.getPages().add(); // create image instance Image image1 = new Image(); // create BufferedImage instance java.awt.image.BufferedImage bufferedImage = ImageIO.read(new File(inputImgPaths.get(i))); ByteArrayOutputStream baos = new ByteArrayOutputStream(); // write buffered Image to OutputStream instance ImageIO.write(bufferedImage, "gif", baos); baos.flush(); ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); // add image to paragraphs collection of first page page.getParagraphs().add(image1); // set image stream as OutputStream holding Buffered image image1.setImageStream(bais); } doc.save(outputFileName); } finally { } }
相关文档:
java aspose.cells Excel(.xls,.xlsx)文件转成csv文件和html文件
java利用aspose组件将word转成pdf 中文乱码问题