public void Run()
        {
            // setup HTML document name
            var name = "testpage1.md";
            // setup local document path
            var srcPath = Path.Combine(CommonSettings.LocalDataFolder, name);
            // setup storage folder where the source document should be present
            string folder = CommonSettings.StorageDataFolder;
            // setup storage name (null for default storage)
            string storage = null;
            // setup storage folder path where the result file will be uploaded to
            string outFolder = "/Html/Testout/Conversion";

            string outFile     = $"{name}_get_to_html_{DateTime.Now.ToString("yyyyMMdd_hhmmss")}.html";
            string storagePath = (folder == null) ? name : Path.Combine(folder, name).Replace('\\', '/');
            string outPath     = Path.Combine(outFolder, outFile).Replace('\\', '/');

            if (File.Exists(srcPath))
            {
                SdkBaseRunner.UploadToStorage(storagePath, srcPath);
            }
            else
            {
                throw new Exception(string.Format("Error: file {0} not found.", srcPath));
            }

            IImportApi     impApi   = new HtmlApi(CommonSettings.ClientId, CommonSettings.ClientSecret, CommonSettings.BasePath, CommonSettings.AuthPath);
            AsposeResponse response = impApi.PutImportMarkdownToHtml(name, outPath, folder, storage);

            if (response != null && response.Status == "OK")
            {
                Console.WriteLine(string.Format("\nResult file uploaded to: {0}", outPath));
            }
        }
예제 #2
0
        protected AsposeResponse CallPostApi(string path, Dictionary <string, string> queryParams, Dictionary <string, string> headerParams, Stream bodyStream, string methodName = "<unknown>")
        {
            string bodyFileName = "";

            if (queryParams.ContainsKey(PAR_FILENAME_I))
            {
                bodyFileName = queryParams[PAR_FILENAME_I];
            }
            else
            {
                bodyFileName = PAR_FILENAME_I;
            }

            HttpResponseMessage resp = ApiClient.CallPost(path, queryParams, headerParams, bodyStream, bodyFileName);

            GenerateApiException(resp, methodName);

            var response = new AsposeResponse()
            {
                Code   = (int)resp.StatusCode,
                Status = resp.StatusCode.ToString()
            };

            return(response);
        }
예제 #3
0
        protected AsposeResponse CallDeleteApi(string path, Dictionary <string, string> queryParams, string methodName = "<unknown>")
        {
            HttpResponseMessage resp = ApiClient.CallDelete(path, queryParams);

            GenerateApiException(resp, methodName);

            var response = new AsposeResponse()
            {
                Code   = (int)resp.StatusCode,
                Status = resp.StatusCode.ToString()
            };

            return(response);
        }
        public void Run()
        {
            string name = "testpage4_embcss.html";
            string path = Path.Combine(CommonSettings.LocalDataFolder, name);

            if (!File.Exists(path))
            {
                throw new FileNotFoundException("File not found in the Data folder", name);
            }

            string folder  = "/Html/Testout/Conversion";
            string storage = null;

            int width        = 800;
            int height       = 1200;
            int leftMargin   = 15;
            int rightMargin  = 15;
            int topMargin    = 15;
            int bottomMargin = 15;
            int resolution   = 96;

            string ext     = (Format == "tiff") ? "tif" : ((Format == "jpeg") ? "jpg" : Format);
            string outFile = $"{Path.GetFileNameWithoutExtension(name)}_converted_at_{DateTime.Now.ToString("yyyyMMdd_hhmmss")}.{ext}";
            string outPath = Path.Combine(folder, outFile).Replace('\\', '/');

            using (Stream srcStream = new FileStream(path, FileMode.Open, FileAccess.Read))
            {
                IConversionApi convApi    = new HtmlApi(CommonSettings.ClientId, CommonSettings.ClientSecret, CommonSettings.BasePath);
                IStorageApi    storageApi = new StorageApi((ApiBase)convApi);

                if (!storageApi.FileOrFolderExists(folder, storage))
                {
                    ((IStorageFolderApi)storageApi).CreateFolder(folder, storage);
                }
                AsposeResponse response = null;
                string         dataType = Path.GetExtension(name).Replace(".", "");
                // call SDK methods that convert HTML document to supported out format
                switch (Format)
                {
                case "pdf":
                    outFile += ".pdf";
                    response = convApi.PostConvertDocumentToPdf(
                        srcStream, outPath, width, height, leftMargin, rightMargin, topMargin, bottomMargin, storage);
                    break;

                case "xps":
                    response = convApi.PostConvertDocumentToXps(
                        srcStream, outPath, width, height, leftMargin, rightMargin, topMargin, bottomMargin, storage);
                    break;

                case "jpeg":
                case "bmp":
                case "png":
                case "tiff":
                case "gif":
                    response = convApi.PostConvertDocumentToImage(
                        srcStream, Format, outPath, width, height,
                        leftMargin, rightMargin, topMargin, bottomMargin,
                        resolution, storage);
                    break;

                default:
                    throw new ArgumentException($"Unsupported output format: {Format}");
                }

                if (response != null && response.Status == "OK")
                {
                    storageApi = new StorageApi((ApiBase)convApi);
                    if (storageApi.FileOrFolderExists(outPath))
                    {
                        Console.WriteLine(string.Format("\nResult file uploaded to: {0}", outPath));
                    }
                }
            }
        }
