iTextSharp的回归同样大小的PDF时,我试图压缩与不同层次的PDF文件 [英] ITextSharp returning same size pdf when I'm trying to compress pdf file with different levels

查看:977
本文介绍了iTextSharp的回归同样大小的PDF时,我试图压缩与不同层次的PDF文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我阅读的PDF和注射用iTextSharp的一些内容。由此产生的字节[] 被传递到下面的方法随着压缩级别。

 公共静态的byte []的方法(字节[] PDF,INT compressionlevel)使用(MemoryStream的outputPdfStream1 =新的MemoryStream())
{

{
// PdfReader reader1 =新PdfReader(PDF);
// PdfStamper stamper1 =新PdfStamper(reader1,outputPdfStream1);
// INT水平=(INT)compressionlevel;
//如果(水平< = 9)
// stamper1.Writer.CompressionLevel =(INT)compressionlevel;
//别的
// stamper1.Writer.SetFullCompression();
//stamper1.SetFullCompression();
//stamper1.Close();
//字节[] =的newfile outputPdfStream1.ToArray();
//返回的newfile;

PdfReader读卡器=新PdfReader(PDF);
PdfStamper模子=新PdfStamper(读卡器,outputPdfStream1,PdfWriter.VERSION_1_5);
级INT =(INT)compressionlevel;
如果(水平< = 9)
{
stamper.Writer.CompressionLevel =水平;
}
,否则
stamper.Writer.SetFullCompression();
INT总= reader.NumberOfPages + 1;
的for(int i = 1; I<全;我++)
{
reader.SetPageContent(I,reader.GetPageContent(I));
}
stamper.SetFullCompression();
stamper.Close();
字节[] =的newfile outputPdfStream1.ToArray();
回报率的newfile;
}
}

如果我评论模子。 SetFullCompression(); ,则此方法将返回字节数组的大小相同不管压缩级别想过去(我从0传递给9在每种情况下)。



如果我取消 stamper.SetFullCompression(); 此方法将返回完全压缩无关的压缩级别输入字节的版本!



什么是 stamper.SetFullCompression(); 和 stamper.Writer.SetFullCompression ();?



什么是使用不同的压缩级别,这样我可以在每一种情况下看到不同尺寸的正确方法。


解决方案

您有几个问题,我会尽我所能回答。



PdfStamper 是最终使用名为 PdfStamperImp 来完成大部分的工作的另一个类的辅助类。 PdfStamperImp PdfWriter 导出,当你使用 stamper.Writer 你实际上又回到这个实现类。许多在 PdfStamper 属性还通过向实现类直接传递。所以这两个调用实际上做同样的事情。



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



混乱的另一点是, SetFullCompression 和在 CompressionLevel 实际上并没有在所有有关。 全压表示在所谓的对象流PDF 1.5,它允许分组PDF对象在一起,可能允许更大的压缩增加了一个功能。实际上,有我们所认为的压缩究竟是什么发生没有要求,但在现实中,我认为它会经常发生。 (可能是一个超级简单的文件可能会得到较大的与此启用,不知道和不喜欢的测试。)



CompressionLevel 其实就是你平常觉得作为压缩,数字0〜9或-1意味着默认值(当前为六点钟,我认为)。此属性其实是类很多类最终从推导 PdfStream的一部分。此设置不涓滴效应,然而。既然你是通过 GetPageContent进口从另一个位置流() SetPageContent()特定流具有其自身的压缩设置无关作家的压缩设置。实际上,有可以传递给 SetPageContent()来设置,如果你想你的具体的压缩级别。



<$第三个参数p $ p> reader.SetPageContent(1,reader.GetPageContent(1),PdfStream.BEST_COMPRESSION);


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 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);

这篇关于iTextSharp的回归同样大小的PDF时,我试图压缩与不同层次的PDF文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