解决方案:

1. 在后台把word文件转化成pdf,在前台用iframe显示pdf,打印iframe,即可。

优点:用户体验好。

缺点:不支持IE。

实现 :

引用netoffice组件

主要代码:

Word.Application wordApplication = new Word.Application();
Word.Document pDoc = wordApplication.Documents.Open(path);
pDoc.Activate(); pDoc.SaveAs(pdf, WdSaveFormat.wdFormatPDF);
wordApplication.Quit();
wordApplication.Dispose();

2. 在后台把word转化面html,读取html文本,增加打印脚本,输出到前台。

优点:浏览器支持的好。

缺点:用html显示word,不是太好看。

实现 :

引用:

主要代码:

    public class WordToHtmlConverter
{
/// <summary>
/// Convert Docx to Html
/// </summary>
/// <param name="source">source file full name</param>
/// <returns>htmlstring</returns>
public static string ToHtml(string source)
{
var htmlString = string.Empty;
var file = new FileInfo(source); byte[] bytes = File.ReadAllBytes(file.FullName);
using (MemoryStream memoryStream = new MemoryStream())
{
memoryStream.Write(bytes, , bytes.Length);
using (var wDoc = WordprocessingDocument.Open(memoryStream, true))
{
var imageFolder = source.Substring(, source.Length - ) + "_files";
int imageCounter = ; 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 (!localDirInfo.Exists)
localDirInfo.Create();
++imageCounter;
string extension = imageInfo.ContentType.Split('/')[].ToLower();
ImageFormat imageFormat = null;
if (extension == "png")
imageFormat = ImageFormat.Png;
else if (extension == "gif")
imageFormat = ImageFormat.Gif;
else if (extension == "bmp")
imageFormat = ImageFormat.Bmp;
else if (extension == "jpeg")
imageFormat = ImageFormat.Jpeg;
else if (extension == "tiff")
{
// Convert tiff to gif.
extension = "gif";
imageFormat = ImageFormat.Gif;
}
else if (extension == "x-wmf")
{
extension = "wmf";
imageFormat = ImageFormat.Wmf;
} // 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" +
imageCounter.ToString() + "." + extension;
try
{
imageInfo.Bitmap.Save(imageFileName, imageFormat);
}
catch (System.Runtime.InteropServices.ExternalException)
{
return null;
}
string imageSource = localDirInfo.Name + "/image" +
imageCounter.ToString() + "." + extension; XElement img = new XElement(Xhtml.img,
new XAttribute(NoNamespace.src, imageSource),
imageInfo.ImgStyleAttribute,
imageInfo.AltText != null ?
new XAttribute(NoNamespace.alt, imageInfo.AltText) : null);
return img;
}
};
XElement htmlElement = HtmlConverter.ConvertToHtml(wDoc, settings); // Produce HTML document with <!DOCTYPE html > declaration to tell the browser we are using HTML5.
var html = new XDocument(new XDocumentType("html", null, null, null), htmlElement); htmlString = html.ToString(SaveOptions.DisableFormatting);
}
} return htmlString;
}
}
        public string NoticePrint(NoticeResult model)
{
// 第一步,生成新的doc文档,替换模板文件中的标签,这里用的是NPOI,这个方法就不显示了,没什么东西。
var path = this.GetDocFileName(model); // 第二步,把doc文件,转化成html文件
var html = WordToHtmlConverter.ToHtml(path); // 第三步,对html追加script脚本
var result = html + GetPrintScript(); // 第四步,返回
return result;
} private string GetPrintScript()
{
return @"<script type='text/javascript'> window.onload = function() { window.print(); };</script >";
}

Controller Code:

public ActionResult Print(NoticeResult model)
{
var result = new Response<bool>(); if (model != null)
{
var html = this.service.NoticePrint(model); return Content(html, "text/html");
} result.ErrMsg = "打印参数异常"; return Json(result); }

代码有删减,不能拿来直接用!!!

