简体   繁体   English

合并PDF和压缩文件.net

[英]Merging PDF and Compressing files .net

i am trying merge and compress PDF files using bitmiracle.docotic.pdf library(trial version) and for a merged file of size 700MB i am facing "out of memory exception", below is my code 我正在尝试使用bitmiracle.docotic.pdf库(试用版)合并和压缩PDF文件,对于大小为700MB的合并文件,我正面临“内存不足异常”,下面是我的代码

 /// <summary>
    /// Open file for copy.
    /// </summary>
    /// <param name="file">File name.</param>
    /// <param name="outputDocument">Output pdf document.</param>
    private void OpenFileForCopy(string file, PdfDocument outputDocument)
    {
        if (isFirstFile)
        {
            outputDocument.Open(file);
        }
        else {
            outputDocument.Append(file);
        }
    }

    /// <summary>
    /// Saves PDF merged pdf file to location specified.
    /// </summary>
    /// <param name="outputDocument">Merged pdf document.</param>
    /// <param name="path">Path to be stored.</param>
    /// <param name="source">Source of file.</param>
    /// <param name="Number">Number.</param>
    private string[] SavePDF(PdfDocument outputDocument, string path, string source, string Number)
    {
        string[] result = new string[2];
        outputDocument.PageLayout = PdfPageLayout.SinglePage;
        string newPath = path + "_" + "Merged";
        string nor= Number.Substring(1);
        Directory.CreateDirectory(newPath);
        newPath += "\\Stmt" + source + nor+ ".pdf";
        outputDocument.SaveOptions.Compression = BitMiracle.Docotic.Pdf.PdfCompression.Flate;
        outputDocument.SaveOptions.UseObjectStreams = true;
        outputDocument.SaveOptions.RemoveUnusedObjects = true;
        outputDocument.SaveOptions.OptimizeIndirectObjects = false;
        outputDocument.SaveOptions.WriteWithoutFormatting = true;

        outputDocument.Save(newPath);
        outputDocument.Dispose();
        isFirstFile = true;
        result[0] = source ;
        result[1] = Convert.ToString(fileCount);
        fileCount = 0;
        return result;
    }

The instance of PdfDocument happens to be used across methods PdfDocument的实例恰好跨方法使用

Kindly let me know if anything needs to modified 请让我知道是否需要修改

Thanks, Kirankumar 谢谢,基兰库马尔

Your code is ok. 您的代码还可以。 Jut please note that amount of memory the library consumes is proportional to total size and number of appended documents. 请注意,库占用的内存量与总大小和附加文档的数量成正比。

I would recommend you to save and re-open documents once in a while to reduce amount of memory consumed by the library. 我建议您不时保存并重新打开文档,以减少库消耗的内存量。 You can also use the following setting for intermediate saves. 您也可以将以下设置用于中间保存。

outputDocument.SaveOptions.UseObjectStreams = false;

So, I propose you to try the following process: 因此,我建议您尝试以下过程:

  1. Open document 打开文件
  2. Append no more than 10 (or other number) documents 追加不超过10个(或其他数量)的文档
  3. Save document (intermediate save) 保存文档(中间保存)
  4. Open the document you just saved 打开您刚刚保存的文档
  5. Append next batch of documents 追加下一批文件
  6. ... ...

Please note that current version of the library can lead to out of memory exceptions even when the proposed process is used. 请注意,即使使用建议的过程,当前版本的库也可能导致内存不足异常。

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

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