I try to merge 2 big PDF files without loading them fully in memory.
I tried with a PdfMerger and manually without a PdfMerger thanks to this kind of code :
using(var writer = new PdfWriter(new FileStream(@"C:\Test\OutBig.pdf",FileMode.OpenOrCreate)))
using (var outputDocument = new PdfDocument(writer)) {
using (var inputDoc = new PdfDocument(new PdfReader((@"C:\Test\InBig.pdf")))) {
for (int i = 1; i <= inputDoc.GetNumberOfPages(); i++) {
var newp = outputDocument.AddNewPage();
var canvas = new PdfCanvas(newp);
var origPage = inputDoc.GetPage(i);
var copy = origPage.CopyAsFormXObject(outputDocument);
canvas.AddXObject(copy, 0, 0);
copy.Flush();
origPage = null;
canvas.Release();
newp.Flush();
writer.Flush();
canvas = null;
newp = null;
}
}
The code is working but every page is loaded in memory and stay loaded, and I consequently have more than 1GB loaded in memory.
Do you know any way to merge 2 pdfs files without loading them in memory with itext7 ?
Regards,
Patrice
I have experimented with a few components now (
Aspose
,ITextSharp
andTelerik
) and Telerik seemed to have cracked it.I followed these steps and the memory remained low.
Sample Code
ITextSharp
Aspose
Telerik