pdf转国产ofd格式代码案例-Java

pdf转国产ofd格式代码案例-Java

mvn坐标

复制代码
        <dependency>
            <groupId>org.ofdrw</groupId>
            <artifactId>ofdrw-full</artifactId>
            <version>2.3.7</version>
        </dependency>

代码

复制代码
import org.ofdrw.converter.ofdconverter.PDFConverter;
import org.ofdrw.reader.OFDReader;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
 * PDF 转 OFD 示例
 * 使用 OFDRW 2.3.7 实现
 *
 * Maven 依赖:
 * <dependency>
 *     <groupId>org.ofdrw</groupId>
 *     <artifactId>ofdrw-full</artifactId>
 *     <version>2.3.7</version>
 * </dependency>
 */
public class PdfToOfdConverter {

    public static void main(String[] args) {
        // 输入输出文件路径
        String pdfPath = "1.pdf";
        String ofdPath = "1.ofd";

        printHeader();

        try {
            // 1. 检查输入文件
            if (!checkInputFile(pdfPath)) {
                return;
            }

            // 2. 执行转换
            convertPdfToOfd(pdfPath, ofdPath);

            // 3. 验证输出文件
            verifyOfdFile(ofdPath);

            // 4. 比较文件大小
            compareFileSize(pdfPath, ofdPath);

            System.out.println("\n" + repeatChar('=', 50));
            System.out.println("✓ 转换成功!所有检查通过!");
            System.out.println(repeatChar('=', 50));

        } catch (Exception e) {
            System.err.println("\n" + repeatChar('=', 50));
            System.err.println("✗ 转换失败!");
            System.err.println(repeatChar('=', 50));
            System.err.println("错误详情:");
            e.printStackTrace();
        }
    }

    /**
     * 打印标题
     */
    private static void printHeader() {
        System.out.println("\n" + repeatChar('=', 50));
        System.out.println("       PDF 转 OFD 转换工具 (OFDRW)");
        System.out.println(repeatChar('=', 50));
    }

    /**
     * 检查输入文件是否存在
     */
    private static boolean checkInputFile(String pdfPath) {
        System.out.println("\n[1] 检查输入文件...");
        File pdfFile = new File(pdfPath);

        if (!pdfFile.exists()) {
            System.err.println("✗ 错误: 找不到文件 '" + pdfPath + "'");
            System.err.println("当前目录: " + System.getProperty("user.dir"));
            return false;
        }

        long fileSize = pdfFile.length();
        System.out.println("✓ 输入文件: " + pdfPath);
        System.out.println("  文件大小: " + formatFileSize(fileSize));
        return true;
    }

    /**
     * 执行 PDF 转 OFD - 核心方法
     */
    private static void convertPdfToOfd(String pdfPath, String ofdPath) throws IOException {
        System.out.println("\n[2] 开始转换...");
        System.out.println("  输入: " + pdfPath);
        System.out.println("  输出: " + ofdPath);

        Path pdfFile = Paths.get(pdfPath);
        Path ofdFile = Paths.get(ofdPath);

        // 记录开始时间
        long startTime = System.currentTimeMillis();

        // ⭐ 核心转换代码 - 使用 PDFConverter
        try (PDFConverter converter = new PDFConverter(ofdFile)) {
            // PDF 转 OFD
            converter.convert(pdfFile);
        }

        // 计算耗时
        long endTime = System.currentTimeMillis();
        double seconds = (endTime - startTime) / 1000.0;

        System.out.println("✓ 转换完成!");
        System.out.println("  耗时: " + String.format("%.2f", seconds) + " 秒");
    }

    /**
     * 验证输出的 OFD 文件
     */
    private static void verifyOfdFile(String ofdPath) {
        System.out.println("\n[3] 验证输出文件...");

        File ofdFile = new File(ofdPath);
        if (!ofdFile.exists()) {
            System.err.println("✗ OFD 文件未生成!");
            return;
        }

        System.out.println("✓ OFD 文件已生成: " + ofdPath);
        System.out.println("  文件大小: " + formatFileSize(ofdFile.length()));

        // 尝试读取 OFD 文件验证其正确性
        try (OFDReader reader = new OFDReader(Paths.get(ofdPath))) {
            int pageCount = reader.getNumberOfPages();
            System.out.println("  页数: " + pageCount);
            System.out.println("✓ OFD 文件结构正确,可以正常读取!");
        } catch (IOException e) {
            System.err.println("✗ OFD 文件读取失败: " + e.getMessage());
        }
    }

