公司最近要求我写过.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文件 } }