当前位置: 首页 > news >正文

常见文件格式转国产ofd案例,支持pdf、word、txt;

importcom.spire.doc.Document;importcom.spire.doc.FileFormat;importcom.spire.pdf.PdfDocument;importjava.io.*;public class DesktopConverter{public static void main(String[]args){//1. 配置路径 String sourceFolderPath="C:\\Users\\qinqin\\Desktop";String outputFolderPath="C:\\Users\\qinqin\\Desktop\\ofd_output";//2. 创建输出目录 File outputDir=new File(outputFolderPath);if(!outputDir.exists()){outputDir.mkdirs();System.out.println("已创建输出目录: "+ outputFolderPath);}//3. 扫描文件 File sourceDir=new File(sourceFolderPath);File[]files=sourceDir.listFiles();if(files==null||files.length==0){System.out.println("目录下没有文件!");return;}System.out.println("=== 开始批量转换 ===");for(Filefile:files){if(file.isDirectory())continue;// 跳过文件夹 String fileName=file.getName();String lowerName=fileName.toLowerCase();String srcPath=file.getAbsolutePath();// 生成输出文件名(例如:1.doc ->1_doc.ofd)String outName=fileName.replace(".","_")+".ofd";String destPath=outputFolderPath + File.separator + outName;try{if(lowerName.endsWith(".doc")||lowerName.endsWith(".docx")){System.out.print("正在转换 Word: "+ fileName +" ... ");convertWordToOfd(srcPath, destPath);System.out.println("✅ 成功");}elseif(lowerName.endsWith(".txt")){System.out.print("正在转换 Txt: "+ fileName +" ... ");convertTxtToOfd(srcPath, destPath);System.out.println("✅ 成功");}elseif(lowerName.endsWith(".pdf")){System.out.print("正在转换 PDF: "+ fileName +" ... ");convertPdfToOfd(srcPath, destPath);System.out.println("✅ 成功");}}catch(Exception e){System.out.println("❌ 失败 ("+ e.getMessage()+")");e.printStackTrace();}}System.out.println("=== 全部完成,请查看文件夹: "+ outputFolderPath +" ===");}// --- 核心转换方法 --- /** * Word/Docx 转 OFD */ public static void convertWordToOfd(String srcPath, String destPath)throws Exception{Document doc=new Document();doc.loadFromFile(srcPath);// Word 转 PDF 流 ByteArrayOutputStream pdfStream=new ByteArrayOutputStream();doc.saveToStream(pdfStream, FileFormat.PDF);// PDF 流转 OFD streamToOfd(new ByteArrayInputStream(pdfStream.toByteArray()), destPath);}/** * Txt 转 OFD */ public static void convertTxtToOfd(String srcPath, String destPath)throws Exception{Document doc=new Document();// 显式指定加载格式为 Text doc.loadFromFile(srcPath, FileFormat.Txt);ByteArrayOutputStream pdfStream=new ByteArrayOutputStream();doc.saveToStream(pdfStream, FileFormat.PDF);streamToOfd(new ByteArrayInputStream(pdfStream.toByteArray()), destPath);}/** * PDF 转 OFD */ public static void convertPdfToOfd(String srcPath, String destPath){PdfDocument pdf=new PdfDocument();pdf.loadFromFile(srcPath);pdf.saveToFile(destPath, com.spire.pdf.FileFormat.OFD);pdf.close();}/** * 通用:内存PDF流 ->OFD文件 */ private static void streamToOfd(InputStream inputStream, String destPath){PdfDocument pdf=new PdfDocument();pdf.loadFromStream(inputStream);pdf.saveToFile(destPath, com.spire.pdf.FileFormat.OFD);pdf.close();}}
http://www.rkmt.cn/news/158386.html

相关文章:

  • python智能停车场车位租赁管理系统vue
  • 【JPCS出版 | EI检索】第七届新材料与清洁能源国际学术会议(ICAMCE 2026)
  • 2025年口碑好的美甲培训学校推荐,专业美甲课程与就业支持全解析 - 工业推荐榜
  • Ryuko-NEHT Reloaded! MAME 0.116 Hack合集
  • Forest项目MySQL数据库迁移指南
  • 论文AI率过高怎么降?3款免费工具亲测有效,附详细避坑指南
  • 成都GEO优化新浪潮:当企业搜索流量被AI重塑,谁能成为领航者? - 品牌企业推荐师(官方)
  • 2025最新智能安检门/X光安检机/升降柱厂家TOP10推荐:场景适配与合规实力双优榜单 - 深度智识库
  • 使用wechatDownload批量保存公众号文章
  • 得物Java面试被问:Fork/Join框架的使用场景
  • Codev材料库
  • “函数指针”和“指针函数”区分清楚
  • Open-AutoGLM手机端首发实测:大模型离线运行真的可行了吗?
  • 别折腾了!2026年降AI只有这3条路能走通(附手动+工具实操)
  • 开源新星Open-AutoGLM:从源码到部署的完整实战指南(含内部架构图)
  • Python爬虫破解JS混淆数据加密实战
  • 十大排序算法原理与多语言实现
  • 小折叠屏手机如何平衡便携与体验
  • 基于SpringAI的智能平台基座开发-(十一)
  • python古诗词鉴赏在线学习系统_3krsp-vue
  • 国产自研大模型引擎崛起,Open-AutoGLM官网透露的3个战略信号
  • 【Open-AutoGLM数据安全深度剖析】:揭秘AI大模型潜在风险与防护策略
  • java实现Apache POI提取word文档标题(类目录)
  • NMN牌子哪个好?2026十大NMN品牌排名与权威口碑对比 - 速递信息
  • 【Open-AutoGLM使用全攻略】:从零入门到实战精通的5大核心步骤
  • 告别人工干预!Open-AutoGLM让大模型真正“自己动起来”
  • MAME 0.116 Ryuko-NEHT Reloaded 游戏列表与ROM信息
  • Delphi 窗体间通信的经典解决方案,掌握后可以应对80%的窗体数据传递需求
  • 学长亲荐8个AI论文工具,专科生毕业论文格式规范必备!
  • 【智谱清言使用全攻略】:3步开启Open-AutoGLM沉思模式,90%的人都不知道的隐藏功能