Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Wed Feb 15, 2023 10:18 pm

i need to convert a certain amount of files, this required so much time if i convert each document..
there is a method that permits to convert a group of files into pdf?
thanks in advance

Simone.Cola
 
Posts: 3
Joined: Wed Feb 15, 2023 10:15 pm

Thu Feb 16, 2023 10:32 am

Hello,

Thanks for your inquiry.
Sorry, there is currently no way to directly convert a large number of Word documents to Pdf file, however, you can put the all Word documents that you want to convert them to Pdf to the same folder, then travel the folder and convert each Word document to Pdf file. I put the relevant code below for your reference.
Code: Select all
static void Main(string[] args)
        {

            string filePath = @"../../data/";
            List<FileInfo> FileList = FindFile(filePath);
         
            foreach (FileInfo File in FileList)
            {
               
                    Document doc = new Document();
                    doc.LoadFromFile(File.FullName);
                string resultName = Path.GetFileNameWithoutExtension(File.FullName);

                doc.SaveToFile(@"../../output/"+ resultName + ".pdf");
             

            }
           
        }



        public static List<FileInfo> FindFile(string sSourcePath)

        {

            List<FileInfo> list = new List<FileInfo>();
     
            DirectoryInfo theFolder = new DirectoryInfo(sSourcePath);

            FileInfo[] thefileInfo = theFolder.GetFiles("*.*", SearchOption.TopDirectoryOnly);

            foreach (FileInfo NextFile in thefileInfo) 
            {
                list.Add(NextFile);

            }

            return list;
        }


Sincerely
Abel
E-iceblue support team
User avatar

Abel.He
 
Posts: 1010
Joined: Tue Mar 08, 2022 2:02 am

Thu Feb 16, 2023 11:37 am

unfortunally this is only another way to convert each document one by one... this dont solve the issue..

mmm...

maybe a sort of thread parallelism? i can convert multiple docs one to one but at the same moment.. there is any way to try this?

im on c# .net 4.7.2

thanks in advance

Simone.Cola
 
Posts: 3
Joined: Wed Feb 15, 2023 10:15 pm

Thu Feb 16, 2023 2:08 pm

maybe i have a kind of solution... not verified yet...

Code: Select all

        public List<Stream> DOCX_to_PDF_multi(List<Stream> list_FromStream, int maxParallel = 10)
        {
            List<Stream> list_ToStream = new List<Stream>();

            IList<System.Threading.Tasks.Task> tasks = new List<System.Threading.Tasks.Task>();

            foreach (Stream fromStream in list_FromStream)
            {
                tasks.Add(
                    System.Threading.Tasks.Task.Factory.StartNew(() =>
                    {
                        try
                        {
                            list_ToStream.Add(DOCX_to_PD2(fromStream));
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.ToString());
                            //continue
                        }
                    })
                );
            }

            System.Threading.Tasks.Task.WaitAll(tasks.ToArray());

            return list_ToStream;
        }

        public Stream DOCX_to_PDF(Stream FromStream)
        {
            Stream ToStream = new MemoryStream() as Stream;
            using (Document document = new Document(FromStream, Spire.Doc.FileFormat.Docx, true))
            {
                document.SaveToStream(ToStream, Spire.Doc.FileFormat.PDF);
            }

            return ToStream;
        }


Simone.Cola
 
Posts: 3
Joined: Wed Feb 15, 2023 10:15 pm

Fri Feb 17, 2023 2:13 am

Hello,

Thanks for your feedback.
Sorry that I misunderstood your requirement, I verified your code and found it to be an effective solution.
If you have any issue, just feel free to contact us.

Sincerely
Abel
E-iceblue support team
User avatar

Abel.He
 
Posts: 1010
Joined: Tue Mar 08, 2022 2:02 am

Return to Spire.PDF

cron