Microsoft Publisher Files 用于打印或发布信息。您可以使用 Java 以编程方式将 PUB) 转换为 JPG、PNG 或 TIFF 图像。在本文中,您将学习如何将 PUB 文件转换为不同的图像格式:
- PUB 到图像的转换 – Java API 安装
- 使用 Java 以编程方式将 PUB 转换为 JPG 图像
- 使用 Java 以编程方式将 PUB 转换为 PNG 图像
- 在 Java 中以编程方式将 PUB 转换为 TIFF
PUB 到图像的转换 – Java API 安装
Aspose.PUB for Java API 支持处理 PUB 文件。您可以将 PUB 文件渲染为 PDF,然后利用 Aspose.PDF for Java API 将输出的 PDF 文件转换为 JPG、PNG、TIFF 等图像格式。您可以下载 Aspose.PUB 的 JAR 文件用于 Java 和 Aspose.PDF 用于 Java API 或指定以下配置以从 Maven 存储库 下载 API:
存储库:
<repositories>
<repository>
<id>AsposeJavaAPI</id>
<name>Aspose Java API</name>
<url>http://repository.aspose.com/repo/</url>
</repository>
</repositories>
依赖:
<dependencies>
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-pub</artifactId>
<version>20.8</version>
</dependency>
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-pdf</artifactId>
<version>20.5</version>
</dependency>
</dependencies>
使用 Java 以编程方式将 PUB 转换为 JPG 图像
您可以通过以下步骤将 Microsoft Publisher (PUB) 文件转换为 JPG 图像:
- 加载输入 PUB 文件。
- 使用 convertToPdf 方法将 PUB 转换为 PDF 文件。
- 从 PDF 文档中获取页面尺寸。
- 指定输出图像的分辨率和尺寸。
- 使用 JpegDevice 保存输出的 JPG 图像。
下面的代码解释了如何使用 Java 将 PUB 转换为 JPG 图像:
// 加载输入 PUB 文件
IPubParser parser = PubFactory.createParser("Test.pub");
Document doc = parser.parse();
// 将 PUB 转换为 PDF 文件
PubFactory.createPdfConverter().convertToPdf(doc, "Test.pdf");
// 加载 PDF 文件
Document document = new Document("Test.pdf");
facades.PdfFileInfo info = new facades.PdfFileInfo(document);
for (Page page : document.getPages())
{
// 从 PDF 文档中获取页面尺寸
int width = (int) info.getPageWidth(page.getNumber());
int height = (int) (info.getPageHeight(page.getNumber()));
// 设置输出图像的分辨率
devices.Resolution resolution = new devices.Resolution(300);
// 创建具有指定宽度和高度的 JPEG 设备
devices.JpegDevice jpegDevice = new devices.JpegDevice(width, height , resolution);
// 将 PUB 转换为 JPG 图像
jpegDevice.process(page, "Page" + page.getNumber() + ".jpg");
}
使用 Java 以编程方式将 PUB 转换为 PNG 图像
您可以通过以下步骤将 PUB 文件转换为 PNG 格式的图像:
- 使用 createParser() 方法加载输入 PUB 文件。
- 将 PUB 转换为 PDF 文件。
- 指定输出图像的宽度和高度。
- 最后,将 PUB 转换为 PNG 图像。
以下代码显示了如何使用 Java 将 PUB 转换为 PNG 图像:
// 加载输入 PUB 文件
IPubParser parser = PubFactory.createParser("Test.pub");
Document doc = parser.parse();
// 将 PUB 转换为 PDF 文件
PubFactory.createPdfConverter().convertToPdf(doc, "Test.pdf");
Document document = new Document("Test.pdf");
facades.PdfFileInfo info = new facades.PdfFileInfo(document);
for (Page page : document.getPages())
{
// 从 PDF 文档中获取页面尺寸
int width = (int) info.getPageWidth(page.getNumber());
int height = (int) (info.getPageHeight(page.getNumber()));
// 创建具有指定宽度和高度的 PNG 设备
devices.PngDevice pngDevice = new devices.PngDevice(width, height);
// 将 PUB 转换为 PNG 图像
pngDevice.process(page, "Page" + page.getNumber() + ".png");
}
在 Java 中以编程方式将 PUB 转换为 TIFF
您可以使用下列步骤将 PUB 文件转换为 TIFF 图像:
- 首先,加载输入发布者 (PUB) 文件。
- 使用 convertToPdf() 方法将 PUB 转换为 PDF 文件。
- 使用 TiffSettings 类指定 TIFF 图像的属性。
- 最后,使用 TiffDevice 实例将 PUB 转换为 TIFF 图像。
以下代码演示了如何使用 Java 以编程方式将 PUB 转换为 TIFF 图像:
// 加载输入 PUB 文件
IPubParser parser = PubFactory.createParser("Test.pub");
Document doc = parser.parse();
// 将 PUB 转换为 PDF 文件
PubFactory.createPdfConverter().convertToPdf(doc, "Test.pdf");
Document document = new Document("Test.pdf");
facades.PdfFileInfo info = new facades.PdfFileInfo(document);
// 从 PDF 文档中获取页面尺寸
int width = (int) info.getPageWidth((int)(1));
int height = (int) info.getPageHeight((int)(1));
devices.Resolution resolution = new devices.Resolution(300);
devices.TiffSettings settings = new devices.TiffSettings();
settings.setCompression(devices.CompressionType.None);
settings.setDepth(devices.ColorDepth.Default);
// 创建具有指定宽度和高度的 TIFF 设备
devices.TiffDevice tiffDevice = new devices.TiffDevice(width, height , resolution, settings);
// 将 PUB 转换为 TIFF 图像
tiffDevice.process(document, "Output.tiff");
获取免费 API 许可证
您可以通过申请 免费临时许可证 来评估 API 的全部功能。
结论
总之,您已经学习了如何使用 Java 以编程方式将 Microsoft Publisher、PUB、文件转换为不同的图像格式,如 JPG、PNG、TIFF 等。此外,您可以通过访问 Aspose.PUB for Java API 的 文档 了解更多功能。此外,如有任何疑问,请随时通过 免费支持论坛 与我们联系。