通过 Java 将 PDF 合并到 PNG

在 Java 上将 PDF 文档合并为单一格式 PNG。程序员可以使用此示例代码在任何基于 Web 或桌面 Java 的应用程序中组合 PDF。

使用 Java 将 PDF 合并到 PNG

如何将 PDF 合并到 PNG?使用 Aspose.PDF for Java 库,你可以轻松地以编程方式将 PDF 合并到 PNG。Aspose 的 PDF 软件非常适合个人以及小型或大型企业。由于它能够处理大量信息,因此可以快速高效地进行连接并保护您的数据。Aspose.PDF 的一个特殊功能是用于将 PDF 合并到 PNG 的 API。

你可以直接从 Maven 下载其最新版本,然后在 pom.xml 中添加以下配置,将其安装在你的基于 Maven 的项目中。

文档 页面上查看安装库的详细信息。要验证该库的优点,请尝试使用将 PDF 转换为 PNG 的代码片段。

Repository

<repository>
    <id>AsposeJavaAPI</id>
    <name>Aspose Java AP</name>
    <url>https://releases.aspose.com/java/repo/</url>
</repository>

Dependency

<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-pdf</artifactId>
<version>version of aspose-pdf API</version>
</dependency>

如何通过 Java 将 PDF 合并到 PNG


Java 开发人员只需几行代码即可轻松地将 PDF 文件加载并合并到 PNG。

  1. 初始化一个新文档,然后运行一个循环来合并文件
  2. 循环中:向 PNG 文档添加新页面
  3. 循环中:将 PDF 文件添加到新页面
  4. 循环后保存结果

以下示例演示了如何在 Java 中将 PDF 合并到 PNG。轻松地将多个文档合并为一个文件。如果您在 Java 中开发代码,则此任务可能比听起来更简单。您可以使用完全限定的文件名进行 PDF 读取和 PNG 写入。请看这个 Java 示例,该示例说明如何使用 Java 将相同或不同文件类型的多个文档合并为一个文件。

使用 Java 合并 PDF 文件并另存为 PNG

示例 Java:此示例代码显示了 PDF 到 PNG 的连接

Input file:

File not added

File not added

Output format:

PNG

Output file:


	// create empty image with calculated width and hight
	// use file system as source for save image
	fileSource = new com.aspose.imaging.sources.FileCreateSource(
		"Merger_pdf_png.png",
		false);
	options = new com.aspose.imaging.imageoptions.PngOptions();
	options.setSource(fileSource);
	newImage = (com.aspose.imaging.fileformats.png.PngImage) com.aspose.imaging.Image.create(options, newWidth, newHeight);
	int stitchedWidth = 0;

	// read pdf file to Aspose Document
	doc = new com.aspose.pdf.Document("1.pdf");

	// setup default resolution to pdf documents 72dpi
	// create image device to save document as image with page dimensions and resolution
	// process document page to image
	// create bounds to nsert small image into large
	for (int pageCount = 1; pageCount <= doc.getPages().size(); pageCount++) {
		resolution = new com.aspose.pdf.devices.Resolution(72);
		imageDevice = new com.aspose.pdf.devices.PngDevice(
						doc.getPages().get_Item(pageCount).getPageInfo().getWidth(),
						doc.getPages().get_Item(pageCount).getPageInfo().getHeight(),
						new com.aspose.pdf.devices.Resolution(72));
		String outPath = "test_" + pageCount + ".png";
		imageDevice.process(doc.getPages().get_Item(pageCount), outPath);

		image = (com.aspose.imaging.RasterImage) com.aspose.imaging.Image.load(outPath);
		bounds = new com.aspose.imaging.Rectangle(
							stitchedWidth,
							0,
							image.getWidth(),
							image.getHeight());

		//combining images into new one
		newImage.saveArgb32Pixels(bounds,image.loadArgb32Pixels(image.getBounds()));
		stitchedWidth += image.getWidth();
	}

	// save created image to disk
	newImage.save();

Java 可将 PDF 合并到 PNG 的库

Aspose.PDF for Java API 是一个允许开发人员向其应用程序添加 PDF 处理功能的库。它可用于构建任何类型的 32 位和 64 位应用程序,无需使用 Adobe Acrobat 即可生成或读取、转换和操作 PDF 文件。Aspose.PDF for Java 允许开发人员在 PDF 文档中插入表格、图形、图像、超链接、自定义字体等。此外,还可以压缩 PDF。适用于 Java 的 Aspose.PDF 为开发安全的 PDF 文件提供了出色的安全功能。

你可以在 文档示例 上找到有关 Aspose.PDF for Java API 的更多信息,了解如何使用 API。Aspose.PDF for Java API 的一些关键功能包括支持各种文件格式,包括 HTML、XFA、TXT、PCL、XML、XPS 和图像文件格式,支持各种 PDF 版本以及广泛的超链接功能。