当前位置: 首页 > 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.jsqmd.com/news/144126/

相关文章:

  • 激光打孔机选购指南:研发实力、维护成本与企业选择 - myqiye
  • Open-AutoGLM使用避坑指南(9大常见错误及解决方案)
  • 【Php期末大作业带数据库】Php+MySQL电商商品展示平台设计与实现、电子购物商城系统(附源码)
  • Open-AutoGLM部署成功率提升80%,这7个关键参数设置你调对了吗
  • DC综合与静态时序分析优化实战
  • 【JPCS出版 | EI检索】第七届新材料与清洁能源国际学术会议(ICAMCE 2026)
  • 《智能体入门课》第一课|从 ChatGPT 到智能体:为什么现在人人都在谈「Agent」
  • 2025年口碑好的美甲培训学校推荐,专业美甲课程与就业支持全解析 - 工业推荐榜
  • Ryuko-NEHT Reloaded! MAME 0.116 Hack合集
  • 导数题三步法:目标函数破单调性难题
  • centos7.9编译安装nginx 1.28.1
  • 【Open-AutoGLM使用全攻略】:从零到精通的5大核心技巧
  • 成都GEO优化新浪潮:当企业搜索流量被AI重塑,谁能成为领航者? - 品牌企业推荐师(官方)
  • Forest项目MySQL数据库迁移指南
  • 大模型RAG性能优化:一文读懂7种分块策略,小白也能轻松上手!
  • 论文AI率过高怎么降?3款免费工具亲测有效,附详细避坑指南
  • 【Open-AutoGLM源码下载地址曝光】:全球首发!AI自动代码生成神器核心代码免费获取
  • Windows Server 2016下搭建SQL Server 2012集群
  • Open-AutoGLM手机端首发实测:大模型离线运行真的可行了吗?
  • Codev材料库
  • 得物Java面试被问:Fork/Join框架的使用场景
  • 使用wechatDownload批量保存公众号文章
  • 为开源数据库集群配置VIP(虚拟IP)的2种方案
  • 2025年口碑好的中空板行业内口碑厂家排行榜 - 行业平台推荐
  • 月薪6W不是梦!双非二本科生也能挑战这个高薪新兴领域,抓住机会就是现在!
  • 天津集装箱生产源头厂家口碑炸裂,集装箱办公/箱式房/活动板房/集装箱住宿,集装箱生产厂家哪个好 - 品牌推荐师
  • 2025最新智能安检门/X光安检机/升降柱厂家TOP10推荐:场景适配与合规实力双优榜单 - 深度智识库
  • TPAMI‘25 | 首个多轮、开放视角视频问答基准,系统分类9大幻觉任务
  • “函数指针”和“指针函数”区分清楚
  • 深入理解函数栈帧的创建与销毁过程