簡體   English   中英

使用ITextSharp合並PDF需要時間

[英]Merging PDF with ITextSharp takes time

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

我做錯了什么嗎? 我怎樣才能改善合並時間?

您缺少一些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();
}

您還應確保在使用后正確處理流。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM