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

相关推荐
weixin_49977155几秒前
Python上下文管理器(with语句)的原理与实践
jvm·数据库·python
weixin_452159553 分钟前
高级爬虫技巧:处理JavaScript渲染(Selenium)
jvm·数据库·python
多米Domi0119 分钟前
0x3f 第48天 面向实习的八股背诵第五天 + 堆一题 背了JUC的题,java.util.Concurrency
开发语言·数据结构·python·算法·leetcode·面试
深蓝海拓15 分钟前
PySide6从0开始学习的笔记(二十六) 重写Qt窗口对象的事件(QEvent)处理方法
笔记·python·qt·学习·pyqt
纠结哥_Shrek15 分钟前
外贸选品工程师的工作流程和方法论
python·机器学习
小汤圆不甜不要钱17 分钟前
「Datawhale」RAG技术全栈指南 Task 5
python·llm·rag
今天_也很困35 分钟前
LeetCode热题100-560. 和为 K 的子数组
java·算法·leetcode
在繁华处1 小时前
线程进阶: 无人机自动防空平台开发教程V2
java·无人机
A懿轩A1 小时前
【Java 基础编程】Java 变量与八大基本数据类型详解:从声明到类型转换,零基础也能看懂
java·开发语言·python