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
核心功能:
-
OFD 文档生成、数字签名、文档保护
-
文档合并、转换、导出等功能
-
完全遵循《GB/T 33190-2016》国家标准
-
Apache 2.0 许可证(可免费商用) easeconvert
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 的转换。