『壹』 .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();