日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

c# asp.net Pdf 转换图片 在线预览 发布到iis中问题 最终解决篇—_—!

發(fā)布時間:2025/7/14 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c# asp.net Pdf 转换图片 在线预览 发布到iis中问题 最终解决篇—_—! 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

關(guān)于:excel和word 預(yù)覽 請看我的博文:

excel和word 在線預(yù)覽 ?詳細(xì)配置及代碼

?

使用Adobe 組件 在本機(jī)vs中調(diào)試成功

發(fā)布到iis中? 在??代碼中涉及到? 剪貼板的地方? 會不成功? 莫名其妙的沒有數(shù)據(jù)?? 也不報錯????? 設(shè)置iis權(quán)限 、com組件權(quán)限+各種搞??? 均無果.....??? -_-!? 很是郁悶????

?

最終放棄

改用 Ghostscript??

須安裝? gs861w32.exe? (高版本 貌似 還有問題......)

在安裝目錄 bin 下 找到gsdll32.dll

下在 dll

itextsharp.dll

PDFView.dll

把三個dll放入 項目dll(新建)文件夾中???

引入

itextsharp.dll

PDFView.dll

gsdll32.dll無法引入?? 拷貝到項目bin 目錄下

上代碼:

View Code 1 /// <summary>2 /// 將PDF 相應(yīng)的頁轉(zhuǎn)換為圖片3 /// </summary>4 /// <param name="strPDFpath">PDF 路徑</param>5 /// <param name="Page">需要轉(zhuǎn)換的頁頁碼</param>6 private string GetImage(string strPDFpath, string imgDire, ImageFormat imgeF)7 {8 StringBuilder b = new StringBuilder();9 10 PdfReader reader = new PdfReader(strPDFpath); 11 // 獲得文檔頁數(shù) 12 int pageCount = reader.NumberOfPages; 13 14 System.IO.MemoryStream Ms = new MemoryStream(); 15 try 16 { 17 b = b.AppendLine("<ul style='azimuth:center; list-style-type:none;' >"); 18 for (int page = 1; page <= pageCount; page++) 19 { 20 System.Drawing.Image img = PDFView.ConvertPDF.PDFConvert.GetPageFromPDF(strPDFpath, page, 100, "", true); 21 img.Save(Ms, imgeF); 22 23 Bitmap returnImage = (Bitmap)Bitmap.FromStream(Ms); 24 25 string strImgPath = Request.MapPath("..\\" + imgDire + "\\" + page.ToString("0000") + ".jpg"); 26 27 returnImage.Save(strImgPath); 28 29 Ms.Position = 0; 30 31 32 b = b.AppendLine("<li> <img src='..\\" + imgDire + "\\" + page.ToString("0000") + ".jpg' /> </li><span>第" + (page) + "頁</span>"); 33 } 34 35 Ms.Close(); 36 b = b.AppendLine("</ul>"); 37 38 } 39 catch (Exception ex) 40 { 41 // b.Clear(); 42 // b.AppendLine(ex.ToString()); 43 throw; 44 } 45 46 47 return b.ToString(); 48 49 50 }

vs 運(yùn)行? 成功

發(fā)布到服務(wù)器iis 中?? 設(shè)置 iis 對應(yīng)應(yīng)用程序池?? 啟用32為應(yīng)用程序? 為true

-_-! 報錯

System.InvalidOperationException: 當(dāng)應(yīng)用程序不是以 UserInteractive 模式運(yùn)行時顯示模式對話框或窗體是無效操作。請指定 ServiceNotification 或 DefaultDesktopOnly 樣式,以顯示服務(wù)應(yīng)用程序發(fā)出的通知。

?

?

?

換一種調(diào)用方法??? :在web中調(diào)用外部的控制臺程序?

將上面方法 放入控制臺程序中

mian函數(shù)如下:

