Java 使用(BufferedImage)ImageIO下载抓取多个图片合成pdf文件

本文主要介绍Java中,使用(BufferedImage)ImageIO下载网页上图片文档,通过aspose.pdf或aspose.word将多个pdf合成到单个pdf文件中。

1、aspose.pdf和aspose.word图片转pdf文档

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

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

2、使用aspose.pdf实现代码

	/**
	 * 
	 * Converts an image to PDF using aspose.pdf for Java.
	 *
	 * 
	 * 
	 * @param inputImgUrls 所有文件档图片地址
	 * 
	 * @param outputFileName 保存的pdf文件名
	 * 
	 */
	public static void convertImageToPdf(ArrayList<String> inputImgUrls, String outputFileName) throws Exception
	{
		// Instantiate Document Object
		try {
			com.aspose.pdf.Document doc = new com.aspose.pdf.Document();
			URL imgUrl = null;
			BufferedImage bufferedImage = null;
			for (int i = 0; i < inputImgUrls.size(); i++)
			//
			{
				imgUrl = new URL(inputImgUrls.get(i));
				// Add a page to pages collection of document
				Page page = doc.getPages().add();
				// Load the source image file to Stream object
				bufferedImage = (BufferedImage) ImageIO.read(imgUrl);
				byte[] buffer = ((DataBufferByte) (bufferedImage).getRaster().getDataBuffer()).getData();
				java.io.InputStream fs = new java.io.ByteArrayInputStream(buffer);
				// 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 {
		}
	}

3、aspose.word实现代码

/**
	 * 
	 * Converts an image to PDF using aspose.words for Java.
	 *
	 * 
	 * 
	 * @param inputImgUrls 所有文件档图片地址
	 * 
	 * @param outputFileName 保存的pdf文件名
	 * 
	 */
public static void convertImageToPdf(ArrayList<String> inputImgUrls, String outputFileName) throws Exception
	{
		// 验证License
		if (!getLicense()) {
			return;
		}
		Document doc = new Document();
		DocumentBuilder builder = new DocumentBuilder(doc);
		try {
			for (int i = 0; i < inputImgUrls.size(); i++) {
				if (i != 0)
					builder.insertBreak(BreakType.SECTION_BREAK_NEW_PAGE);
				URL imgUrl = new URL(inputImgUrls.get(i));// 网络图片
				BufferedImage image = (BufferedImage) ImageIO.read(imgUrl);
				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 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文件

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

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

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

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

推荐阅读
cjavapy编程之路首页