word,html互转,html转pdf

在线word转html

http://www.docpe.com/word/word-to-html.aspx

html转word

        /// <summary>
        /// 获取确认书模板
        /// </summary>
        /// <returns></returns>
        private string GetTemplateString()
        {
            StringBuilder sbHtml = new StringBuilder();
            // 读取模板替换数据
            var path = Server.MapPath("~/App_Data/BookingConfirmationTemplate.html");
            using (Stream inStream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read))
            using (StreamReader outStream = new StreamReader(inStream, Encoding.Default))
            {
                while (!outStream.EndOfStream)
                {
                    sbHtml.Append(outStream.ReadLine());
                }
            }
            var html = sbHtml.ToString();
            return html;
        }


        /// <summary>
        /// 下载确认书
        /// </summary>
        /// <param name="orderId"></param>
        /// <returns></returns>
        public ActionResult DownloadBookingConfirmOrder(int orderId)
        {
            #region 读取模板
            var html = GetTemplateString();
            #endregion

            #region 根据ID获取订单内容 替换数据
            var order = OrderEFHelper.GetOrder(orderId);
            if (order != null)
            {
                var startDate = order.StartDate;
                html = html.Replace("@OrderNo", order.OrderNo.ToString())
                    .Replace("@ConfirmYear", startDate.ToString("yyyy"))
                    .Replace("@ConfirmMonth", startDate.ToString("MM"))
                    .Replace("@ConfirmDay", startDate.ToString("dd"));
            }
            #endregion

            #region 转换为Word文档样式

            StringBuilder sb = new StringBuilder();
            sb.Append(
                "<html xmlns:v=\"urn:schemas-microsoft-com:vml\"  xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:w=\"urn:schemas-microsoft-com:office:word\" xmlns:m=\"http://schemas.microsoft.com/office/2004/12/omml\"xmlns = \"http://www.w3.org/TR/REC-html40\">");
            sb.Append(html);
            sb.Append("</html>");
            return File(Encoding.UTF8.GetBytes(sb.ToString()), "application/msword", $"确认书.doc");

            #endregion
        }

html转pdf

使用iText库

图片说明

        private void SavePDF(string directoryPath, string path, string html)
        {
            if (!Directory.Exists(directoryPath))  //不存在文件夹,创建
            {
                Directory.CreateDirectory(directoryPath);  //创建新的文件夹
            }

            //html转pdf
            using (PdfWriter pdfWriter = new PdfWriter(path))
            {
                PdfDocument pdfDoc = new PdfDocument(pdfWriter);
                CustomFontProvider fontProvider = new CustomFontProvider();
                pdfDoc.SetDefaultPageSize(iText.Kernel.Geom.PageSize.A4);
                var config = new iText.Html2pdf.ConverterProperties();
                config.SetFontProvider(fontProvider);
                iText.Html2pdf.HtmlConverter.ConvertToPdf(html, pdfDoc, config);
                pdfDoc.Close();
            }


        }
全部评论

相关推荐

点赞 评论 收藏
转发
点赞 收藏 评论
分享
牛客网
牛客企业服务