locked
How to compress pdf file size through window 8 app?

    Question

  • Hi,

        I am creating an app in window 8 through which I want to decrease the size of any selected pdf file. Can any one guide me how I can do this through c#. Is it possible to achieve this task? 

    Monday, October 27, 2014 9:21 AM

Answers

  • There is no built-in API for changing PDF files. You would need to use some library (and most PDF libraries are paid) in order to extract the images, compress them based on their file type (e.g. using PNG Gauntlet or something similar) and reinsert them into the PDF.

    So in short: yes, this can be implemented using C# but no, there is no system functionality that will help you there. You will have to build the logic to do this yourself.

    Wednesday, October 29, 2014 11:10 AM

All replies

  • Hi Anand,

    Could you simply zip the PDF files and then unzip them? This is a piece of code for zipping files. Unzipping is similar:

    // pdfFile is your unzipped file as StorageFile
    StorageFile zippedFile = await ZipFile(pdfFile, "document.pdf")
    
    ...
    
    private async Task<StorageFile> ZipFile(StorageFile sourceFile, string destFilename)
    {
        StorageFile zipFile = await ApplicationData.Current.LocalFolder.CreateFileAsync(destFilename, CreationCollisionOption.OpenIfExists);
    
        using (IRandomAccessStream writeStream = await zipFile.OpenAsync(FileAccessMode.ReadWrite))
        {
            using (GZipStream zipStream = new GZipStream(writeStream.AsStreamForWrite(), CompressionMode.Compress))
            {
                using (IRandomAccessStream readStream = await sourceFile.OpenReadAsync())
                {
                    Stream stream = readStream.GetInputStreamAt(0).AsStreamForRead();
                    stream.CopyTo(zipStream);
                }
            }
        }
    
        return zipFile;
    }

    Leszek


    My Apps




    • Edited by ata6502 Monday, October 27, 2014 1:29 PM
    Monday, October 27, 2014 1:25 PM
  • Hi,

       Thanks for your reply but I don't have to zip the file. I need to reduce the size of pdf file. Is it possible any how through c# or any apis?

    Wednesday, October 29, 2014 9:43 AM
  • There is no built-in API for changing PDF files. You would need to use some library (and most PDF libraries are paid) in order to extract the images, compress them based on their file type (e.g. using PNG Gauntlet or something similar) and reinsert them into the PDF.

    So in short: yes, this can be implemented using C# but no, there is no system functionality that will help you there. You will have to build the logic to do this yourself.

    Wednesday, October 29, 2014 11:10 AM