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?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…