当合并多个Pdf文件时,Pdfsharp内存不足异常 [英] Pdfsharp Out of Memory Exception when Combine Multi Pdf File
问题描述
我必须将大量(但未定义的)pdf转换成一个pdf,我正在使用代码PDFsharp。
I have to convert into a single pdf a large number (but undefined) pdf into one for this, I'm using the code PDFsharp here.
// Get some file names
string[] files = filesToPrint.ToArray();
// Open the output document
PdfDocument outputDocument = new PdfDocument();
PdfPage newPage;
int nProcessedFile = 0;
int nMemoryFile = 5;
int nStepConverted = 0;
String sNameLastCombineFile = "";
// Iterate files
foreach (string file in files)
{
// Open the document to import pages from it.
PdfDocument inputDocument = PdfReader.Open(file, PdfDocumentOpenMode.Import);
// Iterate pages
int count = inputDocument.PageCount;
for (int idx = 0; idx < count; idx++)
{
// Get the page from the external document...
PdfPage page = inputDocument.Pages[idx];
// ...and add it to the output document.
outputDocument.AddPage(page);
}
nProcessedFile++;
if (nProcessedFile >= nMemoryFile)
{
//nProcessedFile = 0;
//nStepConverted++;
//sNameLastCombineFile = "ConcatenatedDocument" + nStepConverted.ToString() + " _tempfile.pdf";
//outputDocument.Save(sNameLastCombineFile);
//outputDocument.Close();
}
}
// Save the document...
const string filename = "ConcatenatedDocument1_tempfile.pdf";
outputDocument.Save(filename);
// ...and start a viewer.
Process.Start(filename);
对于代码工作的少量文件,然后在某些时候
生成一个例外内存不足
For small numbers of files the code works but then at some point generates an exception of out of memory
是否有解决方案?
ps
我正在考虑保存文件一步一步然后剩下的那个人就这么谎言,但我找不到方式。
p.s I was thinking of saving the files in step and then the remaining aggiungingere so liebrare memory but I can not find the way.
更新1:
if (nProcessedFile >= nMemoryFile)
{
nProcessedFile = 0;
//nStepConverted++;
sNameLastCombineFile = "ConcatenatedDocument" + nStepConverted.ToString() + " _tempfile.pdf";
outputDocument.Save(sNameLastCombineFile);
outputDocument.Close();
outputDocument = PdfReader.Open(sNameLastCombineFile,PdfDocumentOpenMode.Modify);
}
更新2版本1.32
完整示例
行:
PdfDocument inputDocument = PdfReader.Open(file,PdfDocumentOpenMode.Import);
UPDATE 2 versione 1.32 Complete example Error on line: PdfDocument inputDocument = PdfReader.Open(file, PdfDocumentOpenMode.Import);
文本错误:
<强>不能处理iref流。目前PDFsharp的实现无法处理Acrobat 6引入的PDF功能。
using PdfSharp.Pdf;
using PdfSharp.Pdf.IO;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
List<String> filesToPrint = new List<string>();
filesToPrint = Directory.GetFiles(@"D:\Downloads\RACCOLTA\FILE PDF", "*.pdf").ToList();
// Get some file names
string[] files = filesToPrint.ToArray();
// Open the output document
PdfDocument outputDocument = new PdfDocument();
PdfPage newPage;
int nProcessedFile = 0;
int nMemoryFile = 5;
int nStepConverted = 0;
String sNameLastCombineFile = "";
try
{
// Iterate files
foreach (string file in files)
{
// Open the document to import pages from it.
PdfDocument inputDocument = PdfReader.Open(file, PdfDocumentOpenMode.Import);
// Iterate pages
int count = inputDocument.PageCount;
for (int idx = 0; idx < count; idx++)
{
// Get the page from the external document...
PdfPage page = inputDocument.Pages[idx];
// ...and add it to the output document.
outputDocument.AddPage(page);
}
nProcessedFile++;
if (nProcessedFile >= nMemoryFile)
{
nProcessedFile = 0;
//nStepConverted++;
sNameLastCombineFile = "ConcatenatedDocument" + nStepConverted.ToString() + " _tempfile.pdf";
outputDocument.Save(sNameLastCombineFile);
outputDocument.Close();
inputDocument = PdfReader.Open(sNameLastCombineFile , PdfDocumentOpenMode.Modify);
}
}
// Save the document...
const string filename = "ConcatenatedDocument1_tempfile.pdf";
outputDocument.Save(filename);
// ...and start a viewer.
Process.Start(filename);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.ReadKey();
}
}
}
}
UPDATE3
生成异常内存的代码
UPDATE3 Code that generate exception out of memory
int count = inputDocument.PageCount;
for (int idx = 0; idx < count; idx++)
{
// Get the page from the external document...
newPage = inputDocument.Pages[idx];
// ...and add it to the output document.
outputDocument.AddPage(newPage);
newPage.Close();
}
我不能确定哪一行一般例外
I can not exactly which row general exception
推荐答案
保存并关闭 outputDocument
(代码已在您的代码段中注释掉),您必须打开 outputDocument
再次使用 PdfDocumentOpenMode.Modify
。
After saving and closing outputDocument
(the code is commented out in your snippet), you have to open outputDocument
again, using PdfDocumentOpenMode.Modify
.
它可以帮助为 inputDocument
使用(...)添加。
It could help to add using(...)
for the inputDocument
.
如果您的代码以32位进程运行,则切换到64位将允许您的进程使用超过2 GB的RAM(假设您的计算机具有超过2 GB的RAM)。
If your code is running as a 32-bit process, then switching to 64 bit will allow your process to use more than 2 GB of RAM (assuming your computer has more than 2 GB RAM).
更新:消息无法处理iref流意味着您必须使用PDFsharp 1.50 Prerelease,可在NuGet上使用。
Update: The message "Cannot handle iref streams" means you have to use PDFsharp 1.50 Prerelease, available on NuGet.
这篇关于当合并多个Pdf文件时,Pdfsharp内存不足异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!