首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >合并PDF和压缩文件.net

合并PDF和压缩文件.net
EN

Stack Overflow用户
提问于 2016-08-25 22:10:18
回答 1查看 244关注 0票数 0

我正尝试使用bitmiracle.docotic.pdf库(试用版)合并和压缩PDF文件,对于700MB大小的合并文件,我遇到了“内存不足异常”,以下是我的代码

代码语言:javascript
复制
 /// <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的实例恰好跨方法使用

如果有任何需要修改的地方,请告诉我

谢谢,Kirankumar

EN

回答 1

Stack Overflow用户

发布于 2016-08-26 16:10:59

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

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

代码语言:javascript
复制
outputDocument.SaveOptions.UseObjectStreams = false;

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

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

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39147609

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档