How to convert (doc -> pdf) a large number of files efficiently ?

APIPointNewbie 146 Reputation points
2021-10-11T12:54:35.567+00:00

Hello Community,

I would like to convert a large number of files (+40) as efficiently as possible with the MS Graph SDK. So far I iterate through the file ids and pass them to the necessary methods:

    private async Task<Stream> ConvertFiles(GraphServiceClient graphClient, string id)
    {
        var queryOptions = new List<QueryOption>()
    {
        new QueryOption("format", "pdf")
    };
        var stream = await graphClient.Drives[DriveId]
                                      .Items[id]
                                      .Content
                                      .Request(queryOptions)
                                      .GetAsync();

        return stream;
    }

    private async Task SaveFilesInOneDrive(GraphServiceClient graphClient, Stream stream, string fileName)
    {
        await graphClient.Drives[DriveId]
                         .Items[PdfItemId].ItemWithPath(StringUtils.BuildNameForAPdfFile(fileName))
                         .Content
                         .Request()
                         .PutAsync<DriveItem>(stream);
    }

But my approach does not seem so efficient, is there anything to speed up the process ?

I would be very pleased to receive feedback.

Greetings :-)

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
12,767 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,189 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. CarlZhao-MSFT 44,671 Reputation points
    2021-10-12T09:27:39.3+00:00

    As an idea, you can try to use batch processing to convert multiple doc files to pdf files in a single call request.

    ============

    Update on 10.19

    Added a doc reference about using Microsoft Graph to batch requests, you can have a try.
    https://learn.microsoft.com/en-us/graph/sdks/batch-requests?tabs=csharp

    --------------------------------

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. David 151 Reputation points
    2021-10-15T08:57:44.817+00:00

    Hi, you may try Spire.Doc for .NET, which is a good alternative when doing the file format conversion.

    using System;
    using Spire.Doc;
    using Spire.Doc.Documents;
    
    namespace DoctoPDF
    {
        class toPDF
        {
            static void Main(string[] args)
            {
                //Load Document
                Document document = new Document();
                document.LoadFromFile(@"E:\work\documents\TestSample.docx");
    
                //Convert Word to PDF
                document.SaveToFile("toPDF.PDF", FileFormat.PDF);
    
            }
        }
    }
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.