public PdfDocument GetPdfDocuments(List <File> Files) { var outputDoc = new PdfDocument(); var pFor = Parallel.ForEach(Files, file => { FilesResource.ExportRequest request = service.Files.Export(file.Id, "application/pdf"); var stream = new System.IO.MemoryStream(); // Add a handler which will be notified on progress changes. // It will notify on each chunk download and when the // download is completed or failed. request.MediaDownloader.ProgressChanged += (IDownloadProgress progress) => { switch (progress.Status) { case DownloadStatus.Downloading: { Console.WriteLine(progress.BytesDownloaded); break; } case DownloadStatus.Completed: { Console.WriteLine("Download complete."); var n = PdfReader.Open(stream, PdfDocumentOpenMode.Import); var page1 = n.Pages[0]; var cropped = new PdfDocument(); cropped.AddPage(page1); using (var ms = new MemoryStream()) { cropped.Save(ms); cropped.Close(); Docs.Add(new DocWName { Name = file.Name, Doc = stream.ToArray() }); } //outputDoc.AddPage(page1); //var n = new PdfDocument(new PdfReader(stream)); break; } case DownloadStatus.Failed: { Console.WriteLine("Download failed."); break; } } }; request.DownloadWithStatus(stream); }); Docs = Docs.OrderBy(d => d.Name).ToList(); foreach (var doc in Docs) { using (var ms = new MemoryStream(doc.Doc)) { var d = PdfReader.Open(ms, PdfDocumentOpenMode.Import); outputDoc.AddPage(d.Pages[0]); } } return(outputDoc); }
static void Main(string[] args) { UserCredential credential; // If modifying these scopes, delete previously saved token.json string[] Scopes = { DriveService.Scope.DriveReadonly }; string ApplicationName = "########"; string fileId = "########"; string path = @"########"; //Include filename & extension i.e. C:\stuff\sheet001.xlsx var jetStream = new System.IO.MemoryStream(); using (var stream = new FileStream("credentials.json", FileMode.Open, FileAccess.Read)) { // The file token.json stores the user's access and refresh tokens, and is created // automatically when the authorization flow completes for the first time. string credPath = "token.json"; credential = GoogleWebAuthorizationBroker.AuthorizeAsync( GoogleClientSecrets.Load(stream).Secrets, Scopes, "user", CancellationToken.None, new FileDataStore(credPath, true)).Result; Console.WriteLine("Credential file saved to: " + credPath); } var service = new DriveService(new BaseClientService.Initializer() { HttpClientInitializer = credential, ApplicationName = ApplicationName, }); FilesResource.ExportRequest request = new FilesResource.ExportRequest(service, fileId, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); request.MediaDownloader.ProgressChanged += (Google.Apis.Download.IDownloadProgress progress) => { switch (progress.Status) { case Google.Apis.Download.DownloadStatus.Downloading: { Console.WriteLine(progress.BytesDownloaded); break; } case Google.Apis.Download.DownloadStatus.Completed: { Console.WriteLine("Download complete."); using (System.IO.FileStream file = new System.IO.FileStream(path, System.IO.FileMode.Create, System.IO.FileAccess.Write)) { jetStream.WriteTo(file); } break; } case Google.Apis.Download.DownloadStatus.Failed: { Console.WriteLine("Download failed."); break; } } }; request.DownloadWithStatus(jetStream); }