将PDF与ITextSharp合并需要时间

本文关键字:时间 合并 ITextSharp PDF | 更新日期: 2023-09-27 18:30:13

我正在使用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();

我做错了什么吗?如何提高合并时间?

将PDF与ITextSharp合并需要时间

您错过了一些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();
}

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