    /**
     * 比较文件大小
     */
    private static void compareFileSize(String pdfPath, String ofdPath) {
        System.out.println("\n[4] 文件大小对比...");

        File pdfFile = new File(pdfPath);
        File ofdFile = new File(ofdPath);

        long pdfSize = pdfFile.length();
        long ofdSize = ofdFile.length();

        System.out.println("  PDF 大小:  " + formatFileSize(pdfSize));
        System.out.println("  OFD 大小:  " + formatFileSize(ofdSize));

        double ratio = (double) ofdSize / pdfSize * 100;
        System.out.println("  OFD/PDF: " + String.format("%.2f", ratio) + "%");

        if (ofdSize < pdfSize) {
            long saved = pdfSize - ofdSize;
            System.out.println("  节省空间: " + formatFileSize(saved));
        } else {
            long increased = ofdSize - pdfSize;
            System.out.println("  增加空间: " + formatFileSize(increased));
        }
    }

    /**
     * 格式化文件大小
     */
    private static String formatFileSize(long bytes) {
        if (bytes < 1024) {
            return bytes + " B";
        } else if (bytes < 1024 * 1024) {
            return String.format("%.2f KB", bytes / 1024.0);
        } else {
            return String.format("%.2f MB", bytes / 1024.0 / 1024.0);
        }
    }

    /**
     * 重复字符 (兼容 Java 8)
     */
    private static String repeatChar(char ch, int count) {
        StringBuilder sb = new StringBuilder(count);
        for (int i = 0; i < count; i++) {
            sb.append(ch);
        }
        return sb.toString();
    }
}

🎉 主流开源 OFD 库

1️⃣ OFDRW(最推荐 - Java)

项目地址https://github.com/ofdrw/ofdrw

核心功能

2️⃣ XiaoFeng.Ofd(C#/.NET)

项目地址https://github.com/zhuovi/XiaoFeng.Ofd

核心功能

  • OFD 文档生成、编辑、批注
  • 数字签名、文档合并、拆分
  • OFD 转 PDF
  • 文档查询功能
  • 符合《GB/T 33190-2016》标准 zhihu

3️⃣ ofd.js(JavaScript/前端)

功能 :轻量级前端解析和渲染 OFD 文件的 SDK wondershare

适合在网页上预览 OFD 文档。

4️⃣ OFDConverter(PDF 转 OFD)

项目地址 :简易的 PDF 转 OFD 格式文档工具 foxitsoftware

专门用于 PDF 到 OFD 的转换。

相关推荐
忧郁的Mr.Li17 小时前
SpringBoot中实现多数据源配置
java·spring boot·后端
玄同76518 小时前
从 0 到 1:用 Python 开发 MCP 工具,让 AI 智能体拥有 “超能力”
开发语言·人工智能·python·agent·ai编程·mcp·trae
yq19820430115618 小时前
静思书屋:基于Java Web技术栈构建高性能图书信息平台实践
java·开发语言·前端
一个public的class18 小时前
你在浏览器输入一个网址,到底发生了什么?
java·开发语言·javascript
有位神秘人18 小时前
kotlin与Java中的单例模式总结
java·单例模式·kotlin
golang学习记18 小时前
IntelliJ IDEA 2025.3 重磅发布:K2 模式全面接管 Kotlin —— 告别 K1,性能飙升 40%!
java·kotlin·intellij-idea
小瑞瑞acd18 小时前
【小瑞瑞精讲】卷积神经网络(CNN):从入门到精通,计算机如何“看”懂世界?
人工智能·python·深度学习·神经网络·机器学习
爬山算法18 小时前
Hibernate(89)如何在压力测试中使用Hibernate?
java·压力测试·hibernate
开开心心就好18 小时前
发票合并打印工具,多页布局设置实时预览
linux·运维·服务器·windows·pdf·harmonyos·1024程序员节
火车叼位18 小时前
也许你不需要创建.venv, 此规范使python脚本自备依赖
python