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

阅读: 评论:0

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

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 = "/",
            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:WechatFile123.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 中都可以下载  无水印的

本文发布于:2024-02-03 05:28:51,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170690933148952.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:转成   Interop   Office   Microsoft   PDF
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23