开发手册 欢迎您!
软件开发者资料库

Java 使用aspose.word多张图片转成pdf的方法及示例代码

本文主要介绍Java中,使用aspose.words将多张图片,转成pdf文件,每张占一页。以及转换示例代码。

1、Aspose组件下载

Aspose下载地址https://products.aspose.com/words/java

破解版下载地址https://download.csdn.net/download/ahgaoyong/9615854

官方文档地址https://docs.aspose.com/display/wordsjava/Home

官方Demo代码Words-for-Java" rel="nofollow">https://github.com/aspose-words/Aspose.Words-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) 多图片转成pdf

     public static void convertImageToPdf(ArrayList inputImgPaths, String outputFileName) throws Exception{                // 验证License                if (!getLicense()) {                  return;                 }Document doc = new Document();DocumentBuilder builder = new DocumentBuilder(doc);try{for (int i = 0; i < inputImgPaths.size(); i++){if (i != 0)builder.insertBreak(BreakType.SECTION_BREAK_NEW_PAGE);File file = new File(inputImgPaths.get(i));// 本地图片BufferedImage image = (BufferedImage) ImageIO.read(file);double maxPageHeight = 1584;double maxPageWidth = 1584;double currentImageHeight = ConvertUtil.pixelToPoint(image.getHeight());double currentImageWidth = ConvertUtil.pixelToPoint(image.getWidth());if (currentImageWidth >= maxPageWidth || currentImageHeight >= maxPageHeight){double[] size = CalculateImageSize(image, maxPageHeight, maxPageWidth, currentImageHeight,currentImageWidth);currentImageWidth = size[0];currentImageHeight = size[1];}PageSetup ps = builder.getPageSetup();ps.setPageWidth(currentImageWidth);ps.setPageHeight(currentImageHeight);Shape shape = builder.insertImage(image,RelativeHorizontalPosition.PAGE,0,RelativeVerticalPosition.PAGE,0,ps.getPageWidth(),ps.getPageHeight(),WrapType.NONE);}}finally {}// Save the document to PDF.doc.save(outputFileName);}
        // 等比计算图片尺寸public static double[] CalculateImageSize(BufferedImage img, double containerHeight, double containerWidth,double targetHeight, double targetWidth) throws Exception {// Calculate width and heighttargetHeight = containerHeight;targetWidth = containerWidth;// Get size of an imagedouble imgHeight = ConvertUtil.pixelToPoint(img.getHeight());double imgWidth = ConvertUtil.pixelToPoint(img.getWidth());if (imgHeight < targetHeight && imgWidth < targetWidth){targetHeight = imgHeight;targetWidth = imgWidth;}else{// 计算文档中图像的大小double ratioWidth = imgWidth / targetWidth;double ratioHeight = imgHeight / targetHeight;if (ratioWidth > ratioHeight)targetHeight = (targetHeight * (ratioHeight / ratioWidth));elsetargetWidth = (targetWidth * (ratioWidth / ratioHeight));}double[] size = new double[2];size[0] = targetWidth; // widthsize[1] = targetHeight; // heightreturn (size);}

相关文档:

java aspose.cells Excel(.xls,.xlsx)文件转成csv文件和html文件

java利用aspose组件将word转成pdf 中文乱码问题

java 使用URLConnection下载抓取多个图片合成单个pdf文件

Java 使用aspose.words将一张多帧(frame)图片转成pdf的方法及示例代码