现在国家在推广ofd格式的发票,但现在推广前期,ofd的接受度不高,很多情况下打不开ofd格式的发票,还是希望能接受pdf格式的发票。
在走了一些看起来是抄近路的实际是死胡同(比如用Python)的弯路后,用了Spire.PDF实现了ofd发票批量转PDF。
以下是.net core 6的C#源码:
文件OFD2PDF.csproj
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <OutputType>Exe</OutputType> <TargetFramework>net6.0-windows</TargetFramework> <ImplicitUsings>enable</ImplicitUsings> <Nullable>enable</Nullable> <UseWindowsForms>true</UseWindowsForms> <StartupObject>OFD2PDF.Program</StartupObject> </PropertyGroup> <ItemGroup> <PackageReference Include="FreeSpire.PDF" Version="8.6.0" /> </ItemGroup> </Project>
Program.cs
using Spire.Pdf.Conversion; namespace OFD2PDF { internal class Program { [STAThread] static void Main(string[] args) { FolderBrowserDialog ofd = new FolderBrowserDialog(); if (ofd.ShowDialog() == DialogResult.OK) { string folder = ofd.SelectedPath; DirectoryInfo directory = new DirectoryInfo(folder); List<FileInfo> ofdFiles = GetOFD(directory); foreach (FileInfo fi in ofdFiles) { Console.WriteLine(fi.FullName); OfdConverter converter = new OfdConverter(fi.FullName); converter.ToPdf(fi.FullName.Replace(".ofd", ".pdf", true, null)); } Console.WriteLine("All is done!"); } Console.ReadKey(); } static List<FileInfo> GetOFD(DirectoryInfo folder) { List<FileInfo> files = new List<FileInfo>(); foreach (FileInfo fi in folder.EnumerateFiles()) { if (fi.Extension.ToLower() == ".ofd") files.Add(fi); } foreach (DirectoryInfo d in folder.EnumerateDirectories()) { files.AddRange(GetOFD(d)); } return files; } } }
另外我也附上一个编译好的bin文件 https://files.cnblogs.com/files/crazyghostvon/OFD2PDF.zip?t=1699582426&download=true,解压密码是123456
送给需要的人。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)