如何在 Java 中实现 PDF 与 TIFF 格式互转

142 阅读2分钟

核心概念解析

PDF与TIFF虽同为文档类格式,但在底层实现上存在本质差异:

  • ​TIFF​​:基于光栅图像(像素级存储),擅长医学影像、工程图纸等高精度场景
  • ​PDF​​:复合文档容器(支持文本、矢量图、图像混合),具备跨平台一致性优势
  • ​转换本质​​:并非简单修改后缀,而是需要完成内容编码方式的彻底转换

典型应用场景

  1. ​PDF转TIFF​

    • 合同扫描件归档
    • 表单自动化识别
    • 多页文档分帧处理
  2. ​TIFF转PDF​

    • 图片报告电子化
    • 长期档案数字化
    • 跨平台文档共享

开源实现方案

​1. PDF转TIFF(PDFBox + JAI)​

// 核心依赖(Maven)
<dependency>
    <groupId>org.apache.pdfbox</groupId>
    <artifactId>pdfbox</artifactId>
    <version>3.0.0</version>
</dependency>
<dependency>
    <groupId>javax.media.jai</groupId>
    <artifactId>jai-core</artifactId>
    <version>1.1.3</version>
</dependency>

// 关键代码实现
public class Pdf2TiffConverter {
    public static void convert(String pdfPath, String tiffPath) throws IOException {
        PDDocument document = PDDocument.load(new File(pdfPath));
        PDFRenderer renderer = new PDFRenderer(document);
        
        TIFFEncodeParam params = new TIFFEncodeParam();
        params.setCompression(TIFFEncodeParam.COMPRESSION_GROUP4);
        params.setLittleEndian(false);
        
        ImageEncoder encoder = new TIFFImageEncoder(new File(tiffPath), params);
        for (int i = 0; i < document.getNumberOfPages(); i++) {
            BufferedImage pageImage = renderer.renderImageWithDPI(i, 300);
            encoder.encode(pageImage);
        }
        document.close();
    }
}

​2. TIFF转PDF(iText)​

// 核心依赖(Maven)
<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itextpdf</artifactId>
    <version>5.5.13.3</version>
</dependency>

// 关键代码实现
public class Tiff2PdfConverter {
    public static void convert(String tiffPath, String pdfPath) throws IOException {
        RandomAccessFileOrArray ra = new RandomAccessFileOrArray(tiffPath);
        int pageCount = TiffImage.getNumberOfPages(ra);
        
        Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream(pdfPath));
        document.open();
        
        for (int i = 1; i <= pageCount; i++) {
            Image image = TiffImage.getTiffImage(ra, i);
            image.scaleToFit(document.getPageSize().getWidth(), document.getPageSize().getHeight());
            document.add(image);
        }
        document.close();
    }
}

云服务API方案

​1. PDF转TIFF(Cloudmersive API)​

// Maven依赖配置
<repositories>
    <repository>
        <id>jitpack.io</id>
        <url>https://jitpack.io</url>
    </repository>
</repositories>
<dependency>
    <groupId>com.github.Cloudmersive</groupId>
    <artifactId>Cloudmersive.APIClient.Java</artifactId>
    <version>v4.25</version>
</dependency>

// API调用示例
public class PdfToTiffApi {
    public static void main(String[] args) {
        ApiClient client = Configuration.getDefaultApiClient();
        client.setApiKey("YOUR_API_KEY");
        
        ConvertApi apiInstance = new ConvertApi();
        File inputFile = new File("contract.pdf");
        
        try {
            byte[] result = apiInstance.convertToTiff(inputFile);
            Files.write(Paths.get("output.tiff"), result);
            System.out.println("转换成功!");
        } catch (ApiException | IOException e) {
            e.printStackTrace();
        }
    }
}

​2. TIFF转PDF(Cloudmersive API)​

// Maven依赖同上
public class TiffToPdfApi {
    public static void main(String[] args) {
        ApiClient client = Configuration.getDefaultApiClient();
        client.setApiKey("YOUR_API_KEY");
        
        ConvertDocumentApi apiInstance = new ConvertDocumentApi();
        File inputFile = new File("scan.tiff");
        
        try {
            byte[] result = apiInstance.convertDocumentAutodetectToPdf(inputFile);
            Files.write(Paths.get("output.pdf"), result);
            System.out.println("转换完成!");
        } catch (ApiException | IOException e) {
            e.printStackTrace();
        }
    }
}

技术对比

维度开源方案云服务API
成本无授权费用按API调用计费
维护复杂度需自行处理异常场景全托管服务
功能扩展性可深度定制依赖服务商能力
性能表现受本地资源限制云端分布式处理
典型适用场景企业内部系统SaaS化集成

最佳实践建议

  1. ​生产环境选型​

    • 小规模转换 → 开源库(PDFBox + iText)
    • 高并发场景 → 云服务API(Cloudmersive/Aspose)
    • 医疗影像 → 专用DICOM转码方案
  2. ​性能优化要点​

    • 使用多线程处理批量转换
    • 设置合理的DPI参数(200-600)
    • 开启压缩算法(LZW/JPEG 2000)
  3. ​错误处理机制​

    try {
        // 转换操作
    } catch (FileNotFoundException e) {
        logger.error("输入文件不存在");
    } catch (IOException e) {
        logger.error("I/O操作异常", e);
    } catch (ApiException e) {
        logger.error("API调用失败: {}", e.getResponseBody());
    }
    

通过合理选择技术方案,可以在保证转换质量的同时,显著提升企业文档处理效率。建议根据具体需求在成本、可控性和功能性之间做出平衡。