예제 #5
0
        public void Run()
        {
            var    name    = FileName;
            var    srcPath = Path.Combine(CommonSettings.LocalDataFolder, name);
            string folder  = CommonSettings.StorageDataFolder;
            string storage = null;
            // setup storage folder path where the result file will be uploaded to
            string outFolder = "/Html/Testout/Conversion";

            int width        = 800;
            int height       = 1200;
            int leftMargin   = 15;
            int rightMargin  = 15;
            int topMargin    = 15;
            int bottomMargin = 15;
            int resolution   = 96;

            string storagePath = (folder == null) ? name : Path.Combine(folder, name).Replace('\\', '/');

            string ext     = (Format == "tiff") ? "tif" : ((Format == "jpeg") ? "jpg" : Format);
            string outFile = $"{Path.GetFileNameWithoutExtension(name)}_converted_at_{DateTime.Now.ToString("yyyyMMdd_hhmmss")}.{ext}";
            string outPath = Path.Combine(outFolder, outFile).Replace('\\', '/');

            if (CheckIfStorageFileExists)
            {
                if (File.Exists(srcPath))
                {
                    SdkBaseRunner.UploadToStorage(storagePath, srcPath);
                }
                else
                {
                    throw new Exception(string.Format("Error: file {0} not found.", srcPath));
                }
            }

            IConversionApi convApi  = new HtmlApi(CommonSettings.ClientId, CommonSettings.ClientSecret, CommonSettings.BasePath);
            AsposeResponse response = null;

            // call SDK methods that convert HTML document to supported out format
            switch (Format)
            {
            case "pdf":
                outFile += ".pdf";
                response = convApi.PutConvertDocumentToPdf(
                    name, outPath, width, height, leftMargin, rightMargin, topMargin, bottomMargin, folder, storage);
                break;

            case "xps":
                response = convApi.PutConvertDocumentToXps(
                    name, outPath, width, height, leftMargin, rightMargin, topMargin, bottomMargin, folder, storage);
                break;

            case "jpeg":
            case "bmp":
            case "png":
            case "tiff":
            case "gif":
                response = convApi.PutConvertDocumentToImage(
                    name, Format, outPath, width, height,
                    leftMargin, rightMargin, topMargin, bottomMargin,
                    resolution, folder, storage);
                break;

            case "md":
                response = convApi.PutConvertDocumentToMarkdown(name, outPath, false);
                break;

            default:
                throw new ArgumentException($"Unsupported output format: {Format}");
            }

            if (response != null && response.Status == "OK")
            {
                Console.WriteLine(string.Format("\nResult file uploaded to: {0}", outPath));
            }
        }
