C# tutorial: PDF compression


PDF compression

In iTextSharp, you can use the CompressionLevel property of the PdfWriter class to set the compression level of stream objects that are written to the FileStream. You can specify one compression option from the table below.

Option

Percentage reduced (in file size)

Compression level 0 (no compression) 0%
Default compression 72.30%
Compression level 9 ( best compression) 72.59%
Full compression 77.41%

The compression level 0, default compression, and compression level 9 can be specified by using constant values defined in the PdfStream class: NO_COMPRESSION (0), DEFAULT_COMPRESSION (-1) , and BEST_COMPRESSION (9). You will use the SetFullCompression method of the PdfWriter class to use the full compression option.

var doc = new Document();
PdfWriter writer=PdfWriter.GetInstance(doc, new FileStream(path + "pdfdoc.pdf", FileMode.Create));
writer.CompressionLevel = PdfStream.DEFAULT_COMPRESSION;
Image img = Image.GetInstance("D:/baby.jpg");
doc.Open();
doc.Add(img);
doc.Add(new Paragraph(0,"Hello World",new Font(Font.FontFamily.COURIER,50,Font.BOLD)));
doc.Close();

Compressing and decompressing an existing PDF file

The following code helps you compress an existing PDF document. You can use the GetPageContent method to get the content stream of a page, and the SetPageContent method to put it back. By doing this, iTextSharp thinks the stream has changed, and it will use the compression level that was defined for writer object of the PdfStamper.

PdfReader reader = new PdfReader("D:/pdfcontentadded.pdf");
PdfStamper stamper = new PdfStamper(reader, new FileStream("d:/pdfdoccompressed.pdf",FileMode.Create), PdfWriter.VERSION_1_5);
int pageNum = reader.NumberOfPages;
for (int i = 1; i <= pageNum; i++)
{
reader.SetPageContent(i, reader.GetPageContent(i));
}
stamper.SetFullCompression();
stamper.Close();

This is the Pdf file before being compressed.

pdf compression

This is the Pdf file after being compressed.

pdf file compressed

To decompress a PDF file, you will set the compress level of the writer object of PdfStamper to NO_COMPRESSION or zero.

PdfReader reader = new PdfReader("D:/pdfcompressed.pdf");
PdfStamper stamper = new PdfStamper(reader, new FileStream("d:/pdfdocdecompressed.pdf", FileMode.Create), PdfWriter.VERSION_1_5);
stamper.Writer.CompressionLevel = 0;
stamper.Close();


Comments

Brijesh comment

 Brijesh

private void button1_Click(object sender, EventArgs e)
{
// string password="xyz";
const string originalFile = @"C:\Users\administrator\Desktop\pdf_open_parameters.pdf";
const string compressedFile = @"C:\Users\administrator\Desktop\pdf_open_parametersNewSize.pdf";



using (PdfDocument pdf = new PdfDocument(originalFile))
{
pdf.SaveOptions.Compression = PdfCompression.Flate;
pdf.SaveOptions.UseObjectStreams = true;
pdf.SaveOptions.RemoveUnusedObjects = true;
pdf.SaveOptions.OptimizeIndirectObjects = true;
pdf.SaveOptions.WriteWithoutFormatting = true;

pdf.Save(compressedFile);
}

string message = string.Format("Original file size: {0} bytes;\r\nCompressed file size: {1} bytes",
new FileInfo(originalFile).Length,
new FileInfo(compressedFile).Length
);
MessageBox.Show(message);

Process.Start(compressedFile);
}
From:--brijeshnakrani33@gmail.com


2017-03-23
Rajat Agrawal comment

 Rajat Agrawal

Hello,
I used Compressing an existing PDF file code to compress my pdf file size of 2446 KB. After executing the code the pdf file generated with size of 2491 KB.. Please help me out and below is the code-

string pdffilename = "D:/pdf/List and Detail Screens Exercise.pdf";
string pdffilenamecompress = "D:/pdf/Compress.pdf";

PdfReader reader = new PdfReader(pdffilename);
PdfStamper stamper = new PdfStamper(reader, new FileStream(pdffilenamecompress, FileMode.Create), PdfWriter.VERSION_1_5);
int pageNum = reader.NumberOfPages;

for (int i = 1; i <= pageNum; i++)
{
reader.SetPageContent(i, reader.GetPageContent(i));
}

stamper.SetFullCompression();
bool test = stamper.FullCompression;
stamper.Close();


2016-11-29
JonyGreen comment

 JonyGreen

I'm not a developer, i always use this free online pdf to image converter(http://www.online-code.net/pdf-to-image.html)


2015-10-09



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.