Compress PDF files on Windows Server automatically

2019-08-11 10:01发布

I have a user PDF upload submission system whereas quite often users will upload very large sized PDF's that have been originally meant for Print when we only need them for web sized viewing. I need to automatically compress them on the server. We are running Windows Server 2003. Right now, we just have user upload PDF via SoftArtisans.FileUp method.

My question is what is the best or easiest way to do this on the server automatically after user uploads PDF?

2条回答
叛逆
2楼-- · 2019-08-11 10:32

Why not try this site. Used it before and it works OK:

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

查看更多
何必那么认真
3楼-- · 2019-08-11 10:41

Docotic.Pdf library may be useful in your case.

If I understand you right, you are willing to reduce size / quality of images in uploaded PDF files and apply other general compression options.

Below is a sample code for such scenario. The code:

  • enumerates all images in a given file
  • scales those images
  • removes unused objects and
  • optimizes PDF for Fast Web View and saves it to new file

Using a code like in the sample you should be able to optimize uploaded files.

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);
                }
            }
        }

        doc.SaveOptions.RemoveUnusedObjects = true;

        // this option causes smaller files but 
        // such files can only be opened with Acrobat 6 (released in 2003) or newer
        doc.SaveOptions.UseObjectStreams = true;

        // this option will cause output file to be optimized for Fast Web View
        doc.SaveOptions.Linearize = true;

        doc.Save(outputPath);
    }
}

Disclaimer: I work for Bit Miracle, the vendor of the library.

查看更多
登录 后发表回答