-5

i want to save pdf file to database if the pdf file is more than 5Mb then database becomes Heavy or Burden for more no.of files.so that i want to decrease the size of Pdf file as less as possible.... i tried the following code but not working.please help to compress Large PDF to Smaller size.for example if PDF size is 2Mb it will compress to 700Kb. i tried so many examples but didn't get output. so please help..

        PdfReader reader = new PdfReader("D:/User Guid for Artificial Skin.pdf");
        PdfStamper stamper = new PdfStamper(reader, new FileStream("d:/pdfdoccompressed.pdf", FileMode.Create), PdfWriter.VERSION_1_5);
        reader.SetPageContent(1, reader.GetPageContent(1));
        int pageNum = reader.NumberOfPages;
        for (int i = 1; i <= pageNum; i++)
        {
            reader.SetPageContent(i, reader.GetPageContent(i));
        }
        stamper.FormFlattening = true;
        stamper.Writer.CompressionLevel = PdfStream.BEST_COMPRESSION;
        stamper.SetFullCompression();
        stamper.Close();
  • 3
    What makes you think you can compress the PDF even further? – CodeCaster May 31 '16 at 10:55
  • sorry, i am not able to understand what u said – Varahala Babu May 31 '16 at 10:57
  • 1
    You say you have an example file of 1 MB, and that you want this file to be compressed to 300 KB. Why do you have the expectation that this is possible? Does other software do the same on the same file? Do you know how compression works? – CodeCaster May 31 '16 at 10:58
  • this is my requirement in my application.i don't know about other software's, but i want to compress as less as possible with C# – Varahala Babu May 31 '16 at 11:03
  • 1
    i am new to stackoverflow please give me suggestion how to i find the answer for this.how to call Mr.Pied Piper or Mr.Jan Sloot – Varahala Babu May 31 '16 at 11:11
  • PDF is already compressed. Re-cursive compressing leads to diminishing returns –  May 31 '16 at 11:16
  • 2
    @Moumit I now have asked the OP various times **why** they think they can compress 1 MB of PDF into 300 KB. If all they can answer is "it's my requirement", then Stack Overflow is not the place for them to get help. You telling them to zip the data and then unzip it is even less helpful. You cannot losslessly compress a compressed PDF any further. – CodeCaster May 31 '16 at 11:19
  • @Moumit I completely agree with CodeCaster. The OP's requirement is doubtful and also isn't listening. He wasn't disrepectful –  May 31 '16 at 11:21
  • 1
    @VarahalaBabu .. I hope the conversion above now can help you .. to rethink on requirement ... – Moumit May 31 '16 at 11:27
  • i want to save pdf file to database if the pdf file is more than 5Mb then database becomes Heavy or Burden for more no.of files.so that i want to decrease the size of Pdf file as less as possible – Varahala Babu May 31 '16 at 12:00

1 Answers1

0

The point with lossless compression is that there's an end to how much you can compress data. When looking sec at the file as a container and applying a generic compression algorithm on it, you'll not deflate the file by much, as they're saved quite optimally by default already.

Very simplified: PDF files can generally only be made significantly smaller when they contain many unused (embedded) objects such as fonts and form fields, and unoptimized images. Any optimizer you find will simply drop the unused objects, and minify those images by saving them in either less dots per inch ("smaller resolution"), less bits per pixel ("smaller bitdepth") or both.

So by passing PdfStream.BEST_COMPRESSION to PdfStamper, you're already doing everything you can. You simply cannot trivially and significantly compress the PDF much more than PdfStamper already does.

However, from your comments and edits it simply seems you're afraid that in the future this will hurt your database (even though it's designed to contain data, and a lot of it). But that concern is not concrete enough for us to help you with.

So see any of the many previous discussions on whether you actually should store your data like that:

And many, many others.

Community
  • 1
  • 1
CodeCaster
  • 147,647
  • 23
  • 218
  • 272