ITextSharp returning same size pdf when I'm trying to compress pdf file with different levels

不想你离开。 提交于 2019-11-28 05:13:39

问题


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?


回答1:


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 called PdfStamperImp to do most of the work. PdfStamperImp is derived from PdfWriter and when you use stamper.Writer you are actually getting back this implementation class. Many of the properties on PdfStamper also pass directly through to the implementation class. So these two calls actually do the same thing.

stamper.SetFullCompression();
stamper.Writer.SetFullCompression();

Another point of confusion is that SetFullCompression and the CompressionLevel 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 the PdfStream class which many classes ultimately derive from. This setting doesn't "trickle down", however. Since you are importing a stream from another location via GetPageContent() and SetPageContent() that specific stream has its own compression settings unrelated to the Writer's compression settings. There's actually a third parameter that you can pass to SetPageContent() to set your specific compression level if you want.

reader.SetPageContent(1, reader.GetPageContent(1), PdfStream.BEST_COMPRESSION);



回答2:


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.

PdfReader reader = new PdfReader("/tmp/before.pdf");
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream("/tmp/temp.pdf"), PdfWriter.VERSION_1_5);

stamper.getWriter().setCompressionLevel(9);
int total = reader.getNumberOfPages() + 1;
for(int i=1;i<total;i++){
    reader.setPageContent(i, reader.getPageContent(i), PdfStream.BEST_COMPRESSION);
}

stamper.setFullCompression();
stamper.close();
reader.close();


来源:https://stackoverflow.com/questions/22019032/itextsharp-returning-same-size-pdf-when-im-trying-to-compress-pdf-file-with-dif

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!