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

关于: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 目录下

上代码:

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  同样要 回收资源   杀掉进程

....................................擦   ,貌似解决了。。。

 

转载于:https://www.cnblogs.com/DamonTang/archive/2012/12/04/2801333.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值