Asp.net mvc word预览与打印的更多相关文章

  1. ASP.NET MVC在线预览Excel、Word、TXT、PDF文件

    代码: using System; using System.Collections.Generic; using System.Linq; using System.Web; using Syste ...

  2. 尝新体验ASP.NET Core 6预览版本中发布的最小Web API(minimal APIS)新特性

    本文首发于<尝新体验ASP.NET Core 6预览版本中发布的最小Web API(minimal APIS)新特性> 概述 .NET开发者们大家好,我是Rector. 几天前(美国时间2 ...

  3. 数据库下载word预览功能的研究

    本文参考了这里的一些方法http://tobetobe.blog.51cto.com/1392243/354420 一直想通过缓存来实现,奈何技术不够,走了曲线救国的思路,先下载,然后预览,删除下载文 ...

  4. 网站开发进阶(十二)JS实现打印功能(包括打印预览、打印设置等)

    JS实现打印功能(包括打印预览.打印设置等) 绪 最近在进行项目开发时,需要实现后台管理端打印功能,遂在网上一阵搜索,搜到了很多相关的文章.其中绝大部分文章都是使用的Lodop5.0(Web打印和套打 ...

  5. 借助flexpaper实现word在线预览和打印

    为了实现word能够在web上尽量以原始的排版样式展现出来,选择基于activex控件的方式太过于依赖某种浏览器,并且存在可能需要花费金钱购买相应的控件产品:于是借助flexpaper这种flash展 ...

  6. JS 打印功能代码可实现打印预览、打印设置等

      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.or ...

  7. word预览

    word+excle表格在线浏览 word.ppt.xls文件实现在线预览的方式比较简单可以直接通过调用微软的在线预览功能实现 (预览前提:资源必须是公共可访问的) 通过iframe直接引用微软提供的 ...

  8. dev中 ,usercontrol打印界面内容,打印预览和打印

    首先,在对应的文件夹下面添加引用,如下图 如果没有就下载下来包. 预先在本地文件夹下面建立.frx文件,像这样的, 然后在触发事件下面写 //打印预览 private void btn_Preview ...

  9. winform使用Barcodex控件预览和打印一维码

    1.控件下载. http://files.cnblogs.com/files/masonblog/barcodex.zip . 包含barcodex.ocx控件.barcodex帮助文档.两个winf ...

随机推荐

  1. Redis应用案例,查找某个值的范围(转)

    原文:https://groups.google.com/forum/#!topic/redis-db/lrYbkbxfQiQ 本文来自Redis在Google Group上的一个问题,有一位同学发贴 ...

  2. win10专业版密钥激活

    win10专业版密钥激活 1.查看激活状态,win按键>设置(左下角倒数第二个)>更新和安全>激活---------提示没有有效密钥,需要激活 2.管理员身份打开cmd,搜索框输入c ...

  3. 2.MySQL简介

    MySQL是一种DBMS,即它是一种数据库软件.   1.连接   为了连接到MyAQL,需要一下信息: ➡️主机名(计算机名)---如果连接到本地MySQL服务器,为localhost ➡️duan ...

  4. Python 开发面试总结

    网络基础 如何确定发送过来的数据的完整性(有无中间人攻击)? 散列值校验(MD5.SHA-1).数字签名(PGP),需要用户亲自校验,若是散列值或数字签名本身被篡改,用户是无法判断出来的. HTTPS ...

  5. spark-streaming读kafka数据到hive遇到的问题

    在项目中使用spark-stream读取kafka数据源的数据,然后转成dataframe,再后通过sql方式来进行处理,然后放到hive表中, 遇到问题如下,hive-metastor在没有做高可用 ...

  6. tomcat源码调试

    三.tomcat目录结构 tomcat的下载安装有很多教程,不再赘述. 现在的tomcat已经到9了,当tomcat下载安装完成后,其目录大致如下:     除了上面的文件夹,还有四个文件:     ...

  7. java中boolean类型占几个字节

    java的基本数据类型中,boolean只有两种状态,默认值为false.取值范围是{true,false},理论上占1bit,实际上: 1.单个的boolean 类型变量在编译的时候是使用的int ...

  8. JS正则表达式从入门到入土(2)—— 元字符和字符类

    元字符和字符类 元字符 正则表达式由两种基本字符类型组成: 1.原义(正常)文本字符:代表本身含义的字符,如:a.b.c.1.2.3等. 2.元字符:元字符是在正则表达式中有特殊含义的非字母字符,如\ ...

  9. linux下 安装php的gettext模块

    安装php的模块有两种方式: 一.重新编译php,加上--with-gettext 二.动态安装 现在说下第二个动态安装 1.下载同版本的php原包,解压后进入ext目录,目录下便是模块 2.进入ge ...

  10. win764位英文版系统unicode编码问题

    出现的编码现象: 1.以*.vga格式的课件文件,当作好可读html之后,仍然不能播放,此时为Unicode编码所致: 2.在编辑TXT文档时,内容凡有中文,在保存时,会提示不能识别,当确认后,中文字 ...