0

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

Bobrovsky
  • 13,789
  • 19
  • 80
  • 130
RayK
  • 41
  • 2
  • 10
  • Please provide more information about your process. Is your process 32bit or 64bit? What version of .NET Framework you are using? How many files do you merge? – Bobrovsky Aug 25 '16 at 18:36
  • its a 64bit machine, .net 4.6.1 and number of files differs from 1 to 200, each file will have max of 5 pages – RayK Aug 25 '16 at 21:07

1 Answers1

1

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.

Bobrovsky
  • 13,789
  • 19
  • 80
  • 130