View Code 1 [STAThread]2 static void Main(string[] args)3 {4 #region MyRegion5 /*6 if (args != null && args.Length > 0)7 {8 string PdfPath = args[0].ToString();9 string ImgPath = args[1].ToString(); 10 string imgDire = args[2].ToString(); 11 string b = PDFToPic(PdfPath,ImgPath ,imgDire, ImageFormat.Jpeg); 12 13 if (string.IsNullOrEmpty(b)) 14 { 15 b = "出錯"; 16 } 17 18 Console.WriteLine(b); 19 Console.ReadKey(); 20 21 } 22 else 23 { 24 Console.WriteLine("無參數(shù)"); 25 Console.ReadKey(); 26 } 27 */ 28 29 #endregion 30 31 32 #region gs 33 34 if (args != null && args.Length > 0) 35 { 36 string PdfPath = args[0].ToString(); 37 string HtmlDic = args[1].ToString(); 38 string ImgPath = args[2].ToString(); 39 40 string b = GetImage(PdfPath, HtmlDic, ImgPath, ImageFormat.Jpeg); 41 42 if (string.IsNullOrEmpty(b)) 43 { 44 b = "出錯"; 45 } 46 Console.WriteLine(b); 47 } 48 else 49 { 50 Console.WriteLine("無參數(shù)"); 51 } 52 #endregion 53 // GetImage(@"C:\Users\Administrator.DVT\Desktop\fu.pdf", @"C:\Users\Administrator.DVT\Desktop\imgs", @"C:\Users\Administrator.DVT\Desktop\imgs",ImageFormat.Jpeg); 54 55 }

生成?? 拿到Debug 下? 控制臺.exe? 和三個組件(

itextsharp.dll

PDFView.dll

gsdll32.dll

放入web項目中? (必須放在 同一文件夾下)

?

?

在web中調(diào)用方法的地方??? 改寫成?? 調(diào)用控制臺程序
代碼如下:

View Code 1 try2 {3 Process pro = new Process();4 pro.StartInfo.RedirectStandardOutput = true;5 pro.StartInfo.RedirectStandardInput = false;6 //不顯示窗口7 pro.StartInfo.CreateNoWindow = true;8 pro.StartInfo.UseShellExecute = false;9 //要調(diào)用的控制臺程序 10 pro.StartInfo.FileName = Request.MapPath("../FileUpload/ConsoleApplication1.exe"); 11 //給控制臺程序的參數(shù)傳遞值 12 pro.StartInfo.Arguments = filePath+" "+HtmlDic+" "+imgDire; 13 pro.Start(); 14 15 16 string b= pro.StandardOutput.ReadToEnd(); 17 18 pro.WaitForExit(); 19 pro.Close(); 20 pro.Dispose(); 21 result = b; 22 } 23 catch (Exception) 24 { 25 26 throw; 27 }

vs中測試 通過

發(fā)布到iis中 ?的配置:

iis 中 應(yīng)用程序池 ?設(shè)置中 ?啟用 32位應(yīng)用程序

?

?

ok,終于解決了?? (-_-! 調(diào)了兩周)

?最近改功能上線 ? 又發(fā)現(xiàn)些問題...并解決

1,EXCLE文件生成html 預(yù)覽

xlsx文件 生成html后 ?打開html 會有 ?警告 或提示?

導(dǎo)致進(jìn)程卡死(因為再程序中打開 ,無法響應(yīng))

2,docx文件生成html 預(yù)覽 ? 問題同上 、

解決方法:只需設(shè)置 兩個屬性 即可

?? repExcel.DisplayAlerts = false;

word.DisplayAlerts = Word.WdAlertLevel.wdAlertsNone; ??

?

?另外 要做進(jìn)程回收

excel事例:

System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook);workbook = null;GC.Collect();System.Runtime.InteropServices.Marshal.ReleaseComObject(repExcel.Application.Workbooks);GC.Collect();System.Runtime.InteropServices.Marshal.ReleaseComObject(repExcel);repExcel = null;GC.Collect();//根據(jù)時間殺進(jìn)程System.Diagnostics.Process[] process = System.Diagnostics.Process.GetProcessesByName("EXCEL");//依據(jù)時間殺滅進(jìn)程foreach (System.Diagnostics.Process p in process){if (DateTime.Now.Second - p.StartTime.Second > 0 && DateTime.Now.Second - p.StartTime.Second < 5){p.Kill();}}Thread.Sleep(3000);//保證完全關(guān)閉

?

/// <summary>/// 殺掉進(jìn)程/// </summary>/// <param name="hwnd"></param>/// <param name="id"></param>/// <returns></returns>[DllImport("user32.dll")]private static extern int GetWindowThreadProcessId(IntPtr hwnd, out int id);public void killexcel(Excel.Application xlapp){try{IntPtr app = new IntPtr(xlapp.Hwnd);int processid;GetWindowThreadProcessId(app, out processid);System.Diagnostics.Process.GetProcessById(processid).Kill();}catch{ }}

?

pdf ?同樣要 回收資源 ? 殺掉進(jìn)程

....................................擦 ? ,貌似解決了。。。

?

轉(zhuǎn)載于:https://www.cnblogs.com/DamonTang/archive/2012/12/04/2801333.html

總結(jié)

以上是生活随笔為你收集整理的c# asp.net Pdf 转换图片 在线预览 发布到iis中问题 最终解决篇—_—!的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。