首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Windows服务器上PDF文件的自动压缩

Windows服务器上PDF文件的自动压缩
EN

Stack Overflow用户
提问于 2013-05-07 13:37:18
回答 2查看 2.6K关注 0票数 1

我有一个用户PDF上传提交系统,而通常情况下,用户会上传非常大的PDF,这是最初打算打印时,我们只需要他们的网页大小的查看。我需要在服务器上自动压缩它们。我们正在运行Windows 2003。现在,我们只有用户通过SoftArtisans.FileUp方法上传PDF。

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-05-08 14:25:35

为什么不试试这个网站。以前使用过它,它可以正常工作:

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

票数 1
EN

Stack Overflow用户

发布于 2013-05-09 08:26:34

在您的情况下,Docotic.Pdf库可能很有用。

如果我明白你的意思,你愿意减少上传PDF文件中图像的大小/质量,并应用其他通用压缩选项。

下面是此类场景的示例代码。守则:

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

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

代码语言:javascript
运行
复制
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工作,图书馆的供应商。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16420500

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档