C # - High memory consumption using iTextSharp

2

I have the following code, the objective is to open a file pdf , calculate the hash of it and keep the file in memory (approximately 20 seconds) to later sign it and save it. What happens, a pdf of 90mb is loaded and when you open it and keep it in memory the consumption goes up to 1.2gb :

entitySing.output = new MemoryStream();
entitySing.sap = PdfStamper.CreateSignature(new PdfReader(entitySing.inputBase64File),
entitySing.output, '
entitySing.output = new MemoryStream();
entitySing.sap = PdfStamper.CreateSignature(new PdfReader(entitySing.inputBase64File),
entitySing.output, '%pre%', null, true).SignatureAppearance;
entitySing.sap.SignatureGraphic = iTextSharp.text.Image.GetInstance(FileProcess.CompanyStampImageByte());
if (entitySing.digitalSign.Visible)
    entitySing.sap.SetVisibleSignature(new iTextSharp.text.Rectangle(float.Parse(entitySing.llx), 
                                       float.Parse(entitySing.lly), float.Parse(entitySing.urx),
                                       float.Parse(entitySing.ury)), 1, null);
', null, true).SignatureAppearance; entitySing.sap.SignatureGraphic = iTextSharp.text.Image.GetInstance(FileProcess.CompanyStampImageByte()); if (entitySing.digitalSign.Visible) entitySing.sap.SetVisibleSignature(new iTextSharp.text.Rectangle(float.Parse(entitySing.llx), float.Parse(entitySing.lly), float.Parse(entitySing.urx), float.Parse(entitySing.ury)), 1, null);

The above is only the part of the pdfreader and pdfstamper that according to the tracking performed are the objects that are carrying the memory.

    
asked by Michael Ospina 03.07.2018 в 18:33
source

1 answer

2

The best way is to load the byte array in the pdfreader in the following way:

                PdfReader myPdfReader = new PdfReader(
                new RandomAccessFileOrArray(byteArray))

and this pdf reader upload it to the following pdfStamper:

                PdfStamper.CreateSignature(myPdfReader,
                myOutputFileStream, '
                MyTempFile = Path.GetTempPath() + Guid.NewGuid().ToString()                                         + ".pdf";
', MyTempFile, true)

and before the reader and the stamper create the temporary file MyTempFile:

                PdfReader myPdfReader = new PdfReader(
                new RandomAccessFileOrArray(byteArray))

Before the process, with a 90mb file, it consumed me from 1.2 to 1.5 gb memory. Now just open the file is 200mb and while signing rises up to 330mb . The improvement is remarkable.

    
answered by 09.07.2018 / 18:29
source