‘壹’ .NET中实现HTML生成图片或pdf的几种方式
在.NET中,实现HTML转图片或PDF的功能有多种途径,但每种方案都有其优缺点。这里我们将探讨WebBrowser、Wkhtmltox、PuppeteerSharp以及IronPdf这四种方法的对比。
首先,WebBrowser控件是最基础的实现方式,无需外部依赖,部署简单。然而,它在非Winform项目中不稳定,数据量大时易卡死,且生成的图片质量欠佳。清爽指数高,但功能有限。
Wkhtmltox是一个开源工具,通过命令行工具转换,适合.NET项目,但部署前需要先安装。它功能强大,但部署较为繁琐,清爽指数略低,功能指数高。
PuppeteerSharp源于Puppeteer,是.NET版本的浏览器控制库,功能极其强大,包括模拟用户操作和指定设备。它依赖Chromium,安装过程可能耗时,适合作为独立服务。清爽指数一般,功能指数非常高。
IronPdf是一个商业软件,提供免费试用版,功能全面,支持多种配置。通过DLL引用或NuGet安装,但不如开源工具灵活。清爽指数和功能指数都较高。
最终,项目选择了不用上述方案,而是通过正则解析HTML内容,手动绘图生成图片,因为需求简单且页面结构稳定。这种方式虽然简单,但对HTML结构变化敏感。
总结,选择哪种方法取决于具体需求、性能要求和部署环境,需根据实际场景权衡清爽度与功能性的折中。
‘贰’ ASP.NET 网站,将word文档转换成PDF格式,然后上传的系统所在的文件夹。
你可以借助Office的功能实现doc到pdf。这样做需要需要office 2007 还有一个office2007的插件OfficeSaveAsPDFandXPS,然后借助程序代码就可以实现了!但是这样做有个不好的地方就是必须安装Office,可能会导致服务器不是太安全!具体的实现代码网上多的是比如:
using Microsoft.Office.Interop.Word;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
...
// Create a new Microsoft Word application object
Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();
// C# doesn't have optional arguments so we'll need a mmy value
object oMissing = System.Reflection.Missing.Value;
// Get list of Word files in specified directory
DirectoryInfo dirInfo = new DirectoryInfo(@"\\server\folder");
FileInfo[] wordFiles = dirInfo.GetFiles("*.doc");
word.Visible = false;
word.ScreenUpdating = false;
foreach (FileInfo wordFile in wordFiles)
{
// Cast as Object for word Open method
Object filename = (Object)wordFile.FullName;
// Use the mmy value as a placeholder for optional arguments
Document doc = word.Documents.Open(ref filename, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing);
doc.Activate();
object outputFileName = wordFile.FullName.Replace(".doc", ".pdf");
object fileFormat = WdSaveFormat.wdFormatPDF;
// Save document into PDF Format
doc.SaveAs(ref outputFileName,
ref fileFormat, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing);
// Close the Word document, but leave the Word application open.
// doc has to be cast to type _Document so that it will find the
// correct Close method.
object saveChanges = WdSaveOptions.wdDoNotSaveChanges;
((_Document)doc).Close(ref saveChanges, ref oMissing, ref oMissing);
doc = null;
}
// word has to be cast to type _Application so that it will find
// the correct Quit method.
((_Application)word).Quit(ref oMissing, ref oMissing, ref oMissing);
word = null;
‘叁’ 如何用.NET将DWG文件打印为PDF
pdf是扫描件,这个需要打印之后,扫描就是PDF文件了
‘肆’ 急~急~~急~~~ ASP.NET 怎么讲图片生成PDF文件,求代码
我是借助组件完成导出PDF文件的。在写代码之前,你要下载一个名为iTextSharp的组件,并将其引用到你的程序中。然后将生成的PDF文件通过流形式导出。
代码如下:C#的
stringstrPDF_Nm=DateTime.Now.Year+"-"+DateTime.Now.Month+"-"+DateTime.Now.Day+"-"+DateTime.Now.Hour+"-"+DateTime.Now.Minute+".pdf";
iTextSharp.text.Documentdocument=newDocument();
iTextSharp.text.pdf.PdfWriterpdfwrite=PdfWriter.GetInstance(document,newFileStream(HttpContext.Current.Server.MapPath("~/img_Tmp/"+strPDF_Nm),FileMode.Create));
document.Open();
BaseFontbasefont=BaseFont.CreateFont(@"C:WindowsFontsSTKAITI.TTF",BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);
//解决PDF不能显示中文的关键;创建一个中文楷体的字体对象
iTextSharp.text.Fontfont=newiTextSharp.text.Font(basefont,14);
iTextSharp.text.Tabletable=newiTextSharp.text.Table(4);
iTextSharp.text.Cellcells=newCell(newPhrase("不良报告",font));
cells.Colspan=4;
cells.HorizontalAlignment=1;
table.AddCell(cells);
iTextSharp.text.Imagejpg=iTextSharp.text.Image.GetInstance(Server.MapPath("~/img_Tmp/"+strSavePath));
document.Add(jpg);
document.Add(table);
document.Close();
pdfwrite.Close();
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ContentType="application/PDF";
HttpContext.Current.Response.WriteFile(HttpContext.Current.Server.MapPath("~/img_Tmp/"+strPDF_Nm));
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.Close();