C# 合并多个Pdf文件时出现Pdfsharp内存不足异常
我必须将大量(但未定义的)pdf转换成单个pdf。为此,我在这里使用代码PDFsharpC# 合并多个Pdf文件时出现Pdfsharp内存不足异常,c#,pdf,exception,pdfsharp,C#,Pdf,Exception,Pdfsharp,我必须将大量(但未定义的)pdf转换成单个pdf。为此,我在这里使用代码PDFsharp // Get some file names string[] files = filesToPrint.ToArray(); // Open the output document PdfDocument outputDocument = new PdfDocument(); PdfPage newPage; int nProcessedFile =
// 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);
更新2版本1.32
完整示例
联机错误:
PdfDocument inputDocument=PdfReader.Open(文件,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();
}
}
}
}
使用PdfSharp.Pdf;
使用PdfSharp.Pdf.IO;
使用制度;
使用System.Collections.Generic;
使用系统诊断;
使用System.IO;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
命名空间控制台应用程序1
{
班级计划
{
静态void Main(字符串[]参数)
{
List filesToPrint=新列表();
filesToPrint=Directory.GetFiles(@“D:\Downloads\RACCOLTA\FILE PDF”,“*.PDF”).ToList();
//获取一些文件名
string[]files=filesToPrint.ToArray();
//打开输出文档
PdfDocument outputDocument=新PdfDocument();
PdfPage新页面;
int nProcessedFile=0;
int nMemoryFile=5;
int-nStepConverted=0;
字符串sNameLastCombineFile=“”;
尝试
{
//迭代文件
foreach(文件中的字符串文件)
{
//打开文档以从中导入页面。
PdfDocument inputDocument=PdfReader.Open(文件,PdfDocumentOpenMode.Import);
//迭代页面
int count=inputDocument.PageCount;
for(int idx=0;idx=nMemoryFile)
{
n进程文件=0;
//NSTEP++;
sNameLastCombineFile=“ConcatenatedDocument”+nStepConverted.ToString()+“_tempfile.pdf”;
outputDocument.Save(sNameLastCombineFile);
outputDocument.Close();
inputDocument=PdfReader.Open(sNameLastCombineFile,PdfDocumentOpenMode.Modify);
}
}
//保存文档。。。
const string filename=“ConcatenatedDocument1_tempfile.pdf”;
outputDocument.Save(文件名);
//…并启动一个查看器。
Process.Start(文件名);
}
捕获(例外情况除外)
{
控制台写入线(例如消息);
Console.ReadKey();
}
}
}
}
更新3
在内存外生成异常的代码
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();
}
int count=inputDocument.PageCount;
for(int idx=0;idx
我无法准确确定哪一行常规异常在保存并关闭
outputDocument
(代码在代码段中注释掉)后,您必须使用PdfDocumentOpenMode.Modify再次打开outputDocument
使用(…)
为输入文档添加,可能会有所帮助
如果您的代码以32位进程的形式运行,那么切换到64位将允许您的进程使用超过2GB的RAM(假设您的计算机有超过2GB的RAM)
更新:消息“无法处理iref流”意味着您必须使用NuGet上提供的PDFsharp 1.50预发行版。我遇到了一个类似的问题,保存、关闭和重新打开PdfDocument并没有真正的帮助
我正在将所有(100+以上)大型(高达5Mb)图像(tiff、jpg等)添加到pdf文档中,每个图像都有自己的页面。它在图像50附近坠毁。保存-关闭-重新打开后,它确实完成了整个文档,但仍接近最大内存,大约3Gb。再多一些图像,它仍然会崩溃
经过进一步的改进,我实现了一个对XGraphics对象的使用,它又稍微好了一点,但不是很多
前进的一大步是在循环中处理XImage!在应用程序从未使用超过100-200Kb之后,我删除了PdfDocument的save-close-reopen,这没有问题。我尝试了这段代码,但出现了错误,请参阅update1“Yes”对于“发生了哪个错误”的问题没有有用的答案请提供MCVE。在实践中,同样的例外情况是,如果打几个电话是否合适,请处理?很抱歉延迟,但我生病了。我正在尝试不同的解决方案,但到目前为止我还没有找到任何可以帮助我的方法。我们并不着急。您没有提供MCVE,您没有提供堆栈跟踪,您没有提供详细的错误信息,因此我们现在不能为您做太多。消息“无法处理iref流”意味着您必须使用NuGet上提供的PDFsharp 1.50预发行版。很抱歉,响应太晚,已经病了一段时间。正在使用(XGraphics gfx=XGraphics.FromPdfPage(pdfPage)){//…Code…//Image=XImage.FromFile(file.FullName);//…更多代码…gfx.DrawImage(image,mPositionX,mPositionY,imageWidth,imageHeight);image.Dispose();//…更多代码。
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();
}