我正在尝试使用bitmiracle.docotic.pdf库(试用版)合并和压缩PDF文件,并且对于大小为700MB的合并文件我正面临&#34;内存异常&#34;,下面是我的代码< / p>
/// <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
答案 0 :(得分:0)
您的代码没问题。请注意,库消耗的内存量与总大小和附加文档数成正比。
我建议您偶尔保存并重新打开文档,以减少库消耗的内存量。您还可以使用以下设置进行中间保存。
outputDocument.SaveOptions.UseObjectStreams = false;
所以,我建议你尝试以下过程:
请注意,即使使用了建议的流程,当前版本的库也可能导致内存不足异常。