Multithreading in Word to PDF conversion

24 Jul 202622 minutes to read

This page covers multithreading and thread-safety support in Word to PDF conversion.

Does the Word library support multithreading and thread-safety for Word to PDF conversion?

Yes, the .NET Word Library is thread-safe, and you can create multiple instances of the WordDocument class to load the same file as multiple copies or different Word files, then convert them to PDF using multithreading in C#.

Frequently asked questions about multithreading in Word to PDF

The following code example illustrates how to use multithreading to convert multiple copies of a Word document to PDF by creating multiple tasks in C#.

class MultiThreading
{
    // Indicates the number of threads to be created.
    private const int TaskCount = 1000;
    public static async Task Main()
    {
        // Create an array of tasks based on the TaskCount.
        Task[] tasks = new Task[TaskCount];
        for (int i = 0; i < TaskCount; i++)
        {
            tasks[i] = Task.Run(() => ConvertWordToPDF());
        }
        // Ensure all tasks complete by waiting on each task.
        await Task.WhenAll(tasks);
    }

    // Convert a Word document to PDF using multi-threading.
    static void ConvertWordToPDF()
    {
        using (FileStream inputStream = new FileStream("Input.docx", FileMode.Open, FileAccess.Read))
        {
            // Load an existing Word document.
            using (WordDocument document = new WordDocument(inputStream, FormatType.Docx))
            {
                // Create an instance of DocIORenderer.
                using (DocIORenderer renderer = new DocIORenderer())
                {
                    // Convert Word document to PDF.
                    using (PdfDocument pdfDocument = renderer.ConvertToPDF(document))
                    {
                        // Save the PDF document.
                        using (FileStream outputFileStream = new FileStream("Output" + Guid.NewGuid().ToString() + ".pdf", FileMode.Create, FileAccess.Write))
                        {
                            pdfDocument.Save(outputFileStream);
                        }
                    }
                }
            }
        }
    }
}
class MultiThreading
{
    // Indicates the number of threads to be created.
    private const int TaskCount = 1000;
    public static async Task Main()
    {
        // Create an array of tasks based on the TaskCount.
        Task[] tasks = new Task[TaskCount];
        for (int i = 0; i < TaskCount; i++)
        {
            tasks[i] = Task.Run(() => ConvertWordToPDF());
        }
        // Ensure all tasks complete by waiting on each task.
        await Task.WhenAll(tasks);
    }

    // Convert a Word document to PDF using multi-threading.
    static void ConvertWordToPDF()
    {
        using (FileStream inputStream = new FileStream("Input.docx", FileMode.Open, FileAccess.Read))
        {
            // Load an existing Word document.
            using (WordDocument document = new WordDocument(inputStream, FormatType.Docx))
            {
                // Create an instance of DocToPDFConverter.
                using (DocToPDFConverter converter = new DocToPDFConverter())
                {
                    //Convert Word document to PDF.
                    PdfDocument pdfDocument = converter.ConvertToPDF(document);
                    //Save the PDF document.
                    using (FileStream outputFileStream = new FileStream("Output" + Guid.NewGuid().ToString() +".pdf", FileMode.Create, FileAccess.Write))
                    {
                        pdfDocument.Save(outputFileStream);
                    }
                }
            }
        }
    }
}
Module MultiThreading
    ' Indicates the number of threads to be created.
    Private Const TaskCount As Integer = 1000
    Public Sub Main()
        ' Create an array of tasks based on the TaskCount.
        Dim tasks(TaskCount - 1) As Task
        For i As Integer = 0 To TaskCount - 1
            tasks(i) = Task.Run(Sub() ConvertWordToPDF())
        Next
        ' Ensure all tasks complete by waiting on each task.
        Task.WhenAll(tasks).Wait()
    End Sub

    ' Convert a Word document to PDF using multi-threading.
    Private Sub ConvertWordToPDF()
        Using inputStream As New FileStream("Input.docx", FileMode.Open, FileAccess.Read, FileShare.Read)
            ' Load an existing Word document.
            Using document As New WordDocument(inputStream, FormatType.Docx)
                ' Create an instance of DocToPDFConverter.
                Using converter As New DocToPDFConverter()
                    ' Convert Word document to PDF.
                    Using pdfDocument As PdfDocument = converter.ConvertToPDF(document)
                        ' Save the PDF document.
                        Using outputFileStream As New FileStream("Output" & Guid.NewGuid().ToString() & ".pdf", FileMode.Create, FileAccess.Write)
                            pdfDocument.Save(outputFileStream)
                        End Using
                    End Using
                End Using
            End Using
        End Using
    End Sub
