Open PDF file from Google Drive

13 Dec 20233 minutes to read

To Open a PDF file from Google Drive, you can follow the steps below

Step 1: Set up Google Drive API

You must set up a project in the Google Developers Console and enable the Google Drive API. Obtain the necessary credentials to access the API. For more information, view the official link.

Step 2: Create a simple console application

Project configuration window

Step 3: Install the Google.Apis.Drive.v3 NuGet packages as a reference to your project from the NuGet.org.

NuGet package installation

Step 4: Include the following namespaces in the Program.cs file.

using Google.Apis.Auth.OAuth2;
    using Google.Apis.Drive.v3;
    using Google.Apis.Services;
    using Google.Apis.Util.Store;

Step 5: Add the below code example to open a PDF from google drive.

UserCredential credential;
    string[] Scopes = { DriveService.Scope.DriveReadonly };
    string ApplicationName = "YourAppName";

    using (var stream1 = new FileStream("credentials.json", FileMode.Open, FileAccess.Read))
    {
        string credPath = "token.json";
        credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
            GoogleClientSecrets.Load(stream1).Secrets,
            Scopes,
            "user",
            CancellationToken.None,
            new FileDataStore(credPath, true)).Result;
    }

    // Step 2: Create Drive API service
    var service = new DriveService(new BaseClientService.Initializer()
    {
        HttpClientInitializer = credential,
        ApplicationName = ApplicationName,
    });

    // Step 3: Specify the file ID of the PDF you want to open
    string fileId = "YOUR_FILE_ID"; // Replace with the actual file ID YOUR_FILE_ID

    // Step 4: Download the PDF file from Google Drive
    var request = service.Files.Get(fileId);
    var stream = new MemoryStream();
    request.Download(stream);

    // Step 5: Open the PDF with Syncfusion
    //PdfLoadedDocument loadedDocument = new PdfLoadedDocument(stream);

    // Use the loadedDocument for further processing (e.g., extracting text or images)

    // Remember to dispose of the loadedDocument when you're done
    //loadedDocument.Close(true);

    // Step 5: Save the PDF locally
    using (FileStream fileStream = new FileStream("output.pdf", FileMode.Create, FileAccess.Write))
    {
        stream.WriteTo(fileStream);
    }

You can download a complete working sample from GitHub.