I'm reading a pdf and injecting some content using itextsharp. The resulting byte[]
is passed to the method below along with the compression level.
public static byte[] method(byte[] pdf,int compressionlevel)
{
using (MemoryStream outputPdfStream1 = new MemoryStream())
{
//PdfReader reader1 = new PdfReader(pdf);
//PdfStamper stamper1 = new PdfStamper(reader1, outputPdfStream1);
//int level = (int)compressionlevel;
//if (level <= 9)
// stamper1.Writer.CompressionLevel = (int)compressionlevel;
//else
// stamper1.Writer.SetFullCompression();
//stamper1.SetFullCompression();
//stamper1.Close();
//byte[] newfile = outputPdfStream1.ToArray();
//return newfile;
PdfReader reader = new PdfReader(pdf);
PdfStamper stamper = new PdfStamper(reader, outputPdfStream1,PdfWriter.VERSION_1_5);
int level = (int)compressionlevel;
if (level <= 9)
{
stamper.Writer.CompressionLevel = level;
}
else
stamper.Writer.SetFullCompression();
int total = reader.NumberOfPages + 1;
for (int i = 1; i < total; i++)
{
reader.SetPageContent(i, reader.GetPageContent(i));
}
stamper.SetFullCompression();
stamper.Close();
byte[] newfile = outputPdfStream1.ToArray();
return newfile;
}
}
If I comment stamper.SetFullCompression();
then this method is returning same size of byte array irrespective of the compression level am passing(am passing from 0 to 9 in each case)..
If I uncomment stamper.SetFullCompression();
this method is returning fully compressed version of the input byte irrespective of the compression level!!!
What is the purpose/difference of stamper.SetFullCompression();
and stamper.Writer.SetFullCompression();
?
What is the correct way to use different compression levels so that I can see different sizes in each case?
You have a couple of questions and I'll try my best to answer them.
PdfStamper
is a helper class that ultimately uses another class calledPdfStamperImp
to do most of the work.PdfStamperImp
is derived fromPdfWriter
and when you usestamper.Writer
you are actually getting back this implementation class. Many of the properties onPdfStamper
also pass directly through to the implementation class. So these two calls actually do the same thing.Another point of confusion is that
SetFullCompression
and theCompressionLevel
aren't actually related at all. "Full compression" represents a feature that was added in PDF 1.5 called "Object Streams" that allows grouping PDF objects together to potentially allow for greater compression. There's actually no requirement that what we think of as "compression" actually occurs but in reality I think it would always happen. (Possibly a super simple document might get larger with this enabled, not sure and don't feel like testing.)The
CompressionLevel
is actually what you normally think of as compression, a number from 0 to 9 or -1 to mean default (which currently equals six I think). This property is actually part of thePdfStream
class which many classes ultimately derive from. This setting doesn't "trickle down", however. Since you are importing a stream from another location viaGetPageContent()
andSetPageContent()
that specific stream has its own compression settings unrelated to theWriter
's compression settings. There's actually a third parameter that you can pass toSetPageContent()
to set your specific compression level if you want.I am using the below code and it is not getting compressed. Output file is also having the same size as that of input size.