繁体   English   中英

合并PDF和压缩文件.net

[英]Merging PDF and Compressing files .net

我正在尝试使用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;
    }

PdfDocument的实例恰好跨方法使用

请让我知道是否需要修改

谢谢,基兰库马尔

您的代码还可以。 请注意,库占用的内存量与总大小和附加文档的数量成正比。

我建议您不时保存并重新打开文档,以减少库消耗的内存量。 您也可以将以下设置用于中间保存。

outputDocument.SaveOptions.UseObjectStreams = false;

因此,我建议您尝试以下过程:

  1. 打开文件
  2. 追加不超过10个(或其他数量)的文档
  3. 保存文档(中间保存)
  4. 打开您刚刚保存的文档
  5. 追加下一批文件
  6. ...

请注意,即使使用建议的过程,当前版本的库也可能导致内存不足异常。

暂无
暂无

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

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