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

Syncfusion Essential pdf hangs up in multithreading environment during pdf files creation.

Hi, my company  is going to use your Syncfusion Essential pdf . Out task is to process a big amount of pdf forms in multithreading environmnet, but I came across a weird bug related to generating pdf files in multiple threads. Please see attachment console applicatation where I am trying to save 100 pdf files in one theead and 100 pdf files in multiple threads. One thead method works fine, but multiplethreading method hangs up without any exceptions. As far as I know to be able to work in multithreading environmnet I have to disable PdfDocument.EnableCache = false. I did it but it didn't help. Please provide me instructions how to work with pdf files via Syncfusion Essential pdf  in multithreading environment

Here is my source code:

 private static string PdfOutputPath = @"d:\Temp\1";

        private static string SourcePdfDocumentPath =
            Path.Combine(
                System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location),
                @"Content\TestPdf.pdf");

        static void Main(string[] args)
        {
            PdfDocument.EnableCache = false; // to prevent exceptions im multithreading environment;

            OutputPdfFlattenOneThread(100);
            OutputPdfFlattenMultithreading(100);

            Console.WriteLine("Press any key to exit...");
            Console.ReadKey();
        }

        private static void OutputPdfFlattenOneThread(int documentsCount)
        {
            Console.WriteLine("One thread generating pdf files:");
            Console.WriteLine();

            foreach (var index in Enumerable.Range(0, documentsCount))
            {
                PdfDocument outputDocument = new PdfDocument();
                outputDocument.Form.Flatten = true;

                FileStream sourceStream = new FileStream(SourcePdfDocumentPath, FileMode.Open, FileAccess.Read, FileShare.Read);
                PdfLoadedDocument loadedDocument = new PdfLoadedDocument(sourceStream);
                foreach (PdfPageBase page in loadedDocument.Pages)
                {
                    outputDocument.ImportPage(loadedDocument, page);
                }

                outputDocument.Save(Path.Combine(PdfOutputPath, "oneThread", string.Concat(index, ".pdf")));

                loadedDocument.Close(true);
                outputDocument.Close(true);

                Console.WriteLine("{0}.pdf document has been created", index);
            }

            Console.WriteLine();
            Console.WriteLine();

        }

        private static void OutputPdfFlattenMultithreading(int documentsCount)
        {
            Console.WriteLine("Multiple threads generating pdf files:");

            Enumerable.Range(0, documentsCount).AsParallel().ForAll(index =>
            {
                Console.WriteLine("{0}.pdf document is creating", index);

                PdfDocument outputDocument = new PdfDocument();
                outputDocument.Form.Flatten = true;

                FileStream sourceStream = new FileStream(SourcePdfDocumentPath, FileMode.Open, FileAccess.Read, FileShare.Read);
                PdfLoadedDocument loadedDocument = new PdfLoadedDocument(sourceStream);

                foreach (PdfPageBase page in loadedDocument.Pages)
                {
                    outputDocument.ImportPage(loadedDocument, page);
                }

                outputDocument.Save(Path.Combine(PdfOutputPath, "multipleThreads", string.Concat(index, ".pdf")));

                loadedDocument.Close(true);
                outputDocument.Close(true);
                sourceStream.Close();
            });

            Console.WriteLine();
            Console.WriteLine();
        }


Attachment: SyncFusionPdfFormFlattenBug_7779769e.zip

1 Reply

KK Karthik Krishnaraj Syncfusion Team August 4, 2016 08:41 AM UTC

Hi Andrey, 
We were able to reproduce the issue and we have confirmed this as bug and logged a report on this. Also we have created a support incident under your account to track the status of this issue.                                                    
Please log on to our support website to check for further updates. 
Thanks, 
Karthik. 


Loader.
Live Chat Icon For mobile
Up arrow icon