We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Save multiple PDFs to Stream

I have problem when saving multiple PDFs ( more than 500 ) to Stream and get byte array to export PDF.

Here is the code I am using:

            byte[] ret = null;
            var isLoadedForm = false;
            using (PdfLoadedDocument loadedDocument = new PdfLoadedDocument(template))
            {
                PdfLoadedForm loadedForm = loadedDocument.Form;
                if (loadedForm != null)
                {
                    isLoadedForm = true;
                    foreach (var f in loadedForm.Fields)
                    {
                        if (!(f is PdfLoadedTextBoxField)) continue;
                        var ff = (PdfLoadedTextBoxField)f;
                        string v;
                        if (!data.TryGetValue(ff.Name, out v)) continue;
                        ff.Text = v;
                    }
                    loadedForm.Flatten = true;
                }

                using (var mem = new MemoryStream())
                {                    
                    loadedDocument.Save(mem);                    
                    ret = mem.ToArray();                    
                }
                loadedDocument.Close(true);
            }
            return ret;

I need to run this more than 500 times to get the byte array. But sometimes, loadedDocument.Save(mem) cannot be executed.
If I only run 30 times, this code works well.

Could you please help me to resolve this issue ? 
Thank you so much.


1 Reply

KC Karthikeyan Chandrasekar Syncfusion Team November 26, 2018 06:51 AM

Hi Tu Huynh, 
We have tried the provided code snippet with multiple PDFs (more than 250). It works find in our end and we were unable to reproduce the specified issue. We suspect that this could be the document specific issue. So kindly share us the PDF documents if any. If you have faced any exception in your sample, kindly share the stack trace details too. It will help us to provide the precise solution. 
Regards, 
Karthikeyan  


Loader.
Live Chat Icon For mobile
Up arrow icon