文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>ASP.NET 多媒体电子报刊设计(一)

ASP.NET 多媒体电子报刊设计(一)

时间:2011-02-28  来源:iceknp

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
using System.Diagnostics;

namespace FlexPaperExample.WebDemo
{
    /// <summary>
    /// PDF帮助类
    /// </summary>
    public class PDFHelper
    {
        /// <summary>
        /// 转换图片
        /// </summary>
        /// <param name="InputFile">源文件完整路径</param>
        /// <param name="deletePDF">是否删除</param>
        /// <param name="filename">目标完整路径</param>
        /// <returns></returns>
        public static IList<string> GenerateThumbnailImage(string InputFile, bool deletePDF, string filename)
        {
            return GenerateImage(InputFile, deletePDF, filename, "-dSAFER -dBATCH -dNOPAUSE -r50 -sDEVICE=jpeg -dGraphicsAlphaBits=4");
        }

        /// <summary>
        /// 转换图片
        /// </summary>
        /// <param name="InputFile">源文件完整路径</param>
        /// <param name="deletePDF">是否删除PDF</param>
        /// <param name="filename">目标完整路径</param>
        /// <param name="Arguments">参数设置</param>
        /// <returns></returns>
        private static IList<string> GenerateImage(string InputFile, bool deletePDF, string filename, string Arguments)
        {
            IList<string> result = new List<string>();
            int PDFPageCount = GetPDFPageCount(InputFile);
            if (PDFPageCount == 0)
            {
                return result;
            }

            string OutputFile = filename;

            string extOut = Path.GetExtension(OutputFile);
            string partOut = OutputFile.Remove(OutputFile.Length - extOut.Length, extOut.Length);

            if (PDFPageCount == 1)
            {
                OutputFile = partOut + ".jpg";
                result.Add(OutputFile);
                if (File.Exists(OutputFile))
                {
                    File.Delete(OutputFile);
                }
            }

            else
            {
                for (int i = 0; i < PDFPageCount; i++)
                {
                    string eachFileName = partOut + "_" + (i + 1).ToString() + ".jpg";
                    result.Add(eachFileName);
                    if (File.Exists(eachFileName))
                    {
                        File.Delete(eachFileName);
                    }
                }

                OutputFile = OutputFile.Remove(OutputFile.Length - extOut.Length, extOut.Length);
                OutputFile += "_%d.jpg";
            }

            ProcessStartInfo info = new ProcessStartInfo();
            info.CreateNoWindow = true;
            info.WindowStyle = ProcessWindowStyle.Hidden;
            info.WorkingDirectory = System.Configuration.ConfigurationManager.AppSettings["GhostScriptView"];
            info.Arguments = Arguments + @" -sOutputFile=" + OutputFile + "  " + InputFile;
            info.FileName = @"gswin32c.exe";
            Process subProcess = new Process();
            subProcess.StartInfo = info;
            subProcess.Start();
            subProcess.WaitForExit(int.MaxValue);
            if (deletePDF)
            {
                System.IO.File.Delete(InputFile);
            }
            return result;
        }

        /// <summary>
        /// 获取pdf文件的页数
        /// </summary>
        /// <param name="path">文件路径</param>
        /// <returns></returns>
        private static int GetPDFPageCount(string strPath)
        {
            FileStream fs = new FileStream(strPath, FileMode.Open, FileAccess.Read);
            StreamReader r = new StreamReader(fs);
            string pdfText = r.ReadToEnd();

            Regex rx1 = new Regex(@"/Type\s*/Page[^s]");
            MatchCollection matches = rx1.Matches(pdfText);

            r.Close();
            r.Dispose();
            fs.Close();
            fs.Dispose();

            return matches.Count;
        }
    }
}

使用方法非常简单,调用GenerateThumbnailImage方法,其会返回一个IList泛型集合,里面装的是转换之后的图片完整路径集合。

若有疑问或不正之处,欢迎提出指正和讨论。

相关阅读 更多 +
排行榜 更多 +
找茬脑洞的世界安卓版

找茬脑洞的世界安卓版

休闲益智 下载
滑板英雄跑酷2手游

滑板英雄跑酷2手游

休闲益智 下载
披萨对对看下载

披萨对对看下载

休闲益智 下载