iTextSharp가 있습니다 내가
PDFWriter를가하여 FileStream을 기반으로합니다 (각 행 후) 여러 지점에서 세척 및 전체 압축 시도했습니다
코드는 거의 다음과 같습니다 PdfPTable
이 구현하는 ILargeElement
이라는 매우 멋진 인터페이스입니다. 문서에 따르면
/**
* Interface implemented by Element objects that can potentially consume
* a lot of memory. Objects implementing the LargeElement interface can
* be added to a Document more than once. If you have invoked setCompleted(false),
* they will be added partially and the content that was added will be
* removed until you've invoked setCompleted(true);
* @since iText 2.0.8
*/
것은 그래서 당신이해야 할 모든 당신이 당신의 PdfPTable
을 만든 후이며, false로 Complete
속성을 설정합니다. 내부 루프에서는 매번 한 번씩 테이블을 추가하여 메모리를 지우는 카운터의 일부 형식을 수행합니다. 그런 다음 루프 끝에 Complete
을 true로 설정하고 한 번 더 추가하십시오.
다음은이를 보여주는 샘플 코드입니다. 카운터 검사를하지 않으면이 코드는 내 컴퓨터에서 약 500MB의 RAM을 사용합니다. 카운터가 1,000 개의 항목을 모두 확인하면 16MB RAM으로 줄어 듭니다. 당신은 카운터에 대한 자신의 달콤한 자리를 찾아야하고 그것은 평균적으로 각 셀에 얼마나 많은 텍스트를 추가하는지에 달려 있습니다.
string fileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "table.pdf");
Document document = new Document();
FileStream stream = new FileStream(fileName, FileMode.Create);
var pdfWriter = PdfWriter.GetInstance(document, stream);
document.Open();
PdfPTable table = new PdfPTable(4);
table.Complete = false;
for (int i = 0; i < 1000000; i++) {
PdfPCell cell = new PdfPCell(new Phrase(i.ToString()));
table.AddCell(cell);
if (i > 0 && i % 1000 == 0) {
Console.WriteLine(i);
document.Add(table);
}
}
table.Complete = true;
document.Add(table);
document.Close();
코드를 공유 할 수 있습니까? –