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 ...
随机推荐
- 适配器模式(Adapter Pattern)--不兼容结果的协调
定义:将一个接口转换成客户希望的另一个接口,使接口不兼容的那些类可以一起工作,其别名为包装器(Wrapper); 分类: 对象适配器:适配器与适配者之间是关联关系; 类适配器:适配器和适配者之间是继承 ...
- Python并行编程(八):with语法
1.基本概念 当有两个相关的操作需要在一部分代码块前后分别执行的时候,可以使用with语法自动完成.同时,使用with语法可以在特定的地方分配和释放资源,因此,with语法也叫作"上下文管理 ...
- tornado调用ioloop TracebackFuture实现非堵塞的模块
转载http://xiaorui.cc/2014/11/26/tornado调用ioloop-tracebackfuture实现非堵塞的模块/ 当然实现的方法,还是存在点问题的, 但是最少流程是跑通了 ...
- 【MFC系列】MFC快速设置控件文本字体、大小、颜色、背景
以静态文本为例,分享一下怎么修改文本字体.大小.颜色.背景等参数.其他文本.控件等可参照修改. 1.修改字体.大小 这个很简单,首先在Dlg类中声明一个CFont类型的成员变量: 然后在类的初始化函数 ...
- 跨域问题session不一致问题的解决(前端和后台在不同的服务器)
场景:首先调用发送短信接口,然后调用注册接口,结果后台的session不一致获取不到之前验证码. 实质:每次ajax跨域访问都是新的session ,每次的sessionID都不一样 解决: pack ...
- 关于android:windowNoTitle不起作用的解决办法
今天测试一个新功能的时候,在styles.xml设置<item name="android:windowNoTitle">true</item>并没有生效, ...
- oracle RMAN复制数据库
列出创建副本数据库的目的 • 选择用于复制数据库的方法 • 使用RMAN 复制数据库 • 使用RMAN 备份复制数据库 • 基于正在运行的实例复制数据库 使用副本数据库 • 使用副本数据库可执行以 ...
- PEP8编码规范
1.代码布局设计 1.1 缩进 -4个空格进行缩进 1.2 tab键-在python2中tab和空格是混用的,但是在python中基本上使用tab(pycharm开发工具会自动对代码缩进) 1.3 最 ...
- 使用cygwin移植Linux的项目到Windows下之总结(转)
使用cygwin移植Linux的项目到Windows下之总结(转) 原文 http://my.oschina.net/michaelyuanyuan/blog/68615?p=1 一.why ...
- property函数的使用
描述 property函数的作用是在新式类中返回属性值 在绑定属性时,如果我们直接把属性暴露出去,虽然写起来很简单,但是,没办法检查参数,导致可以把成绩随便改 s=Student() s.score= ...