Aspmvcword预览与打印
解决⽅案:
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, 0, bytes.Length);
using (var wDoc = WordprocessingDocument.Open(memoryStream, true))
{
var imageFolder = source.Substring(0, source.Length - 5) + "_files";
int imageCounter = 0;
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('/')[1].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);
}
html document是什么}
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'> load = 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);
}
代码有删减,不能拿来直接⽤

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。