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 ...
随机推荐
- 如何使用 libtorch 实现 AlexNet 网络?
如何使用 libtorch 实现 AlexNet 网络? 按照图片上流程写即可.输入的图片大小必须 227x227 3 通道彩色图片 // Define a new Module. struct Ne ...
- System.ArgumentException: 字体“Courier New”不支持样式“Regular”。
使用MongoVUE,发现报错,报错信息如下: System.ArgumentException: 字体“Courier New”不支持样式“Regular”. 说明本机字体安装不够:需安装完整的Co ...
- Flask路由系统与模板系统
路由系统 @app.route('/user/<username>') @app.route('/post/<int:post_id>') @app.route('/post/ ...
- Flask的配置与路由
配置管理 flask中的配置文件是一个flask.config.Config对象(继承字典),默认配置为: { 'DEBUG': get_de ...
- TensorFlow学习笔记(三)-- feed_dict 使用
个人理解:就是TF的一种输入语法. 跟C语言的scanf(),C++的 cin>> 意思差不多,只是长相奇怪了点而已. 做完下面几个例子,基本也就适应了. 首先占位符申请空间:使用的时候, ...
- Spark2.0 特征提取、转换、选择之二:特征选择、文本处理,以中文自然语言处理(情感分类)为例
特征选择 RFormula RFormula是一个很方便,也很强大的Feature选择(自由组合的)工具. 输入string 进行独热编码(见下面例子country) 输入数值型转换为double(见 ...
- SVM数学原理推导&鸢尾花实例
//看了多少遍SVM的数学原理讲解,就是不懂,对偶形式推导也是不懂,看来我真的是不太适合学数学啊,这是面试前最后一次认真的看,并且使用了sklearn包中的SVM来进行实现了一个鸢尾花分类的实例,进行 ...
- MySQL整理(二)
一.MySQL操作表的约束 MySQL提供了一系列机制来检查数据库表中的数据是否满足规定条件,以此来保证数据库表中数据的准确性和一致性,这种机制就是约束. (1)设置非空约束(NOT NULL),唯一 ...
- 剑指offer-java
面试题67 机器人的运动范围 题意: 地上有一个m行和n列的方格.一个机器人从坐标0,0的格子开始移动,每一次只能向左,右,上,下四个方向移动一格,但是不能进入行坐标和列坐标的数位之和大于k的格子. ...
- 8种主要排序算法的C#实现 (二)
归并排序 归并排序也是采用“分而治之”的方式.刚发现分治法是一种算法范式,我还一直以为是一种需要意会的思想呢. 不好意思了,孤陋寡闻了,哈哈! 原理:将两个有序的数列,通过比较,合并为一个有序数列. ...