word转pdf

该代码示例展示了一个方法,使用Spire.Doc库将Word文档转换为PDF,同时设置了PDF的合规性级别并移除了生成的水印。在处理多个文件时,如果源文件是.docx格式,会先进行内容替换,然后转换为PDF。
摘要由CSDN通过智能技术生成
        /// <summary>
        /// word转pdf 免费版 25个table,10page
        /// </summary>
        /// <param name="sourcePath">word文件地址</param>
        /// <param name="targetPath">pdf文件地址</param>
        /// <returns></returns>
        public static bool WordToPDF(string wordPath, string pdfPath)
        {
            bool result = false;
            try
            {
                Spire.Doc.Document document = new Spire.Doc.Document();
                document.LoadFromFile(wordPath, Spire.Doc.FileFormat.Docx);

                ToPdfParameterList toPdf = new ToPdfParameterList();
                toPdf.PdfConformanceLevel = Spire.Pdf.PdfConformanceLevel.Pdf_A1B;
                document.SaveToFile(pdfPath, toPdf);

                // 消除水印
                PdfDocument doc = new PdfDocument(pdfPath);
                doc.Pages.RemoveAt(doc.Pages.Count - 1);
                doc.SaveToFile(pdfPath);

                result = true;
            }
            catch (Exception e)
            {
                result = false;
            }
            return result;
        }
        [HttpPost]
        [IdentifyDescription("all")]
        [AllowAnonymous]
        public HttpResponseMessage GetFileReply(JObject pars)
        {
            Reuse.CreateDir(StoragePath.zccl, out string fullPath, out string lastDir);
            JArray arrOpinionIds = pars.GetValue("ids") as JArray;
            long[] lArrOpinionIds = arrOpinionIds.Select(v => Reuse.GetId(v)).ToArray();
            var mlOpinion = db.Queryable<application_information>().Where(v => lArrOpinionIds.Contains(v.ID)).ToList();
            var name = "";
            var wordBytes = mlOpinion.Select(m =>
            {
                var mUser = db.Queryable<users>().Where(v => v.ID == m.UserNameID).ToList().FirstOrDefault();
                var mPorject = db.Queryable<project_information>().InSingle(m.ProjectID);
                name = mUser.name + ".docx";
                return new Tool.FileListModel
                {
                    name = $"{mUser.name}的职称证明.docx",
                    bytes = Tool.ExportWordByReplaceString(HttpContext.Current.Server.MapPath("/Word/technical.docx"),
                   (xmlText, wordNewLine, newLine1, newLine2) => WatcloController.ReplaceOpinionWord(xmlText, wordNewLine, newLine1, newLine2, mPorject, m, mUser))
                };
            }).ToList();
            if (mlOpinion.Count == 1)
            {
                using (FileStream fs = File.Create(fullPath + name))
                {

                }
                File.WriteAllBytes(fullPath + name, wordBytes[0].bytes);
            }
            if (string.IsNullOrEmpty(name))
            {
                return null;
            }
            var path = System.Web.HttpContext.Current.Server.MapPath(StoragePath.zccl + name);
            if (!File.Exists(path))
            {
                return null;
            }
            bool flag = false;
            string fileExtension = Path.GetExtension(path);
            string newPath = string.Empty;
            string newUrl = string.Empty;
            if (fileExtension == ".docx")
            {
                newPath = path.Substring(0, path.Length - 4) + "pdf";
                newUrl = name.Substring(0, name.Length - 4) + "pdf";
                flag = Tool.WordToPDF(path, newPath);
            }
            else if (fileExtension == ".doc")
            {
                newPath = path.Substring(0, path.Length - 3) + "pdf";
                newUrl = name.Substring(0, name.Length - 3) + "pdf";
                flag = Tool.WordToPDF(path, newPath);
            }
            if (flag)
            {
                FileStream fs = new FileStream(newPath, FileMode.Open);
                byte[] buffer = new byte[fs.Length];
                fs.Read(buffer, 0, buffer.Length);
                fs.Close();
                return Reuse.ReturnFile(buffer);
            }
            else
            {
                FileStream fs = new FileStream(path, FileMode.Open);
                byte[] buffer = new byte[fs.Length];
                fs.Read(buffer, 0, buffer.Length);
                fs.Close();
                return Reuse.ReturnFile(buffer);
            }

        }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

滴滴答答哒

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值