C# 使用Microsoft.Office.Interop.Word 将WORD转成PDF

    /// <summary>
    /// 测试文件
    /// </summary>
    /// <param name="input">文件名</param>
    /// <returns></returns>
    [ApiDescriptionSettings(Name = "Getword")]
    [HttpGet]
    public IActionResult getWord(string wordName)
    {
        string templatePath = "D:\\Template\\wordTemplate.docx";
        string log = "D:\\Template\\l123jpg";
        var tt = new MiniWordPicture() { Extension = DocumentFormat.OpenXml.Packaging.ImagePartType.Jpeg.ToString(), Bytes = File.ReadAllBytes(log), Width = 200, Height = 100 };
        var baidu = new MiniWordHyperLink()
        {
            Url = "https://www.baidu.com/",
            Text = "百度"
        };
        //var tt = new MiniWordPicture() { Extension = ImagePartType.Jpeg.ToString(), Bytes = FileToByte(log), Width = 100, Height = 100 };
        var endDate = new MiniWordColorText { Text = DateTime.Parse("2022-09-15 15:30:00").ToString(), HighlightColor = "#eb70AB", FontColor = "#ffffff" };
        var value = new Dictionary<string, object>()
        {
            ["Name"] = "Jack",
            ["Department"] = "IT Department",
            ["Purpose"] = "Shanghai site needs a new system to control HR system.",
            ["StartDate"] = DateTime.Parse("2022-09-07 08:30:00"),
            ["EndDate"] = endDate,//
            ["Approved"] = true,
            ["Total_Amount"] = 123456,
            //["Logo"] = new MiniWordPicture() { Path = log, Width = 180, Height = 180 },
            ["Logo"] = tt,
            ["baidu"] = baidu,
            ["person"] = new List<Dictionary<string, object>> {
                 new Dictionary<string, object>{ { "name","wade"},{ "age","HR" } },
                 new Dictionary<string, object>{ { "name","admin"},{ "age","it" } },
                 new Dictionary<string, object>{ { "name","erid"},{ "age","测试" } },
                 new Dictionary<string, object>{ { "name","张三"},{ "age","HR" } },
                 new Dictionary<string, object>{ { "name","大"},{ "age","HR在" } },
            }
        };
        MemoryStream wordStream = new MemoryStream();
        MiniWord.SaveAsByTemplate(wordStream, templatePath, value);
        wordStream.Close();

        return new FileStreamResult(new MemoryStream(wordStream.GetBuffer()), "application/octet-stream") { FileDownloadName = wordName + ".docx" };
    }


    public static byte[] ConvertWordToPdf(string wordUrl, string pdfUrl)
    {

        wordUrl = "D:\\Template\\wordTemplate.docx";  //源word地址
         pdfUrl = @"D:\WechatFile\123.pdf";  //生成后的pdf文件地址
        // 创建Word应用程序对象
        Microsoft.Office.Interop.Word.Application wordApplication = new Microsoft.Office.Interop.Word.Application();

        // 将Word文件加载到Document对象中
        Document document = wordApplication.Documents.Open(wordUrl);

        // 创建内存流,并将PDF文件流保存到其中
        MemoryStream pdfStream = new MemoryStream();
        document.SaveAs(pdfUrl, WdExportFormat.wdExportFormatPDF);

        // 关闭Word文档和应用程序对象
        document.Close(false);
        wordApplication.Quit(false);

        // 将生成的PDF转换为byte数组并返回
        byte[] pdfBytes = pdfStream.ToArray();
        pdfStream.Close();

        return pdfBytes;
    }

对应的几个dll版本都在截图中,在nuget 中都可以下载  无水印的

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值