设为首页 收藏本站
查看: 1083|回复: 0

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

[复制链接]

尚未签到

发表于 2015-8-12 12:23:36 | 显示全部楼层 |阅读模式
  关于:excel和word 预览 请看我的博文:
  excel和word 在线预览  详细配置及代码
  
  使用Adobe 组件 在本机vs中调试成功
  发布到iis中  在  代码中涉及到  剪贴板的地方  会不成功  莫名其妙的没有数据   也不报错      设置iis权限 、com组件权限+各种搞    均无果.....    -_-!  很是郁闷   
  
  最终放弃
  改用 Ghostscript  
  须安装  gs861w32.exe  (高版本 貌似 还有问题......)
  在安装目录 bin 下 找到gsdll32.dll
  下在 dll
  itextsharp.dll
  PDFView.dll
  把三个dll放入 项目dll(新建)文件夹中   
  引入
  itextsharp.dll
  PDFView.dll
  gsdll32.dll无法引入   拷贝到项目bin 目录下
  上代码:


DSC0000.gif DSC0001.gif View Code


1  /// <summary>
2         /// 将PDF 相应的页转换为图片
3         /// </summary>
4         /// <param name="strPDFpath">PDF 路径</param>
5         /// <param name="Page">需要转换的页页码</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             // 获得文档页数
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 运行  成功
  发布到服务器iis 中   设置 iis 对应应用程序池   启用32为应用程序  为true
  -_-! 报错
  System.InvalidOperationException: 当应用程序不是以 UserInteractive 模式运行时显示模式对话框或窗体是无效操作。请指定 ServiceNotification 或 DefaultDesktopOnly 样式,以显示服务应用程序发出的通知。
  
  
  
  换一种调用方法    :在web中调用外部的控制台程序
  将上面方法 放入控制台程序中
  mian函数如下:


View Code


1 [STAThread]
2         static void    Main(string[] args)
3         {
4             #region MyRegion
5             /*
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("无参数");
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("无参数");
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中调用方法的地方    改写成   调用控制台程序
代码如下:


View Code


1   try
2                 {
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                     //要调用的控制台程序
10                     pro.StartInfo.FileName = Request.MapPath("../FileUpload/ConsoleApplication1.exe");
11                     //给控制台程序的参数传递值
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中测试 通过
  发布到iis中  的配置:
  iis 中 应用程序池  设置中  启用 32位应用程序
  
  
  ok,终于解决了   (-_-! 调了两周)
  最近改功能上线   又发现些问题...并解决
  1,EXCLE文件生成html 预览
  xlsx文件 生成html后  打开html 会有  警告 或提示
  导致进程卡死(因为再程序中打开 ,无法响应)
  2,docx文件生成html 预览   问题同上 、
  解决方法:只需设置 两个属性 即可
  repExcel.DisplayAlerts = false;
  word.DisplayAlerts = Word.WdAlertLevel.wdAlertsNone;   
  
  另外 要做进程回收
  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();
//根据时间杀进程
System.Diagnostics.Process[] process = System.Diagnostics.Process.GetProcessesByName("EXCEL");//依据时间杀灭进程
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);//保证完全关闭
  



    /// <summary>
/// 杀掉进程
/// </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  同样要 回收资源   杀掉进程
  ....................................擦   ,貌似解决了。。。
  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-97925-1-1.html 上篇帖子: 微软IIS服务器的最佳优化工具- IIS Tuner 下篇帖子: ASP.NET页面与IIS底层交互和工作原理详解(一)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表