1

I found many samples on the web, one of these:

var oldFile = @"C:\Users\XXX\Downloads\3921B2014901_2014_7346.pdf";
var newFile = @"C:\Users\XXX\Downloads\new\3921B2014901_2014_7346.pdf";

RandomAccessFileOrArray raf = new RandomAccessFileOrArray(oldFile);
PdfReader reader = new PdfReader(raf, null);
using (FileStream fs = new FileStream(newFile, FileMode.Create))
{
    PdfStamper stamper = new PdfStamper(reader, fs, PdfWriter.VERSION_1_5);
    PdfWriter writer = stamper.Writer;
    writer.SetPdfVersion(PdfWriter.PDF_VERSION_1_5);
    writer.CompressionLevel = PdfStream.BEST_COMPRESSION;

    for (int i = 1; i <= reader.NumberOfPages; i++)
    {
        reader.SetPageContent(i, reader.GetPageContent(i), PdfStream.BEST_COMPRESSION, true);
    }
    stamper.SetFullCompression();
    stamper.Close();
}
reader.Close();
raf.Close();

The original file is 2.75Mb and the resulting new file is 2.77Mb. Why?

I tried online converters and they reduce the same file to 500Kb. How can I achieve the same result?

JosephA
  • 1,187
  • 3
  • 13
  • 27
Emaborsa
  • 2,360
  • 4
  • 28
  • 50
  • Pdf compression is a tricky matter. Take a look at this question: https://stackoverflow.com/questions/21341130/pdf-compressing-library-tool – LNyarla Jun 09 '17 at 09:42
  • 1
    Well, there is written a lot....but in fact there is nothing useful. – Emaborsa Jun 12 '17 at 10:26
  • "Audit space usage" function of Adobe Acrobat could be really useful to understand what determines your file size, imho it's a good starting point. – LNyarla Jun 13 '17 at 12:19
  • Well, I have to reduce them programmatically...as many website do. – Emaborsa Jun 14 '17 at 05:55

1 Answers1

0
protected void btnCompressPdf_click(object sender, EventArgs e)
{  
    string[] filename= Directory.GetFiles(input.Text, ".jpg");
    string outputPath = MyDirectory;
    if (!System.IO.Directory.Exists(MyDirectory))
        System.IO.Directory.CreateDirectory(MyDirectory);

    foreach (string filename in files)
    {
        ImageCollection images = new ImageCollection();
        images.Add(filename);
        String pdfName = Path.GetFileNameWithoutExtension(file);
        for (int pageNumber = 0; pageNumber < images.Count; pageNumber++)
        {
            images[pageNumber].Save(MyDirectory + "\\" + pageNumber.ToString() + ".jpg");
        }

        string[] files1 = System.IO.Directory.GetFiles(MyDirectory, ".jpg");

        iTextSharp.text.Document myDocument = new iTextSharp.text.Document(iTextSharp.text.PageSize.A4, 0, 0, 0, 0);
        //PdfWriter.GetInstance(myDocument, new System.IO.FileStream(uxOutput.Text + "\\" + pdfName + ".pdf", System.IO.FileMode.Create));
        PdfWriter writer = PdfWriter.GetInstance(myDocument, new System.IO.FileStream(Output.Text + "\\" + pdfName + ".pdf", System.IO.FileMode.Create));
        writer.SetPdfVersion(PdfWriter.PDF_VERSION_1_5);
        writer.CompressionLevel = PdfStream.BEST_COMPRESSION;
        writer.SetFullCompression();
        for (int i = 0; i <= files1.Length - 1; i++)
        {
            if (myDocument != null)
            {
                myDocument.Open();
                AddImageToPDF(myDocument, MyDirectory + "\\" + (i).ToString() + ".jpg");
            }
        }
        myDocument.Close();
        myDocument = null;
        System.IO.File.Delete(MyDirectory + "\\" + ".jpg");
    }
}
pushkin
  • 9,575
  • 15
  • 51
  • 95
Code
  • 679
  • 5
  • 9
  • A short explanation would be great. However, it seems you are creating a new pdf from some jpgs, which is nor really what I asked for... – Emaborsa Apr 10 '20 at 13:50