【问题标题】:Compress PDF files on Windows Server automatically在 Windows Server 上自动压缩 PDF 文件
【发布时间】:2013-05-07 13:37:18
【问题描述】:

我有一个用户 PDF 上传提交系统,而当我们只需要它们用于 Web 大小的查看时,用户通常会上传原本用于打印的非常大尺寸的 PDF。 我需要在服务器上自动压缩它们。我们正在运行 Windows Server 2003。现在,我们只是让用户通过SoftArtisans.FileUp 方法上传 PDF。

我的问题是用户上传 PDF 后在服务器上自动执行此操作的最佳或最简单的方法是什么?

【问题讨论】:

    标签: .net iis pdf asp-classic windows-server-2003


    【解决方案1】:

    为什么不试试这个网站。以前用过,效果不错:

    http://www.neeviapdf.com/PDFcompress/?w=code

    【讨论】:

      【解决方案2】:

      Docotic.Pdf library 可能对您有用。

      如果我理解正确,您愿意减小上传 PDF 文件中图像的大小/质量并应用其他常规压缩选项。

      以下是此类场景的示例代码。代码:

      • 枚举给定文件中的所有图像
      • 缩放这些图像
      • 删除未使用的对象并
      • 针对快速 Web 视图优化 PDF 并将其保存到新文件中

      使用示例中的代码,您应该能够优化上传的文件。

      public static void compressPdf(string path, string outputPath)
      {
          using (PdfDocument doc = new PdfDocument(path))
          {
              foreach (PdfImage image in doc.Images)
              {
                  // image that is used as mask or image with attached mask are
                  // not good candidates for recompression
                  // 
                  // also, small images might be used as mask even though image.IsMask for them is false
                  //
                  if (!image.IsMask && image.Mask == null && image.Width >= 8 && image.Height >= 8)
                  {
                      if (image.ComponentCount == 1)
                      {
                          // for black-and-white images fax compression gives better results
                          image.RecompressWithGroup4Fax();
                      }
                      else
                      {
                          // scale image and recompress it with JPEG compression
                          // this will cause image to be smaller in terms of bytes
                          // please note that some of the image quality will be lost
                          image.Scale(0.5, PdfImageCompression.Jpeg, 65);
                          
                          // or you can just recompress the image without scaling
                          //image.RecompressWithJpeg(65);
                          
                          // or you can resize the image to specific size
                          //image.ResizeTo(640, 480, PdfImageCompression.Jpeg, 65);
                      }
                  }
              }
                  
              var saveOptions = new PdfSaveOptions
              {
                  RemoveUnusedObjects = true,
      
                  // this option causes smaller files but 
                  // such files can only be opened with Acrobat 6 (released in 2003) or newer
                  UseObjectStreams = true,
      
                  // this option will cause output file to be optimized for Fast Web View
                  Linearize = true
              };
              doc.Save(outputPath, saveOptions);
          }
      }
      

      免责声明:我为图书馆的供应商 Bit Miracle 工作。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-12-19
        • 1970-01-01
        • 1970-01-01
        • 2012-03-22
        • 1970-01-01
        • 2013-11-13
        • 2020-02-26
        相关资源
        最近更新 更多