简体   繁体   English

ITextSharp合并的pdf大小增加

[英]ITextSharp merged pdf size increased

The following code is used to merge multiple pdf file into a single file 以下代码用于将多个pdf文件合并为一个文件

public byte[] MergePdfFiles(IEnumerable<byte[]> files)
{
    using (var stream = new MemoryStream())
    {
        var pdfDoc = new iTextSharp.text.Document();
        var pdf = new PdfCopy(pdfDoc, stream);
        pdf.SetMergeFields();

        pdfDoc.Open();

        foreach (var file in files)
        {
            try
            {
                pdf.AddDocument(new PdfReader(file));
            }
            catch (InvalidPdfException ex)
            {
                _loggingServiceClient.Log(LogLevel.Error, ex);
                throw;
            }
        }

        pdfDoc.Close();

        return stream.GetBuffer();
    }
}

The code works fine, BUT the generated file size is almost twice the total size of all merged files. 该代码工作正常,但是生成的文件大小几乎是所有合并文件总大小的两倍。 I try to add some compression instructions such as pdf.CompressionLevel = PdfStream.BEST_COMPRESSION; 我尝试添加一些压缩指令,例如pdf.CompressionLevel = PdfStream.BEST_COMPRESSION; and pdf.SetFullCompression(); pdf.SetFullCompression(); with no success. 没有成功。

Note that doing a simple saveAs in acrobat reader from the generated file result in new file with a decent size (the combined size of every merged files). 请注意,在acrobat Reader中从生成的文件中执行简单的saveAs会导致新文件的大小(每个合并文件的合并大小)相当大。

I also tried other ways to merge file, like using addPage method for example, which end up the same. 我还尝试了其他方法来合并文件,例如使用addPage方法,最终效果相同。

Please give this a try: 请尝试一下:

public byte[] MergePdfFiles(IEnumerable<byte[]> files)
{
    using (var stream = new MemoryStream())
    {
        var pdfDoc = new iTextSharp.text.Document();
        var pdf = new PdfSmartCopy(pdfDoc, stream);
        pdf.SetMergeFields();

        pdfDoc.Open();

        foreach (var file in files)
        {
            try
            {
                pdf.AddDocument(new PdfReader(file));
            }
            catch (InvalidPdfException ex)
            {
                _loggingServiceClient.Log(LogLevel.Error, ex);
                throw;
            }
        }

        pdfDoc.Close();

        return stream.GetBuffer();
    }
}

In case you don't immediately see the difference with your own code: I changed PdfCopy to PdfSmartCopy . 如果您没有立即看到与自己的代码的区别:我将PdfCopy更改为PdfSmartCopy If this doesn't help (along with using full compression), there is very little one can do. 如果这没有帮助(以及使用完全压缩),则几乎无能为力。 The main cause is probably the fact that iText can't combine the font subsets of the different files into one new font (which is one of the tricks used by Acrobat to reduce file size). 主要原因可能是iText无法将不同文件的字体子集组合为一种新字体的事实(这是Acrobat用来减小文件大小的技巧之一)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM