2023年7月3日发(作者:)
word预览与打印
解决⽅案:1. 在后台把word⽂件转化成pdf,在前台⽤iframe显⽰pdf,打印iframe,即可。优点:⽤户体验好。缺点:不⽀持IE。实现 :引⽤netoffice组件主要代码:
ation wordApplication = new ation(); nt pDoc = (path); te(); (pdf, atPDF); (); e();
2. 在后台把word转化⾯html,读取html⽂本,增加打印脚本,输出到前台。优点:浏览器⽀持的好。缺点:⽤html显⽰word,不是太好看。实现 :引⽤:主要代码: public class WordToHtmlConverter { ///
HtmlConverterSettings settings = new HtmlConverterSettings() { AdditionalCss = "body { margin: 1cm auto; max-width: 20cm; padding: 0; }", PageTitle = "新系统", FabricateCssClasses = true, CssClassPrefix = "pt-", RestrictToSupportedLanguages = false, RestrictToSupportedNumberingFormats = false, ImageHandler = imageInfo => { DirectoryInfo localDirInfo = new DirectoryInfo(imageFolder); if (!) (); ++imageCounter; string extension = ('/')[1].ToLower(); ImageFormat imageFormat = null; if (extension == "png") imageFormat = ; else if (extension == "gif") imageFormat = ; else if (extension == "bmp") imageFormat = ; else if (extension == "jpeg") imageFormat = ; else if (extension == "tiff") { // Convert tiff to gif. extension = "gif"; imageFormat = ; } else if (extension == "x-wmf") { extension = "wmf"; imageFormat = ; } // If the image format isn't one that we expect, ignore it, // and don't return markup for the link. if (imageFormat == null) return null; string imageFileName = imageFolder + "/image" + ng() + "." + extension; try { (imageFileName, imageFormat); } catch (alException) { return null; } string imageSource = + "/image" + ng() + "." + extension; XElement img = new XElement(, new XAttribute(, imageSource), leAttribute, t != null ? new XAttribute(, t) : null); return img; } }; XElement htmlElement = tToHtml(wDoc, settings); // Produce HTML document with declaration to tell the browser we are using HTML5. var html = new XDocument(new XDocumentType("html", null, null, null), htmlElement); htmlString = ng(eFormatting); } } return htmlString; } } public string NoticePrint(NoticeResult model) { // 第⼀步,⽣成新的doc⽂档,替换模板⽂件中的标签,这⾥⽤的是NPOI,这个⽅法就不显⽰了,没什么东西。 var path = FileName(model); // 第⼆步,把doc⽂件,转化成html⽂件 var html = (path); // 第三步,对html追加script脚本 var result = html + GetPrintScript(); // 第四步,返回 return result; } private string GetPrintScript() { return @""; }Controller Code:public ActionResult Print(NoticeResult model) { var result = new Response
return Content(html, "text/html"); } = "打印参数异常"; return Json(result); }
代码有删减,不能拿来直接⽤
发布者:admin,转转请注明出处:http://www.yc00.com/web/1688380908a129412.html
评论列表(0条)