我必须将一个大量(但未定义的)pdf转换成一个pdf到一个,我在这里使用代码PDFsharp。
// 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);
对于少量文件,代码可以工作,但在某些时候 生成内存不足的例外
有解决方案吗?
P.S 我正在考虑将步骤中的文件保存起来,然后剩余的agiungingere这样的谎言记忆,但我无法找到方法。
UPDATE1:
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);
文字错误: 无法处理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 生成异常内存的代码
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();
}
我不能确切地说哪一行一般异常
答案 0 :(得分:0)
保存并关闭while (!multiplicities.isEmpty() && iterator.hasNext())
后(代码已在您的代码段中注释掉),您必须使用TRANSACTION
再次打开outputDocument
。
可能有助于为outputDocument
添加PdfDocumentOpenMode.Modify
。
如果您的代码是作为32位进程运行的,那么切换到64位将允许您的进程使用超过2 GB的RAM(假设您的计算机具有超过2 GB的RAM)。
更新:消息&#34;无法处理iref流&#34;意味着您必须使用NuSet上提供的PDFsharp 1.50 Prerelease。
答案 1 :(得分:0)
我有一个类似的问题,保存,关闭和重新打开PdfDocument并没有真正帮助。
我将大量(100+)大(最多5Mb)图像(tiff,jpg等)添加到pdf文档中,其中每个图像都有自己的页面。它在图像#50周围坠毁。在保存 - 关闭 - 重新打开之后,它确实完成了整个文档,但仍然接近最大内存,大约3Gb。一些更多的图像,它仍然会崩溃。
经过更多的改进,我实现了对XGraphics对象的使用,它再好一点但不多。
向前迈出的一大步是在循环中处理XImage!之后应用程序从未使用超过100-200Kb,我删除了PdfDocument的save-close-reopen,这没问题。