Translating PDF files with GroupDocs.Translation SDK
Translating PDF files with GroupDocs.Translation SDK
Leave feedback
Although you can directly call the GroupDocs.Translation Cloud REST API to send PDF file for translation and fetch translated file, there is a much easier way to implement translation functionality in your applications. We provide software development kits (SDKs) for all popular programming languages. They wrap up all routine operations such as establishing connections, sending API requests, and parsing responses into a few simple methods. It makes interaction with GroupDocs.Translation Cloud services much easier, allowing you to focus on business logic rather than technical details.
usingSystem.IO;usingSystem.Collections.Generic;usingSystem.Net.Http;usingGroupDocs.Translation.Cloud.Sdk.Api;usingGroupDocs.Translation.Cloud.Sdk.Client;usingGroupDocs.Translation.Cloud.Sdk.Client.Auth;usingGroupDocs.Translation.Cloud.Sdk.Model;usingConfiguration=GroupDocs.Translation.Cloud.Sdk.Client.Configuration;usingHttpStatusCode=System.Net.HttpStatusCode;namespaceGroupDocs.Translation.Cloud.Sdk{publicclassFileTranslator{publicFileTranslator(){Configurationconfig=newConfiguration();/** Authorize your requests to GroupDocs.Translation Cloud */config.OAuthFlow=OAuthFlow.APPLICATION;config.OAuthClientId="YOU_CLIENT_ID";config.OAuthClientSecret="YOU_CLIENT_SECRET";/** Initialize GroupDocs.Translation API */config.BasePath="https://api.groupdocs.cloud/v2.0/translation";TranslationApiapi=newTranslationApi(config);FileApifileApi=newFileApi(config);/** Specify translation parameters */stringfilePath="/path/to/myfile.pdf";stringsavePath="/path/to/savefile/";stringsourceLanguage="en";vartargetLanguages=newList<string>(){"de"};stringformat="pdf";stringoutputFormat="docx";byte[]file=File.ReadAllBytes(filePath);MemoryStreamms=newMemoryStream(file);stringurl=fileApi.FileUploadPost(format,ms);CloudFileResponseresponse=newCloudFileResponse();varrequest=newPdfFileRequest(sourceLanguage:sourceLanguage,targetLanguages:targets,url:url,outputFormat:outputFormat,savingMode:PdfFileRequest.SavingModeEnum.Files);/** Send file to translation */varresponseId=awaitapi.PdfPostAsyncrequest);/** Wait for results from translation queue */try{if((HttpStatusCode)responseId.Status==HttpStatusCode.Accepted){while(true){response=awaitapi.DocumentRequestIdGetAsync(responseId.Id);if(response.Urls.Count>0)break;elseThread.Sleep(2000);}Console.WriteLine("File translated");using(varclient=newHttpClient()){foreach(stringlangintargets){stringsavePath=$"{savepath}{lang}.{outputFormat}";varresult=awaitclient.GetByteArrayAsync(response.Urls[lang].Url);File.WriteAllBytes(savePath,result);}}}else{response=newCloudFileResponse(){Status=responseId.Status,Message=responseId.Message};Console.WriteLine("error: "+response.Message);}}catch(Exceptionex){Console.WriteLine("exception: "+ex.ToString());}}}}