Compress PDF files

17

There is this PDF Compressor site that compresses files from 300k to 90k , was looking for in Google and I could not find anything related to doing in c# .

Does anyone know of an algorithm that does the same thing in C# ?

Use of libs only open source.

    
asked by anonymous 03.07.2015 / 18:44

5 answers

1

The PDF format itself already has a compression block (by default). To make extra compression, for example SmallPDF goes to all raster images included in it and replace them with images with lower resolution (144dpi by default) that does not look too bad but which is an irreversible lossy compression operation.

In addition we can reduce the number of colors of images raster included (again lossy compression)

This type of intervention has to be well measured! Is this the type of compression you want?

Is it worth using this type of compression?

    
10.07.2015 / 13:51
0

Really without using itextSharp will be complicated, there is another NPOI.XWPF.UserModel library, perhaps this code can help you, in this example the code actually decreases the size of the pages, therefore decreasing the size of the pdf too: / p>

const double scale = 0.5;

using (FileStream inputStream = new FileStream(@"..\..\../inputdocuments/PackingLightBrochure.pdf", FileMode.Open, FileAccess.Read))
{
    // open the source document
    Document documentIn = new Document(inputStream);

    // create the target document
    Document documentOut = new Document();

    // enumerate the pages in the source document
    foreach (Page originalPage in documentIn.Pages)
    {
        // calculate a new size of the page
        double scaledWidth = originalPage.Width * scale;
        double scaledHeight = originalPage.Height * scale;

        // append a scaled version of the original page to the target document
        Page scaledPage = new Page(scaledWidth, scaledHeight);
        documentOut.Pages.Add(scaledPage);

        PageShape pageShape = new PageShape(originalPage, 0, 0, scaledPage.Width, scaledPage.Height);
        scaledPage.VisualOverlay.Add(pageShape);
    }

    // write the target document to disk
    using (FileStream outFile = new FileStream(@"..\..\output.pdf", FileMode.Create, FileAccess.Write))
    {
        documentOut.Write(outFile);
    }
}

I saw this example on this site: link

    
06.07.2015 / 15:13
0

I've never done this, not least because the libraries I use for compression are paid, but there's a Open Source JBIG2 Encoder and Decoder that may be useful for you.

Here are examples of use .

    
06.07.2015 / 16:47
0

You can do this via Ghostscript.

In the example below I'm calling the executable via the command line and passing these parameters.

-sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/printer -dNOPAUSE -dQUIET -dBATCH -sOutputFile=C:\temp\saida.pdf C:\temp\entrada.pdf

You can see the documentation here:

link

    
09.07.2015 / 14:28
0

Try this FreeOpenSource PDF Compressor: link

    
12.07.2015 / 01:27