【问题标题】:Merging PDF and Compressing files .net合并 PDF 和压缩文件 .net
【发布时间】:2017-01-02 00:33:34
【问题描述】:

我正在尝试使用 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 的实例恰好跨方法使用

如果有什么需要修改的请告诉我

谢谢, 基兰库马尔

【问题讨论】:

  • 请提供有关您的流程的更多信息。你的进程是32位还是64位?您使用的是哪个版本的 .NET Framework?你合并了多少个文件?
  • 它是 64 位机器,.net 4.6.1,文件数从 1 到 200 不等,每个文件最多 5 页

标签: pdf docotic.pdf bitmiracle


【解决方案1】:

您的代码没问题。请注意,库消耗的内存量与总大小和附加文档的数量成正比。

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

outputDocument.SaveOptions.UseObjectStreams = false;

所以,我建议你尝试以下过程:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-07
    • 2010-12-19
    • 2011-12-30
    • 1970-01-01
    • 2012-08-11
    • 1970-01-01
    • 2011-05-19
    • 2014-06-30
    相关资源
    最近更新 更多