zoukankan      html  css  js  c++  java
  • 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  同样要 回收资源   杀掉进程

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

  • 相关阅读:
    Scrapy学习-7-数据存储至数据库
    Scrapy学习-6-JSON数据处理
    Scrapy学习-5-下载图片实例
    Scrapy学习-4-Items类&Pipelines类
    Scrapy学习-3-Request回调巧用
    Scrapy学习-2-xpath&css使用
    iTunes.exe 在win7系统中运行出错解决办法
    [转]手机浏览器的User-Agent汇总
    apple mobile device服务无法启动,错误1053 解决
    [转]EF 4.1 Code First
  • 原文地址:https://www.cnblogs.com/DamonTang/p/2801333.html
Copyright © 2011-2022 走看看