예제 #6
0
        ///<Summary>
        /// Convert method to convert PDF file to other format
        ///</Summary>
        public Response Convert(string fileName, string outputType)
        {
            PdfApi pdfApi = new PdfApi(Config.Configuration.AppKey, Config.Configuration.AppSID);
            string fileNamewithOutExtension = Path.GetFileNameWithoutExtension(fileName);
            string fileExtension            = Path.GetExtension(fileName);

            fileExtension = fileExtension.Replace(".", "").Trim().ToLower();
            string outputFileName = fileNamewithOutExtension + "." + outputType;

            if (pdfApi != null)
            {
                AsposeResponse response = null;
                if ((outputType.Equals("pdf")) && (fileExtension.Equals("epub") || fileExtension.Equals("latex") || fileExtension.Equals("mht") || fileExtension.Equals("html") || fileExtension.Equals("ps") || fileExtension.Equals("xps") || fileExtension.Equals("svg") || fileExtension.Equals("pcl") || fileExtension.Equals("xml")))
                {
                    switch (fileExtension)
                    {
                    case "epub":
                        response = pdfApi.PutEpubInStorageToPdf(fileName, outputFileName);
                        break;

                    case "latex":
                        response = pdfApi.PutLaTeXInStorageToPdf(fileName, outputFileName);
                        break;

                    case "mht":
                        response = pdfApi.PutMhtInStorageToPdf(fileName, outputFileName);
                        break;

                    case "html":
                        response = pdfApi.PutHtmlInStorageToPdf(fileName, outputFileName);
                        break;

                    case "ps":
                        response = pdfApi.PutPsInStorageToPdf(fileName, outputFileName);
                        break;

                    case "xps":
                        response = pdfApi.PutXpsInStorageToPdf(fileName, outputFileName);
                        break;

                    case "svg":
                        response = pdfApi.PutSvgInStorageToPdf(fileName, outputFileName);
                        break;

                    case "pcl":
                        response = pdfApi.PutPclInStorageToPdf(fileName, outputFileName);
                        break;

                    case "xml":
                        response = pdfApi.PutXmlInStorageToPdf(fileName, outputFileName);
                        break;
                    }
                }
                else if (outputType.Equals("jpeg") || outputType.Equals("emf") || outputType.Equals("png") || outputType.Equals("bmp") || outputType.Equals("gif"))
                {
                    // Invoke Aspose.PDF Cloud SDK API to get page count from pdf document
                    DocumentPagesResponse apiResponse = pdfApi.GetPages(fileName, storage: null, folder: null);

                    if (apiResponse != null && apiResponse.Status.Equals("OK"))
                    {
                        int count      = apiResponse.Pages.List.Count;
                        int pageNumber = 0;
                        for (int i = 0; i < count; i++)
                        {
                            pageNumber     = i + 1;
                            outputFileName = Path.Combine(fileNamewithOutExtension, "Page_" + pageNumber + "." + outputType);

                            switch (outputType)
                            {
                            case "jpeg":
                                response = pdfApi.PutPageConvertToJpeg(fileName, pageNumber, outputFileName, width: null, height: null, folder: null, storage: null);
                                break;

                            case "emf":
                                response = pdfApi.PutPageConvertToEmf(fileName, pageNumber, outputFileName, width: null, height: null, folder: null, storage: null);
                                break;

                            case "png":
                                response = pdfApi.PutPageConvertToPng(fileName, pageNumber, outputFileName, width: null, height: null, folder: null, storage: null);
                                break;

                            case "bmp":
                                response = pdfApi.PutPageConvertToBmp(fileName, pageNumber, outputFileName, width: null, height: null, folder: null, storage: null);
                                break;

                            case "gif":
                                response = pdfApi.PutPageConvertToGif(fileName, pageNumber, outputFileName, width: null, height: null, folder: null, storage: null);
                                break;
                            }
                        }
                    }
                }
                else
                {
                    switch (outputType)
                    {
                    case "xls":
                        response = pdfApi.PutPdfInStorageToXls(fileName, outputFileName, folder: null);
                        break;

                    case "xlsx":
                        response = pdfApi.PutPdfInStorageToXlsx(fileName, outputFileName, folder: null);
                        break;

                    case "pptx":
                        response = pdfApi.PutPdfInStorageToPptx(fileName, outputFileName, folder: null);
                        break;

                    case "doc":
                        response = pdfApi.PutPdfInStorageToDoc(fileName, outputFileName, folder: null);
                        break;

                    case "epub":
                        response = pdfApi.PutPdfInStorageToEpub(fileName, outputFileName, folder: null);
                        break;

                    case "html":
                        response = pdfApi.PutPdfInStorageToHtml(fileName, outputFileName, folder: null);
                        break;

                    case "tex":
                        response = pdfApi.PutPdfInStorageToTeX(fileName, outputFileName, folder: null);
                        break;

                    case "svg":
                        response = pdfApi.PutPdfInStorageToSvg(fileName, outputFileName, folder: null);
                        break;

                    case "latex":
                        response = pdfApi.PutPdfInStorageToLaTeX(fileName, outputFileName, folder: null);
                        break;

                    case "tiff":
                        response = pdfApi.PutPdfInStorageToTiff(fileName, outputFileName, folder: null);
                        break;

                    case "xml":
                        response = pdfApi.PutPdfInStorageToXml(fileName, outputFileName, folder: null);
                        break;

                    case "xps":
                        response = pdfApi.PutPdfInStorageToXps(fileName, outputFileName, folder: null);
                        break;

                    case "text":
                        response = pdfApi.PutPdfInStorageToTeX(fileName, outputFileName, folder: null);
                        break;
                    }
                }

                return(new Response
                {
                    FileName = outputFileName,
                    Status = "OK",
                    StatusCode = 200,
                });
            }
            return(new Response
            {
                FileName = null,
                Status = "Output type not found",
                StatusCode = 500
            });
        }