简体   繁体   中英

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

 /// <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

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
  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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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