C#: 用Libreoffice实现Word文件转PDF

现实场景中要实现Word格式转PDF格式还是比较常见的。

如果要用开源的组件,只有用Libreoffice了。

一、下载安装Libreoffice

先进入如下链接,找到最新版本和匹配的操作系统来安装。

官网试过,下载是能下载,但安装了用不了,下面的链接是镜像。

https://mirrors.cloud.tencent.com/libreoffice/libreoffice/stable/

下面的链接可以直接下载。

LibreOffice_25.2.2_Win_x86-64

二、下面是C#的帮助类中的方法:

cs 复制代码
/// <summary>
/// 从网络上的Word文件,获取到pdf, 保存到临时文件。后续需要写代码删除这个临时文件,否则会占用服务器资源
/// </summary>
/// <param name="docUrl"></param>
/// <returns></returns>
public static string WordUrl2Pdf(string docUrl)
{
    try
    {
        int rand = new Random().Next(1000, 9999);
        var tempWord = $"d:\\tmp\\Convert\\{rand}.docx";
        var tempPdf = $"d:\\tmp\\Convert\\{rand}.pdf";
        FileHelper.DownloadAndSave(docUrl, tempWord);
        Word2Pdf(tempWord, tempPdf);
        File.Delete(tempWord);
        return tempPdf;
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
        return null;
    }
}

/// <summary>
/// 将 Word 文件转换为 PDF
/// </summary>
/// <param name="docPath">Word 文件路径</param>
/// <param name="pdfPath">输出 PDF 文件路径</param>
public static void Word2Pdf(string docPath, string pdfPath)
{
    // 检查输入文件是否存在
    if (!File.Exists(docPath))
    {
        throw new FileNotFoundException("输入文件不存在!", docPath);
    }

    // 确保输出目录存在
    string outputDir = System.IO.Path.GetDirectoryName(pdfPath);
    if (!Directory.Exists(outputDir))
    {
        Directory.CreateDirectory(outputDir);
    }

    // 定义 LibreOffice 路径和动态端口号
    string libreOfficePath = @"d:\Program Files\LibreOffice\program\soffice.exe";
    int port = GetUniquePort(); // 获取唯一端口号

    // 启动 LibreOffice 实例并执行转换
    Process process = new Process();
    process.StartInfo.FileName = libreOfficePath;
    process.StartInfo.Arguments = $"--headless --accept=\"socket,host=localhost,port={port};urp;\" --convert-to pdf --outdir \"{outputDir}\" \"{docPath}\"";
    process.StartInfo.UseShellExecute = false;
    process.StartInfo.CreateNoWindow = true;

    try
    {
        Console.WriteLine($"正在转换文件 {docPath} -> {pdfPath},使用端口: {port}");
        process.Start();
        process.WaitForExit();

        if (process.ExitCode != 0)
        {
            throw new Exception($"转换失败,退出代码: {process.ExitCode}");
        }
    }
    catch (Exception ex)
    {
        throw new Exception($"转换文件 {docPath} 时发生错误: {ex.Message}", ex);
    }
    finally
    {
        // 确保进程结束
        if (!process.HasExited)
        {
            process.Kill();
        }
    }
}

/// <summary>
/// 获取唯一的端口号
/// </summary>
/// <returns>唯一端口号</returns>
private static int GetUniquePort()
{
    // 使用 Interlocked.Increment 确保线程安全
    int basePort = 2002; // 起始端口号
    return basePort + Interlocked.Increment(ref _portCounter);
}

private static int _portCounter = 0; // 全局计数器,用于生成唯一端口号
相关推荐
yivifu12 小时前
怎样将Word文档中脚注引用后面的空格轻松删除
word·vba
Sour13 小时前
【无标题】aPPT 翻译后保留版式的流程:文本框、图片、母版和动画检查
pdf·powerpoint
无心水15 小时前
27、私有化部署|PaddleOCR-Server 本地OCR服务搭建
金融·pdf·ocr·pdf解析·pdf抽取·pdf提取·ocr服务
开开心心就好20 小时前
解决截图被拦截黑屏问题的免费小工具
安全·智能手机·flink·kafka·pdf·音视频·1024程序员节
软件工程小施同学21 小时前
CCF A区块链论文分享-NDSS 2026(2)-CtPhishCapture:揭露针对加密货币钱包的基于凭证窃取的网络钓鱼诈骗(附pdf)
网络·pdf·区块链
2601_9618451521 小时前
2026法考资料pdf|电子版|资料已整理
开发语言·前端框架·pdf·c#·xhtml·csrf·view design
qq_4221525721 小时前
PDF 解密工具怎么选?2026 年文档密码移除方案与注意事项
java·前端·pdf
Sour21 小时前
Word 文档翻译后保留格式的检查清单:标题、表格、图片、目录和批注
pdf·word·办公软件·office·文档翻译
Sour21 小时前
扫描版 PDF 翻译流程:OCR 识别、译文校对和排版保留
pdf·ocr
zyplayer-doc1 天前
继飞书之后,又一款知识库推出了开源 Agent-Native CLI 工具
人工智能·pdf·编辑器·飞书·开源软件