Exemplo n.º 1
0
        ///<Summary>
        /// Convert method to convert file to other format
        ///</Summary>
        public Response Convert(string fileName, string folderName, string outputType)
        {
            ImagingApi imagingApi = new ImagingApi(Config.Configuration.AppKey, Config.Configuration.AppSID, "https://api.aspose.cloud/");

            string filenamepath = Config.Configuration.WorkingDirectory + folderName + "\\" + fileName;

            // Upload local image to Cloud Storage
            using (FileStream localInputImage = File.OpenRead(filenamepath))
            {
                var uploadFileRequest    = new UploadFileRequest(fileName, localInputImage);
                FilesUploadResult result = imagingApi.UploadFile(uploadFileRequest);
            }

            // Please refer to https://docs.aspose.cloud/display/imagingcloud/Supported+File+Formats
            // for possible output formats
            string format  = outputType.ToLower();
            string folder  = null; // Input file is saved at the root of the storage
            string storage = null; // Cloud Storage name

            var    request      = new ConvertImageRequest(fileName, format, folder, storage);
            Stream updatedImage = imagingApi.ConvertImage(request);

            updatedImage.Position = 0;
            string fileNamewithOutExtension = Path.GetFileNameWithoutExtension(filenamepath);
            string outputFileName           = fileNamewithOutExtension + "." + outputType;

            Aspose.Storage.Cloud.Sdk.Model.Requests.PutCreateRequest putCreateRequest = new Aspose.Storage.Cloud.Sdk.Model.Requests.PutCreateRequest(outputFileName, updatedImage, null, null);
            storageApi.PutCreate(putCreateRequest);

            bool foundSaveOption = true;

            if (outputType == "pdf")
            {
            }
            else
            {
                foundSaveOption = false;
            }

            if (foundSaveOption)
            {
                return(new Response
                {
                    FileName = outputFileName,
                    Status = "OK",
                    StatusCode = 200,
                });
            }

            return(new Response
            {
                FileName = null,
                Status = "Output type not found",
                StatusCode = 500
            });
        }
Exemplo n.º 2
0
 /// <summary>
 /// Upload file on cloud storage
 /// </summary>
 /// <param name="srcFile">Path to the file</param>
 /// <param name="dstPath">Name of the file on cloud</param>
 protected void UploadFile(string srcFile, string dstPath)
 {
     using (FileStream fs = new FileStream(srcFile, FileMode.Open))
     {
         FilesUploadResult response = FileApi.UploadFile(new UploadFileRequest(dstPath, fs));
         if (response.Errors.Count == 0)
         {
             Console.WriteLine($"File {dstPath} uploaded successfully with response Status: Ok");
         }
     }
 }
        static void TranslateDocument(Configuration conf)
        {
            // request parameters for translation
            string     name      = "test.docx";
            string     folder    = "";
            string     pair      = "en-fr";
            string     format    = "docx";
            string     outformat = "";
            string     storage   = "First Storage";
            string     saveFile  = "translated_d.docx";
            string     savePath  = "";
            bool       masters   = false;
            List <int> elements  = new List <int>();

            // local paths to upload and download files
            string uploadPath   = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.Parent.Parent.FullName + "/" + name;
            string downloadPath = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.Parent.Parent.FullName + "/" + saveFile;

            TranslationApi api     = new TranslationApi(conf);
            FileApi        fileApi = new FileApi(conf);


            Stream stream = File.Open(uploadPath, FileMode.Open);

            UploadFileRequest uploadRequest = new UploadFileRequest {
                File = stream, path = name, storageName = storage
            };
            FilesUploadResult uploadResult = fileApi.UploadFile(uploadRequest);

            Console.WriteLine("Files uploaded: " + uploadResult.Uploaded.Count);

            TranslateDocumentRequest request  = api.CreateDocumentRequest(name, folder, pair, format, outformat, storage, saveFile, savePath, masters, elements);
            TranslationResponse      response = api.RunTranslationTask(request);

            Console.WriteLine(response.Message);
            foreach (var key in response.Details.Keys)
            {
                Console.WriteLine(key + ": " + response.Details[key]);
            }

            DownloadFileRequest downloadRequest = new DownloadFileRequest {
                storageName = storage, path = saveFile
            };
            Stream result = fileApi.DownloadFile(downloadRequest);

            Console.WriteLine("Translated file downloaded");

            using (FileStream file = new FileStream(downloadPath, FileMode.Create, FileAccess.Write))
            {
                result.CopyTo(file);
            }
            Console.WriteLine("Translated file saved");
        }
