Compressing PDF files

Asked

Viewed 2,046 times

17

Is there such a site PDF compressor which compresses the files of 300k in 90k, I was googling and I couldn’t find anything related to do on c#.

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

Use of libs open source only.

  • 3

    The complex here is that, because these algorithms are of intense commercial interest, it’s hard to find references that teach you how to write this type of code. At least until then I didn’t find any completely open example of C# code for this.

  • I’m also on the hunt for a free library for pdf compression in c# I’m using itextsharp but it doesn’t compress much compared to paid libs

5 answers

1

The PDF format itself already has (by default) a compression block. For extra compression, for example Smallpdf goes to all embedded raster images and replaces them with images with lower resolution (144dpi by default) which does not get too bad but which is an irreversible loss compression operation.

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

This type of intervention must be well measured! It is this type of compression that you want?

Worth using this type of compression?

  • I did tests with the smallPDF and he reduced me PDF's of 300k for 90K, loss of almost imperceptible quality. Files are usually text. So yes it is worth the loss of quality, while what is written in the file is understandable to the user.

  • @Meuchapeu, would you please provide a PDF file pair for us to co-operatively experiment with various hypotheses and in the end make a response of experimental conclusions.

0

Really without using itextSharp will be complicated, there is another library NPOI.XWPF.Usermodel, maybe this code can help you, in this example the code actually decreases the pages size, consequently decreasing the pdf size also:

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: http://www.tallcomponents.com/pdfkit/resize-pdf-pages

  • The Library is paid, has to be open source.

0

0

0

It is possible to do this via Ghostscript.

In the example below I am calling the executable via 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:

http://www.ghostscript.com/doc/9.16/Ps2pdf.htm

Browser other questions tagged

You are not signed in. Login or sign up in order to post.