简体   繁体   English

ITextSharp合并多个pdf的内存不足异常

[英]ITextSharp Out of memory exception merging multiple pdf

I have to merge multiple 1 page pdf's into one pdf. 我必须将多个1页pdf合并为一个pdf。 I'm using iTextSHarp 5.5.5.0 to accomplish this, but when I get to merge more than 900-1000 pdf I get an out of memory exception. 我正在使用iTextSHarp 5.5.5.0来实现这一目标,但是当我合并超过900-1000 pdf时,我得到一个内存不足异常。 I noticed that even if I free my reader and close it the memory never gets cleaned properly (the amount of memory used by the process never decreases)so I was wondering what I could possibly be doing wrong. 我注意到,即使我释放我的阅读器并关闭它,内存永远不会被正确清理(进程使用的内存量永远不会减少)所以我想知道我可能做错了什么。 This is my code: 这是我的代码:

 using (MemoryStream msOutput = new MemoryStream())
        {
            Document doc = new Document();
            PdfSmartCopy pCopy = new PdfSmartCopy(doc, msOutput);
            doc.Open();
            foreach (Tuple<string, int> file in filesList)
            {
                PdfReader pdfFile = new PdfReader(file.Item1);
                for (int j = 0; j < file.Item2; j++)
                    for (int i = 1; i < pdfFile.NumberOfPages + 1; i++)//in this case it's always 1. 
                        pCopy.AddPage(pCopy.GetImportedPage(pdfFile, i));
                pCopy.FreeReader(pdfFile);
                pdfFile.Close();
                File.Delete(file.Item1);
            }
            pCopy.Close();
            doc.Close();

            byte[] content = msOutput.ToArray();
            using (FileStream fs = File.Create(Out))
            {
                fs.Write(content, 0, content.Length);
            }
        }

It never gets to writing the file, I get an out of memory exception during the p.Copy().AddPage() part. 它永远不会写入文件,我在p.Copy()期间得到一个内存不足异常.AddPage()部分。 I even tried flushing the pCopy variable but didn't change anything. 我甚至尝试刷新pCopy变量,但没有改变任何东西。 I looked in the documentation of iText and various questions around StackOverflow but seems to me that I'm taking every suggestion to keep memory usage low, but this isn't happening. 我查看了iText的文档以及围绕StackOverflow的各种问题,但在我看来,我正在采取一切建议来保持较低的内存使用率,但这种情况并没有发生。 Any ideas on this? 有什么想法吗?

Since this is a large amount of stuff I'd recommend writing directly to a FileStream instead of a MemoryStream . 由于这是大量的东西,我建议直接写入FileStream而不是MemoryStream This might be an actual case where an Out of Memory Exception might literally mean "Out of Memory". 这可能是一个实际情况,其中内存异常可能实际上意味着“内存不足”。

Also, as Bruno pointed out, the "smart" part of PdfSmartCopy unfortunately comes at the cost of memory, too. 此外,正如Bruno指出的那样,不幸的是, PdfSmartCopy的“智能”部分也是以内存为代价的。 Switching to PdfCopy should reduce memory pressure although your final PDF might be larger. 切换到PdfCopy应该会降低内存压力,尽管最终的PDF可能会更大。

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

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