Asp.net mvc word预览与打印
解决方案:
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预览与打印的更多相关文章
- ASP.NET MVC在线预览Excel、Word、TXT、PDF文件
代码: using System; using System.Collections.Generic; using System.Linq; using System.Web; using Syste ...
- 尝新体验ASP.NET Core 6预览版本中发布的最小Web API(minimal APIS)新特性
本文首发于<尝新体验ASP.NET Core 6预览版本中发布的最小Web API(minimal APIS)新特性> 概述 .NET开发者们大家好,我是Rector. 几天前(美国时间2 ...
- 数据库下载word预览功能的研究
本文参考了这里的一些方法http://tobetobe.blog.51cto.com/1392243/354420 一直想通过缓存来实现,奈何技术不够,走了曲线救国的思路,先下载,然后预览,删除下载文 ...
- 网站开发进阶(十二)JS实现打印功能(包括打印预览、打印设置等)
JS实现打印功能(包括打印预览.打印设置等) 绪 最近在进行项目开发时,需要实现后台管理端打印功能,遂在网上一阵搜索,搜到了很多相关的文章.其中绝大部分文章都是使用的Lodop5.0(Web打印和套打 ...
- 借助flexpaper实现word在线预览和打印
为了实现word能够在web上尽量以原始的排版样式展现出来,选择基于activex控件的方式太过于依赖某种浏览器,并且存在可能需要花费金钱购买相应的控件产品:于是借助flexpaper这种flash展 ...
- JS 打印功能代码可实现打印预览、打印设置等
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.or ...
- word预览
word+excle表格在线浏览 word.ppt.xls文件实现在线预览的方式比较简单可以直接通过调用微软的在线预览功能实现 (预览前提:资源必须是公共可访问的) 通过iframe直接引用微软提供的 ...
- dev中 ,usercontrol打印界面内容,打印预览和打印
首先,在对应的文件夹下面添加引用,如下图 如果没有就下载下来包. 预先在本地文件夹下面建立.frx文件,像这样的, 然后在触发事件下面写 //打印预览 private void btn_Preview ...
- winform使用Barcodex控件预览和打印一维码
1.控件下载. http://files.cnblogs.com/files/masonblog/barcodex.zip . 包含barcodex.ocx控件.barcodex帮助文档.两个winf ...
随机推荐
- 注册表REG文件编写大全
Windows 中的注册表文件( system.dat 和 user.dat )是 Windows 的核心数据库,因此,对 Windows 来说是非常重要的. 通过修改注册表文件中的数据,可以达到优化 ...
- [转发]SPRING MVC3.2案例讲解--SPRING MVC3的@ResponseBody和ResponseEntity
在传统的开发过程中,我们的控制CONTROLL层通常需要转向一个JSP视图:但随着WEB2.0相关技术的崛起,我们很多时候只需要返回数据即可,而不是一个JSP页面. SPRING MVC3的@Resp ...
- jQuery中on()方法用法实例
这篇文章主要介绍了jQuery中on()方法用法,实例分析了on()方法的功能.定义及在匹配元素上绑定一个或者多个事件处理函数的使用技巧,需要的朋友可以参考下 本文实例讲述了jQuery中on()方法 ...
- MongoDB的分布式部署
一.分片的概念 分片(sharding)是指根据片键,将数据进行拆分,使其落在不同的机器上的过程.如此一来,不需要功能,配置等强大的机器,也能储存大数据量,处理更高的负载. 二.分片的原理和思想 Mo ...
- nginx php fastcgi安装
CGI全称是“公共网关接口”(Common Gateway Interface),HTTP服务器与你的或其它机器上的程序进行“交谈”的一种工具,其程序须运行在网络服务器上. CGI可以用任何一种语言编 ...
- 1.MySQL必知必会之数据库基础
下面这几个是几个关于数据库的关键字的概念,为后面的教程做基础的: 数据库:保存有组织的数据的容器(通常是一个文件或一组文件). 表: 某种特定类型数据的结构化清单. 模式:关于数据库和表的布局 ...
- 关于cgi、FastCGI、php-fpm、php-cgi(复制)
首先,CGI是干嘛的?CGI是为了保证web server传递过来的数据是标准格式的,方便CGI程序的编写者. web server(比如说nginx)只是内容的分发者.比如,如果请求/index.h ...
- Git在Githib和Github上的使用
本文使用的环境是linux里 一.git的常用命令解释: 1.基础命令: git init #创建版本库 git add <file> #将文件修改添加到暂存区 git commit -m ...
- AngularJS多模块开发与路由
这里只是做一个笔记 angularjs模块(父子级)比如我有一个项目叫做shcool,那么我school下边有两个模块,student.teacher.此时school就属于主模块,其他都是子模块.子 ...
- 今天刚接触lua 写写环境配置和基本设置
首先是安装开发工具: Quick: http://www.tairan.com/engines-download 引擎 Sublime Text的下载地址为:http://www.sublimetex ...