End Module

You can download a complete working sample from GitHub.

Note: When running many concurrent tasks, wrap the Task.WhenAll call in a try/catch to handle AggregateException and inspect individual task exceptions as needed.

Multithreading Word to PDF using parallel process

The following code example illustrates how to use a parallel for loop to process multiple tasks concurrently, converting Word documents to PDF using multithreading in C#.

class MultiThreading
{
    static void Main(string[] args)
    {
        // Indicates the number of threads to be created.
        int limit = 5;
        Console.WriteLine("Parallel For Loop");
        Parallel.For(0, limit, count =>
        {
            Console.WriteLine("Task {0} started", count);
            // Convert multiple Word documents, one document on each thread.
            ConvertWordToPDF(count);
            Console.WriteLine("Task {0} is done", count);
        });
    }

    // Convert a Word document to PDF using multi-threading.
    static void ConvertWordToPDF(int count)
    {
        using (FileStream inputStream = new FileStream("Input.docx", FileMode.Open, FileAccess.Read))
        {
            // Load an existing Word document.
            using (WordDocument document = new WordDocument(inputStream, FormatType.Docx))
            {
                // Create an instance of DocIORenderer.
                using (DocIORenderer renderer = new DocIORenderer())
                {
                    // Convert Word document to PDF.
                    PdfDocument pdfDocument = renderer.ConvertToPDF(document);
                    //Save the PDF document.
                    using (FileStream outputFileStream = new FileStream("Output" + count + ".pdf", FileMode.Create, FileAccess.Write))
                    {
                        pdfDocument.Save(outputFileStream);
                    }
                    // Dispose pdf to free resources.
                    pdfDocument.Dispose();
                }
            }
        }
    }
}
class MultiThreading
{
    static void Main(string[] args)
    {
        // Indicates the number of threads to be created.
        int limit = 5;
        Console.WriteLine("Parallel For Loop");
        Parallel.For(0, limit, count =>
        {
            Console.WriteLine("Task {0} started", count);
            // Convert multiple Word documents, one document on each thread.
            ConvertWordToPDF(count);
            Console.WriteLine("Task {0} is done", count);
        });
    }

    // Convert a Word document to PDF using multi-threading.
    static void ConvertWordToPDF(int count)
    {
        using (FileStream inputStream = new FileStream("Input.docx", FileMode.Open, FileAccess.Read))
        {
            // Load an existing Word document.
            using (WordDocument document = new WordDocument(inputStream, FormatType.Docx))
            {
                // Create an instance of DocToPDFConverter.
                using (DocToPDFConverter converter = new DocToPDFConverter())
                {
                    // Convert Word document to PDF.
                    using (PdfDocument pdfDocument = converter.ConvertToPDF(document))
                    {
                        // Save the PDF document.
                        using (FileStream outputFileStream = new FileStream("Output" + count + ".pdf", FileMode.Create, FileAccess.Write))
                        {
                            pdfDocument.Save(outputFileStream);
                        }
                    }
                }
            }
        }
    }
}
Module MultiThreading
    Public Sub Main(args As String())
        ' Indicates the number of threads to be created.
        Dim limit As Integer = 5
        Console.WriteLine("Parallel For Loop")
        Parallel.For(0, limit, Sub(count)
                                   Console.WriteLine("Task {0} started", count)
                                   ' Convert multiple Word documents, one document on each thread.
                                   ConvertWordToPDF(count)
                                   Console.WriteLine("Task {0} is done", count)
                               End Sub)
    End Sub

    ' Convert multiple Word documents to PDF using multi-threading.
    Private Sub ConvertWordToPDF(count As Integer)
        Using inputStream As New FileStream("Input.docx", FileMode.Open, FileAccess.Read, FileShare.Read)
            ' Load an existing Word document.
            Using document As New WordDocument(inputStream, FormatType.Docx)
                ' Create an instance of DocToPDFConverter.
                Using converter As New DocToPDFConverter()
                    ' Convert Word document to PDF.
                    Using pdfDocument As PdfDocument = converter.ConvertToPDF(document)
                        ' Save the PDF document.
                        Using outputFileStream As New FileStream("Output" & count.ToString() & ".pdf", FileMode.Create, FileAccess.Write)
                            pdfDocument.Save(outputFileStream)
                        End Using
                    End Using
                End Using
            End Using
        End Using
    End Sub
End Module

You can download a complete working sample from GitHub.

See also