C# 将PDF与ITextSharp合并需要时间

C# 将PDF与ITextSharp合并需要时间,c#,pdf,itextsharp,C#,Pdf,Itextsharp,我正在使用ITextSharp合并PDF。 我的问题是,当我合并巨大的PDF时,需要很长时间(很多分钟)。似乎在“document.close()”上花费了所有的时间 这是我的密码: iTextSharp.text.Document doc = new iTextSharp.text.Document(); PdfCopy copy = new PdfCopy(doc, msOutput); copy.SetMergeFields(); doc.Open(); byte[] byteArra

我正在使用ITextSharp合并PDF。 我的问题是,当我合并巨大的PDF时,需要很长时间(很多分钟)。似乎在“document.close()”上花费了所有的时间

这是我的密码:

iTextSharp.text.Document doc = new iTextSharp.text.Document();

PdfCopy copy = new PdfCopy(doc, msOutput);
copy.SetMergeFields();

doc.Open();
byte[] byteArray = Convert.FromBase64String("someString");

PdfReader reader = new PdfReader(byteArray);
copy.AddDocument(reader);

doc.Close(); // <== It takes time here !
byte[] form = msOutput.ToArray();
iTextSharp.text.Document doc=new iTextSharp.text.Document();
PdfCopy copy=新的PdfCopy(单据、msOutput);
copy.SetMergeFields();
doc.Open();
byte[]byteArray=Convert.FromBase64String(“someString”);
PdfReader reader=新PdfReader(byteArray);
副本。添加文件(读卡器);
doc.Close();// 您缺少一些Close()调用-这可能有助于缩短您的时间:

byte[] form
using (var msOutput = new MemoryStream())
{
    iTextSharp.text.Document doc = new iTextSharp.text.Document();
    byte[] byteArray = Convert.FromBase64String("someString");

    PdfCopy copy = new PdfCopy(doc, msOutput);

    copy.SetMergeFields();

    doc.Open();

    PdfReader reader = new PdfReader(byteArray);

    copy.AddDocument(reader);

    reader.Close();
    copy.Close();

    doc.Close(); 

    form = msOutput.ToArray();
}

您还应确保在使用后正确处理流。

代码中的“读取器”是什么?它总是很长,还是越来越长(例如,这可能是处理你的阅读器等问题吗?我已经更新了我的代码。在巨大的PDF上总是需要时间(例如,我尝试合并3个PDF,每个36页)36页并不是那么大。它们的文件大小是否很大?你能分享有问题的pdf吗?这可能是里面的表单问题。不幸的是,我不能真正分享它,因为它是保密的。但是pdf包含一个有很多字段(acrofields)的表单。谢谢,我会尝试一下。我实际上使用了“using”对于我的MemoryStream,当我试图关闭阅读器或副本或两者时,它会抛出一个错误“它已经关闭”。