PDF处理控件Aspose.PDF功能演示:使用Java将PDF文件转换为PNG等图像格式

  • 使用Java将PDF文件转换为PNG
  • 使用Java将PDF文件转换为JPEG
  • 使用Java将PDF转换为BMP
  • 使用Java将PDF转换为TIFF

整合所有格式的Aspose.Total永久授权正在火热促销中,联系客服立马1分钟了解全部咨询!


使用Java将PDF文件转换为PNG

在PDF到PNG的转换中,PDF文件的每一页都转换为单独的PNG图像。因此,可以根据情况转换PDF的单个页面,所有页面或特定页面。以下是使用Java将PDF文件转换为PNG的步骤。

  • 使用Document类加载PDF文件。
  • 使用Document.getPages()方法循环浏览PDF文件中的页面。
  • 为每个PNG图像创建OutputStream对象。
  • 实例化Resolution类以设置渲染图像的分辨率。
  • 创建一个PngDevice类的对象,并使用Resolution对象对其进行初始化。
  • 使用PngDevice.process(Document.getPages()。get_Item(Index),OutputStream)方法将PDF页面转换为PNG图像。
  • 关闭文件流。

以下代码示例显示了如何使用Java将PDF转换为PNG。

// Open documentDocument pdfDocument = new Document("input.pdf");// Loop through all the pages of PDF filefor (int pageCount = 1; pageCount <= pdfDocument.getPages().size(); pageCount++) { // Create stream object to save the output image java.io.OutputStream imageStream = new java.io.FileOutputStream("Converted_Image" + pageCount + ".png"); // Create Resolution object Resolution resolution = new Resolution(300); // Create PngDevice object with particular resolution PngDevice pngDevice = new PngDevice(resolution); // Convert a particular page and save the image to stream pngDevice.process(pdfDocument.getPages().get_Item(pageCount), imageStream); // Close the stream imageStream.close(); }

使用Java将PDF文件转换为JPEG

以下是使用Java将PDF文件中的页面转换为JPEG图像的步骤。

  • 使用Document类加载PDF文件。
  • 使用Document.getPages()方法循环浏览PDF页面。
  • 为每个JPEG图像创建OutputStream对象。
  • 实例化Resolution类以设置渲染图像的分辨率。
  • 创建JpegDevice类的对象,然后使用Resolution对象对其进行初始化。
  • 使用JpegDevice.process(Document.getPages()。get_Item(Index),OutputStream)方法将PDF页面转换为JPEG图像。
  • 关闭文件流。

以下代码示例显示了如何使用Java将PDF页面转换为JPEG图像。

// Open document Document pdfDocument = new  Document("input.pdf");// Loop through all the pages of PDF filefor (int pageCount = 1; pageCount <= pdfDocument.getPages().size(); pageCount++) { // Create stream object to save the output image java.io.OutputStream imageStream = new java.io.FileOutputStream("Converted_Image" + pageCount + ".jpg"); // Create Resolution object Resolution resolution = new Resolution(300); // Create JpegDevice object where second argument indicates the quality of resultant image JpegDevice jpegDevice = new JpegDevice(resolution, 100); // Convert a particular page and save the image to stream jpegDevice.process(pdfDocument.getPages().get_Item(pageCount), imageStream); // Close the stream imageStream.close(); }

使用Java将PDF转换为BMP

以下是在Java中执行PDF到BMP转换的步骤。

  • 使用Document类加载PDF文件。
  • 使用Document.getPages()方法遍历PDF页面。
  • 为每个BMP图像创建OutputStream对象。
  • 实例化Resolution类以设置渲染图像的分辨率。
  • 创建一个BmpDevice类的对象,并使用Resolution对象对其进行初始化。
  • 使用BmpDevice.process(Document.getPages()。get_Item(Index),OutputStream)方法将PDF页面转换为BMP图像。
  • 关闭文件流。

以下代码示例显示了如何在Java中执行PDF到BMP的转换。

// Open documentDocument pdfDocument = new Document("input.pdf");// Loop through all the pages of PDF filefor (int pageCount = 1; pageCount <= pdfDocument.getPages().size(); pageCount++) { // Create stream object to save the output image java.io.OutputStream imageStream = new java.io.FileOutputStream("Converted_Image" + pageCount + ".bmp"); // Create Resolution object Resolution resolution = new Resolution(300); // Create BmpDevice object with particular resolution BmpDevice bmpDevice = new BmpDevice(resolution); // Convert a particular page and save the image to stream bmpDevice.process(pdfDocument.getPages().get_Item(pageCount), imageStream); // Close the stream imageStream.close(); }

使用Java将PDF转换为TIFF

与上述光栅图像格式相反,TIFF是多页图像格式。因此,可以一次将PDF文件转换为TIFF,而无需循环浏览每个页面。另一方面,您也可以指定要转换为TIFF的PDF页面范围。以下是使用Java将PDF文件转换为TIFF的步骤。

  • 使用Document类加载PDF文件。
  • 为TIFF图像创建OutputStream对象。
  • 实例化Resolution类以设置渲染图像的分辨率。
  • 使用TiffSettings类设置其他选项,例如压缩类型,颜色深度等。
  • 创建一个TiffDevice类的对象,并使用Resolution对象对其进行初始化。
  • 使用TiffDevice.process(Document,OutputStream)方法(或其他重载方法指定页面范围)将PDF转换为TIFF。
  • 关闭文件流。

以下代码示例显示了如何使用Java将PDF文件转换为TIFF图像。

// Open documentDocument pdfDocument = new Document("input.pdf");// Create stream object to save the output imagejava.io.OutputStream imageStream = new java.io.FileOutputStream("Converted_Image.tiff");// Create Resolution objectResolution resolution = new Resolution(300);// instantiate TiffSettings objectTiffSettings tiffSettings = new TiffSettings();// set the compression of resultant TIFF imagetiffSettings.setCompression(CompressionType.CCITT4);// set the color depth for resultant imagetiffSettings.setDepth(ColorDepth.Format8bpp);// skip blank pages while rendering PDF to TIFFtiffSettings.setSkipBlankPages(true);// Create TiffDevice object with particular resolutionTiffDevice tiffDevice = new TiffDevice(resolution, tiffSettings);// Convert a particular page (Page 1) and save the image to streamtiffDevice.process(pdfDocument, 1, 1, imageStream);// Close the streamimageStream.close();

如果你想试用Aspose的全部完整功能,可联系在线客服获取30天临时授权体验。


还想要更多吗可以点击阅读【2020 · Aspose最新资源整合】查找需要的教程资源。如果您有任何疑问或需求,请随时加入Aspose技术交流群(),我们很高兴为您提供查询和咨询
标签:

声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2021年1月16日
下一篇 2021年1月16日

相关推荐

发表回复

登录后才能评论