在 Java 中将 PDF 转换为图像格式

Aspose.PDF for Java 可以将 PDF 页面渲染为栅格和矢量图像格式,并提供特定格式的设备选项。

将 PDF 转换为 BMP

当需要将 PDF 页面渲染为 BMP 图像时,请使用此示例。

  1. 使用 Document 实例打开源 PDF。
  2. 创建一个 BmpDevice,并将 Resolution 设置为 300 DPI。
  3. 遍历 document.getPages(),并对每页调用 device.process(...)。
  4. 将生成的 BMP 图像保存到编号的输出路径。
public static void convertPdfToBmp(Path inputFile, Path outputPrefix) {
       try (Document document = new Document(inputFile.toString())) {
           BmpDevice device = new BmpDevice(new Resolution(300));
           for (int page = 1; page <= document.getPages().size(); page++) {
               device.process(document.getPages().get_Item(page), numberedOutput(outputPrefix, page, "bmp"));
           }
       }
       System.out.println(inputFile + " converted into " + outputPrefix);
   }

将 PDF 转换为 EMF

当需要将 PDF 页面导出为 EMF 矢量图像时,请使用此示例。

  1. 使用 Document 实例打开源 PDF。
  2. 创建一个 EmfDevice,并将 Resolution 设置为 300 DPI。
  3. 遍历页面,并对每页调用 device.process(...)。
  4. 将 EMF 输出保存到编号的文件路径。
public static void convertPdfToEmf(Path inputFile, Path outputPrefix) {
    try (Document document = new Document(inputFile.toString())) {
        EmfDevice device = new EmfDevice(new Resolution(300));
        for (int page = 1; page <= document.getPages().size(); page++) {
            device.process(document.getPages().get_Item(page), numberedOutput(outputPrefix, page, "emf"));
        }
    }
    System.out.println(inputFile + " converted into " + outputPrefix);
}

将 PDF 转换为 GIF

当需要将 PDF 页面转换为 GIF 图像时,请使用此示例。

  1. 使用 Document 实例打开源 PDF。
  2. 创建一个 GifDevice,并将 Resolution 设置为 300 DPI。
  3. 遍历页面并调用 device.process(...) 渲染每页。
  4. 将 GIF 文件保存到编号的输出路径。
public static void convertPdfToGif(Path inputFile, Path outputPrefix) {
    try (Document document = new Document(inputFile.toString())) {
        GifDevice device = new GifDevice(new Resolution(300));
        for (int page = 1; page <= document.getPages().size(); page++) {
            device.process(document.getPages().get_Item(page), numberedOutput(outputPrefix, page, "gif"));
        }
    }
    System.out.println(inputFile + " converted into " + outputPrefix);
}

将 PDF 转换为 JPEG

当需要将 PDF 页面导出为 JPEG 图像时,请使用此示例。

  1. 使用 Document 实例打开源 PDF。
  2. 创建一个 JpegDevice,并将 Resolution 设置为 300 DPI。
  3. 遍历页面并调用 device.process(...) 将每页栅格化为 JPEG。
  4. 将 JPEG 输出文件保存到编号路径。
public static void convertPdfToJpeg(Path inputFile, Path outputPrefix) {
    try (Document document = new Document(inputFile.toString())) {
        JpegDevice device = new JpegDevice(new Resolution(300));
        for (int page = 1; page <= document.getPages().size(); page++) {
            device.process(document.getPages().get_Item(page), numberedOutput(outputPrefix, page, "jpeg"));
        }
    }
    System.out.println(inputFile + " converted into " + outputPrefix);
}

将 PDF 转换为 PNG

当需要将 PDF 页面转换为 PNG 图像时,请使用此示例。

  1. 使用 Document 实例打开源 PDF。
  2. 创建一个 PngDevice,并将 Resolution 设置为 300 DPI。
  3. 遍历页面,并对每页调用 device.process(...)。
  4. 将 PNG 输出保存到编号的文件路径。
public static void convertPdfToPng(Path inputFile, Path outputPrefix) {
    try (Document document = new Document(inputFile.toString())) {
        PngDevice device = new PngDevice(new Resolution(300));
        for (int page = 1; page <= document.getPages().size(); page++) {
            device.process(document.getPages().get_Item(page), numberedOutput(outputPrefix, page, "png"));
        }
    }
    System.out.println(inputFile + " converted into " + outputPrefix);
}

将 PDF 转换为 PNG,使用默认字体回退

在渲染时,如果缺少字形需要使用回退字体,请使用此示例。

  1. 使用 Document 实例打开源 PDF。
  2. 创建一个 PngDevice,并将 Resolution 设置为 300 DPI。
  3. 启用 document.setAbsentFontTryToSubstitute(true) 因此,在渲染时缺失的字形可以回退到替代字体。
  4. 渲染页面并保存 PNG 文件。
public static void convertPdfToPngWithDefaultFont(Path inputFile, Path outputPrefix) {
    try (Document document = new Document(inputFile.toString())) {
        PngDevice device = new PngDevice(new Resolution(300));
        document.setAbsentFontTryToSubstitute(true);
        for (int page = 1; page <= document.getPages().size(); page++) {
            device.process(document.getPages().get_Item(page), numberedOutput(outputPrefix, page, "png"));
        }
    }
    System.out.println(inputFile + " converted into " + outputPrefix);
}

将 PDF 转换为 SVG

当需要将 PDF 页面导出为 SVG 图形时,请使用此示例。

  1. 使用 Document 实例打开源 PDF。
  2. 创建 SvgSaveOptions 并在 raw 时禁用 ZIP 压缩 .svg 需要输出。
  3. 启用 setTreatTargetFileNameAsDirectory(true) 所以,每页的 SVG 输出可以组织在目标路径下。
  4. 保存 SVG 输出。
public static void convertPdfToSvg(Path inputFile, Path outputPrefix) {
    try (Document document = new Document(inputFile.toString())) {
        SvgSaveOptions saveOptions = new SvgSaveOptions();
        saveOptions.setCompressOutputToZipArchive(false);
        saveOptions.setTreatTargetFileNameAsDirectory(true);
        document.save(outputPrefix + ".svg", saveOptions);
    }
    System.out.println(inputFile + " converted into " + outputPrefix);
}

将 PDF 转换为 TIFF

当需要将一个或多个 PDF 页面导出为 TIFF 时,请使用此示例。

  1. 使用 Document 实例打开源 PDF。
  2. 创建 TiffSettings 并配置压缩、颜色深度和空白页行为。
  3. 创建一个 TiffDevice,使用分辨率为 300 DPI 的 Resolution 和已准备好的 TIFF 设置。
  4. 渲染页面并保存 TIFF 输出。
public static void convertPdfToTiff(Path inputFile, Path outputPrefix) {
    try (Document document = new Document(inputFile.toString())) {
        TiffSettings tiffSettings = new TiffSettings();
        tiffSettings.setCompression(CompressionType.LZW);
        tiffSettings.setDepth(ColorDepth.Default);
        tiffSettings.setSkipBlankPages(false);

        TiffDevice tiffDevice = new TiffDevice(new Resolution(300), tiffSettings);
        tiffDevice.process(document, outputPrefix + ".tiff");
    }
    System.out.println(inputFile + " converted into " + outputPrefix);
}