.Net 压缩PDF文件,送源码!

    公司最近要求我写过.net程序 压缩PDF ,网上找了很久没发现相关功能的插件。 自己折腾几天重要找到了个迂回的方法。使用DLL组件,Aspose.Pdf。

送上代码,如果你觉得好用,别忘了顶一下。

/// <summary>
        /// PDF压缩
        /// </summary>
        /// <param name="southPath">文件所在路径</param>
        /// <param name="filename">文件名</param>
        public static void PDFCompress(object obj)
        {

            string[] arr = (string[])obj;

            string yasuopath = arr[0] + "压缩版";
            string southPath = arr[0] + "\\";
            string fileName = arr[1];
            ZKAccessHelper.CreateFolder(yasuopath); //压缩文件夹
            yasuopath += "\\";
            if (string.IsNullOrEmpty(southPath) || string.IsNullOrEmpty(fileName))
            {
                return;
            }
            string fullpath = Path.Combine(southPath, fileName);
            //Stopwatch sw = new Stopwatch();
            //sw.Start();
            string xpsFullName = yasuopath + fileName + ".xps";
            try
            {
                Document doc = new Document(fullpath);
                doc.Save(xpsFullName, SaveFormat.Xps);
                doc.Dispose();
                Aspose.Pdf.LoadOptions options = new XpsLoadOptions();
                Aspose.Pdf.Document document = new Aspose.Pdf.Document(xpsFullName, options);
                string tempdf = Path.Combine(yasuopath, fileName);
                document.Save(tempdf);
                document.Dispose();
               // sw.Stop();
               // TimeSpan tw = sw.Elapsed;
              //  File.Move(tempdf, Path.Combine(southPath, fileName + "已压缩:" + tw.Minutes + "分" + tw.Seconds + "秒.pdf"));//修改文件名称

            }
            catch
            {
                //复制文件,不压缩。
                File.Copy(fullpath, Path.Combine(southPath, fileName + "原版.pdf"));

            }
            finally
            {
                File.Delete(xpsFullName); //删除临时xps文件
            }
          
        }

 

转载于:https://www.cnblogs.com/wudicainiao/p/5749380.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值