Ggalla1779
Ggalla1779

Reputation: 476

Threading a word routine to convert doc to PDF

Am new to threading and have started a 2 threads application, it runs a basic Doc to PDF conversion method. The client has Word 2003 files to convert.

However the code runs it seems to open a word instance to screen, it did open a box and progress bar before I threaded it.

Should I be handling word in a different way now?

Am trying to utlise the processor by running more than 1 thread and speed up converting 30000 doc files.

Am not looking to use any 3rd party tools, just word as from posts on the site Word is the best at conversion.

Main

 MyThread thr1 = new MyThread();
 MyThread thr2 = new MyThread();

 Thread tid1 = new Thread(new ThreadStart(thr1.Thread1));
 Thread tid2 = new Thread(new ThreadStart(thr2.Thread1));

 tid1.Name = "Thread 1";
 tid2.Name = "Thread 2";

 tid1.Start();
 tid2.Start();

Thread1 Code

     Word.Application word = new Microsoft.Office.Interop.Word.Application();

     // C# doesn't have optional arguments so we'll need a dummy value
     object oMissing = System.Reflection.Missing.Value;

     // Get list of Word files in specified directory
     DirectoryInfo dirInfo = new DirectoryInfo(@"C:\ConvertToPDF\Docs");
     FileInfo[] wordFiles = dirInfo.GetFiles("*.doc");

     Thread thr = Thread.CurrentThread;

     if (thr.Name == "Thread 1")
        {
        var orderedSort = wordFiles.OrderBy(f => f.CreationTime);
        }
     else
        {
        var orderedSort = wordFiles.OrderByDescending(f => f.CreationTime);
        }

     word.Visible = false;
     word.ScreenUpdating = false;

     foreach (FileInfo wordFile in wordFiles)
        {
       // Cast as Object for word Open method
       Object filename = (Object)wordFile.FullName;

      // Use the dummy value as a placeholder for optional arguments
      Document doc = word.Documents.Open(ref filename, ref oMissing,
        ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
        ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
        ref oMissing, ref oMissing, ref oMissing, ref oMissing);
      doc.Activate();

      object outputFileName = wordFile.FullName.Replace(".doc", ".pdf");
      object fileFormat = WdSaveFormat.wdFormatPDF;

      // Save document into PDF Format
      doc.SaveAs(ref outputFileName,
                ref fileFormat, ref oMissing, ref oMissing,
                ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                ref oMissing, ref oMissing, ref oMissing, ref oMissing);

      object saveChanges = WdSaveOptions.wdDoNotSaveChanges;
         ((_Document)doc).Close(ref saveChanges, ref oMissing, ref oMissing);
            doc = null;
        }

        // word has to be cast to type _Application so that it will find
        // the correct Quit method.
        ((_Application)word).Quit(ref oMissing, ref oMissing, ref oMissing);
        word = null;

Upvotes: 1

Views: 986

Answers (2)

Mario
Mario

Reputation: 179

Have a look at the code I used in the answer on the following question:

Wordapp not closing in Thread or Parallel process

Upvotes: 1

Jerod Venema
Jerod Venema

Reputation: 44642

Don't use Word on the server. You're gonna have a bad time.

Use the OpenXML SDK:

https://www.microsoft.com/en-us/download/details.aspx?id=5124

Upvotes: 1

Related Questions