这个简单的主题展示了如何在 Java 中将 PDF 转换为 JPEG。它涵盖了设置环境的所有步骤,并使用示例代码来完成任务。为了将 PDF 转换为 JPEG Java,提供了基于 Java 的代码以及输出 JPEG 图像的自定义。
在 Java 中将 PDF 转换为 Jpeg 的步骤
- 使用存储库管理器建立项目环境以将 Aspose.PDF for Java 添加到应用程序中
- 使用 Document 类的实例加载示例 PDF 文件以转换为 JPEG 图像
- 使用 Resolution 类设置输出 JPEG 图像的图像分辨率
- 使用指定的分辨率和可能的其他设置创建和设置 JpegDevice 对象
- 使用 JpegDevice.process 方法将加载的 PDF 文件页面保存为相应的 JPEG 图像
上面提到的详细步骤涵盖了使用 Java 将 PDF 转换为 JPEG* 所需的必要 JAR 文件的信息,以及实施的一系列完整步骤。人们可以轻松地从磁盘加载源 PDF 文件,并毫不费力地将其所有或所需页面转换为单独的 JPEG 文件。 JpegDevice 类实例设置输出 JPEG 分辨率,并有一个处理方法将选定的 PDF 页面转换为 JPEG 图像。
使用 Java 将 PDF 转换为 Jpeg 的代码
package testpdf; | |
import com.aspose.pdf.Document; | |
import com.aspose.pdf.License; | |
import com.aspose.pdf.devices.JpegDevice; | |
import com.aspose.pdf.devices.Resolution; | |
public class PdfToJpeg { | |
static void main(String[] args) throws Exception // Exception for PDF to JPEG Conversion | |
{ | |
String path= "/Users/KnowledgeBase/TestData/"; | |
// Instantiate a license to convert PDF to JPEG | |
License PdfToTiffLicense = new License(); | |
PdfToTiffLicense.setLicense(path+"Aspose.Total.lic"); | |
// Initialize Document Class to load PDF and save as JPG Image | |
Document document = new Document(path+"Exported.pdf"); | |
// Setting the JPEG device for rendering | |
Resolution resolution = new Resolution(300); | |
JpegDevice jpegDevice = new JpegDevice(resolution); | |
//Loop through all of the PDF document pages | |
for (int pageNumber = 1; pageNumber <= document.getPages().size(); pageNumber++) | |
{ | |
// Initialize the output stream object to save the image | |
java.io.OutputStream outputBinImageFile = new java.io.FileOutputStream( | |
"image" + pageNumber + "_out.jpg"); | |
// Save each page as a separate image | |
jpegDevice.process(document.getPages().get_Item(pageNumber), outputBinImageFile); | |
// Close the output stream after saving the Jpeg image | |
outputBinImageFile.close(); | |
} | |
System.out.println("Done"); | |
} | |
} |
为了将 PDF 转换为 JPEG Java 代码,该过程通过从磁盘加载源 PDF 文件开始。然后为输出 JPEG 图像设置所需的分辨率,并将其传递给 JpegDevice 类的实例。 PDF 文档页面逐页迭代,PDF 中的每一页都根据设置的图像分辨率作为单独的 JPEG 图像保存在磁盘上。
在本文中,我们见证了如何使用 Java 将 PDF 转换为 JPEG,但是,如果您想了解如何将 PDF 转换为 SVG,请参阅 如何使用 Java 将 PDF 转换为 SVG 上的文章。