1、Aspose组件下载
Aspose下载地址:https://products.aspose.com/words/java
破解版下载地址:https://download.csdn.net/download/ahgaoyong/9615854
官方文档地址:https://docs.aspose.com/display/wordsjava/Home
官方Demo代码:https://github.com/aspose-words/Aspose.Words-for-Java
2、将多帧图片转成pdf
/** * 获取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; } public static void convertImageToPdf(String inputFileName, String outputFileName) throws Exception { // 验证License if (!getLicense()) { return; } // 创建 Aspose.Words.Document 和 DocumentBuilder. // 构建器使向文档添加内容变得很简单。 Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); // 使用approriate读取器从磁盘加载图像。 ImageInputStream iis = ImageIO.createImageInputStream(new File(inputFileName)); ImageReader reader = ImageIO.getImageReaders(iis).next(); reader.setInput(iis, false); try { // 获取图像中的帧数。 int framesCount = reader.getNumImages(true); // 遍历所有帧。 for (int frameIdx = 0; frameIdx < framesCount; frameIdx++) { // 如果是多帧图像,则在每个新页面之前插入分段。 if (frameIdx != 0) builder.insertBreak(BreakType.SECTION_BREAK_NEW_PAGE); BufferedImage image = reader.read(frameIdx); // Max page size double maxPageHeight = 1584; double maxPageWidth = 1584; double currentImageHeight = ConvertUtil.pixelToPoint(image.getHeight()); double currentImageWidth = ConvertUtil.pixelToPoint(image.getWidth()); if (currentImageWidth >= maxPageWidth || currentImageHeight >= maxPageHeight) { // Get max image size. 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 { if (iis != null) { iis.close(); reader.dispose(); } } // 将文档保存为PDF doc.save(outputFileName); } public static double[] CalculateImageSize(BufferedImage img, double containerHeight, double containerWidth, double targetHeight, double targetWidth) throws Exception { // Calculate width and height targetHeight = containerHeight; targetWidth = containerWidth; // Get size of an image double 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)); else targetWidth = (targetWidth * (ratioWidth / ratioHeight)); } double[] size = new double[2]; size[0] = targetWidth; // width size[1] = targetHeight; // height return (size); }
相关文档:
java aspose.cells Excel(.xls,.xlsx)文件转成csv文件和html文件