Exemplo n.º 4
0
        private string PutTestFile(string fileName)
        {
            using FileStream fileToUpload = File.Open(TestFilePath(fileName), FileMode.Open, FileAccess.Read);
            FilesUploadResult uploaded = _fileApi.UploadFile(
                new UploadFileRequest(
                    $"{TempFolderPath}/{fileName}",
                    fileToUpload
                    )
                );

            Assert.IsNotEmpty(uploaded.Uploaded);

            return(TempFolderPath);
        }
        public void ConvertFromStorageExampleTest()
        {
            var config     = this.ImagingApi.Configuration;
            var imagingApi = config.OnPremise ? new ImagingApi(config.ApiBaseUrl, config.ApiVersion, config.DebugMode)
                : new ImagingApi(config.ClientSecret, config.ClientId, config.ApiBaseUrl);

            try
            {
                // upload local image to storage
                using (FileStream localInputImage = File.OpenRead(Path.Combine(LocalTestFolder, "test.png")))
                {
                    var uploadFileRequest =
                        new UploadFileRequest("ExampleFolderNet/inputImage.png", localInputImage,
                                              config.OnPremise ? this.TestStorage : null);
                    FilesUploadResult result = imagingApi.UploadFile(uploadFileRequest);
                    // inspect result.Errors list if there were any
                    // inspect result.Uploaded list for uploaded file names
                }

                // convert image from storage to JPEG
                var getConvertRequest =
                    new ConvertImageRequest("inputImage.png", "jpg", "ExampleFolderNet",
                                            config.OnPremise ? this.TestStorage : null);

                using (Stream convertedImage = imagingApi.ConvertImage(getConvertRequest))
                {
                    // process resulting image
                    // for example, save it to storage
                    var uploadFileRequest =
                        new UploadFileRequest("ExampleFolderNet/resultImage.jpg", convertedImage,
                                              config.OnPremise ? this.TestStorage : null);
                    FilesUploadResult result = imagingApi.UploadFile(uploadFileRequest);
                    // inspect result.Errors list if there were any
                    // inspect result.Uploaded list for uploaded file names
                }
            }
            finally
            {
                // remove files from storage
                imagingApi.DeleteFile(new DeleteFileRequest("ExampleFolderNet/inputImage.png",
                                                            config.OnPremise ? this.TestStorage : null));
                imagingApi.DeleteFile(new DeleteFileRequest("ExampleFolderNet/resultImage.jpg",
                                                            config.OnPremise ? this.TestStorage : null));
            }
        }
Exemplo n.º 6
0
        internal static (string txt, string err) ExtractTextFromPdfStream(Stream pdfStream)
        {
            Console.WriteLine("ExtractTextFromPdfStream");
            Configuration cfg  = new Configuration(Common.AsposeKey, Common.AsposeSID);
            PdfApi        api  = new PdfApi(cfg);
            string        path = Path.GetRandomFileName();

            try {
                FilesUploadResult filesUploadResult = api.UploadFile(path, pdfStream);
                if (filesUploadResult.Errors != null && filesUploadResult.Errors.Count > 0)
                {
                    return("", string.Join(Environment.NewLine, filesUploadResult.Errors.Select(error => error.ToString())));
                }
                TextRectsResponse textRectsResponse = api.GetText(path, 0, 0, 0, 0);
                if (textRectsResponse.Code != StatusCodes.Status200OK)
                {
                    return("", textRectsResponse.ToString());
                }
                return(string.Concat(textRectsResponse.TextOccurrences.List.Select(textRect => textRect.Text)), "");
            }
            finally {
                api.DeleteFileWithHttpInfo(path);
            }
        }