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 的转换。

相关推荐
从零开始学习人工智能5 小时前
【踩坑实录】WSL2 解决 onnxruntime\-gpu ImportError: libcudart\.so\.13 无 CUDA13 运行库问题
python
嘻哈∠※5 小时前
0061基于 SpringBoot 的投稿与稿件处理系统设计与实现
java·spring boot·后端
卷无止境6 小时前
在 awesome-fastapi 里,哪些库值得一看?
后端·python
zhanghaha13146 小时前
Python进阶教程:6_JSON 数据解析 —— 新手完全指南
开发语言·python·json
Python私教6 小时前
API 输出模型怎么设计:从 model_dump() 到显式展示层
python·fastapi
白狐_7986 小时前
408数据结构第8章:排序②——性质对比秒杀、场景选择与外部排序
java·数据结构·算法
Python私教7 小时前
本地 AI 工具服务该绑定 127.0.0.1 还是 0.0.0.0?
python·fastapi
zander2587 小时前
LeetCode 84:柱状图中的最大矩形——单调栈如何确定左右边界
java·数据结构·算法
码匠许师傅7 小时前
【C++ 面试真题】聊聊 C++ 的拷贝构造与拷贝赋值
java·c++·面试
卷无止境7 小时前
FastAPI 的Admin面板生态
后端·python