簡體   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