Skip to content

Instantly share code, notes, and snippets.

@aspose-cloud
Last active September 19, 2018 09:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aspose-cloud/61188ebe2fe3b6003f5a37514d572cdd to your computer and use it in GitHub Desktop.
Save aspose-cloud/61188ebe2fe3b6003f5a37514d572cdd to your computer and use it in GitHub Desktop.
The GIST contains .NET Examples of Aspose.HTML Cloud APIs.
The GIST contains .NET Examples of Aspose.HTML Cloud APIs.
// For complete examples and data files, please go to https://github.com/aspose-html-cloud/aspose-html-cloud-dotnet
using System;
using System.IO;
using Com.Aspose.Html.Api;
using Com.Aspose.Html.Api.Interfaces;
namespace Aspose.HTML.Cloud.Examples.SDK.HtmlConvert
{
/// <summary>
/// Aspose.HTML Cloud for .NET SDK - examples.
/// =========================================
/// Example that demonstrates how to convert HTML page by its URL to one of formats
/// supported by Aspose.HTML for Cloud and save it to the cloud storage.
/// </summary>
public class ConvertHTMLByUrl : ISdkRunner
{
private string Format { get; set; }
public ConvertHTMLByUrl(string format)
{
Format = format;
}
public string FileUrl { get; set; }
public void Run()
{
FileUrl = @"https://www.le.ac.uk/oerresources/bdra/html/page_01.htm";
//FileUrl = @"https://docs.gitlab.com/ee/README.html";
string name = "page_01.htm";
string ext = (Format == "tiff") ? "tif" : ((Format == "jpeg") ? "jpg" : Format);
string outFile = $"{Path.GetFileNameWithoutExtension(name)}_converted.{ext}";
string outPath = Path.Combine(CommonSettings.OutDirectory, outFile);
IConversionApi convApi = new ConversionApi(CommonSettings.AppKey, CommonSettings.AppSID, CommonSettings.BasePath);
Stream response = null;
// call SDK methods that convert HTML document to supported out format
switch (Format)
{
case "pdf":
response = convApi.GetConvertDocumentToPdfByUrl(FileUrl, 1200, 800);
break;
case "xps":
response = convApi.GetConvertDocumentToXps(FileUrl, 1200, 800);
break;
case "jpeg":
case "bmp":
case "png":
case "tiff":
response = convApi.GetConvertDocumentToImage(Format, FileUrl, 800, 1200);
break;
default:
throw new ArgumentException($"Unsupported output format: {Format}");
}
if (response != null && typeof(FileStream) == response.GetType())
{
using (FileStream fstr = new FileStream(outPath, FileMode.Create, FileAccess.Write))
{
response.Position = 0;
response.CopyTo(fstr);
fstr.Flush();
Console.WriteLine(string.Format("\nResult file downloaded to: {0}", outPath));
}
}
}
}
}
// For complete examples and data files, please go to https://github.com/aspose-html-cloud/aspose-html-cloud-dotnet
using System;
using System.IO;
using Com.Aspose.Storage.Model;
using Com.Aspose.Storage.Api;
using Com.Aspose.Html.Api;
using Com.Aspose.Html.Api.Interfaces;
using Com.Aspose.Html.NativeClient;
namespace Aspose.HTML.Cloud.Examples.SDK.HtmlConvert
{
/// <summary>
/// Aspose.HTML Cloud for .NET SDK - examples.
/// =========================================
/// Example that demonstrates how to convert HTML page in the local filesystem passing it to the request stream,
/// convert it to one of the supported by Aspose.HTML for Cloud and save it to the cloud storage.
/// </summary>
public class ConvertHTMLLocal : ISdkRunner
{
private string Format { get; set; }
public ConvertHTMLLocal(string format)
{
Format = format;
}
public void Run()
{
string name = "testpage4_embcss.html";
string path = Path.Combine(CommonSettings.DataFolder, name);
if (!File.Exists(path))
throw new FileNotFoundException("File not found in the Data folder", name);
string folder = null;
string storage = null;
int width = 800;
int height = 1200;
int leftMargin = 15;
int rightMargin = 15;
int topMargin = 15;
int bottomMargin = 15;
int xResolution = 96;
int yResolution = 96;
string ext = (Format == "tiff") ? "tif" : ((Format == "jpeg") ? "jpg" : Format);
string outFile = $"{Path.GetFileNameWithoutExtension(name)}_converted.{ext}";
using (Stream srcStream = new FileStream(path, FileMode.Open, FileAccess.Read))
{
IConversionApi convApi = new ConversionApi(CommonSettings.AppKey, CommonSettings.AppSID, CommonSettings.BasePath);
NativeRestResponse response = null;
// call SDK methods that convert HTML document to supported out format
switch (Format)
{
case "pdf":
outFile += ".pdf";
response = convApi.PutConvertDocumentToPdf(
srcStream, outFile, width, height, leftMargin, rightMargin, topMargin, bottomMargin, storage);
break;
case "xps":
response = convApi.PutConvertDocumentToXps(
srcStream, outFile, width, height, leftMargin, rightMargin, topMargin, bottomMargin, storage);
break;
case "jpeg":
case "bmp":
case "png":
case "tiff":
response = convApi.PutConvertDocumentToImage(
srcStream, Format, outFile, width, height,
leftMargin, rightMargin, topMargin, bottomMargin,
xResolution, yResolution, storage);
break;
default:
throw new ArgumentException($"Unsupported output format: {Format}");
}
if (response != null
&& (string)response.Content == "storage"
&& response.ContentType == NativeRestResponse.RespContentType.FileName)
{
// get the result file name from response object
string outFileName = response.ContentName;
StorageApi storageApi = new StorageApi(CommonSettings.AppKey, CommonSettings.AppSID, CommonSettings.BasePath);
FileExistResponse resp2 = storageApi.GetIsExist(outFileName, null, null);
if (resp2.FileExist.IsExist)
{
// if result file exists in the storage, try to downloa it to the local file system
var resp3 = storageApi.GetDownload(outFileName, null, null);
if (resp3.ResponseStream != null)
{
string outPath = Path.Combine(CommonSettings.OutDirectory, outFileName);
using (FileStream fstr = new FileStream(outPath, FileMode.Create, FileAccess.Write))
{
fstr.Write(resp3.ResponseStream, 0, resp3.ResponseStream.Length);
fstr.Flush();
Console.WriteLine(string.Format("\nResult file downloaded to: {0}", outPath));
}
}
}
else
{
Console.WriteLine("Error: result file wasn't saved to storage.");
}
}
}
}
}
}
// For complete examples and data files, please go to https://github.com/aspose-html-cloud/aspose-html-cloud-dotnet
using System.IO;
using Com.Aspose.Html.Api;
namespace Aspose.HTML.Cloud.Examples.SDK.HtmlDocument
{
public class GetDocumentSdk : ISdkRunner
{
public void Run()
{
var name = "zero.txt";
string folder = null;
string storage = null;
DocumentApi api = new DocumentApi(CommonSettings.AppKey, CommonSettings.AppSID, CommonSettings.BasePath);
var result = api.GetDocument(name, storage, folder);
if (result != null && typeof(FileStream) == result.GetType())
{
}
}
}
}
// For complete examples and data files, please go to https://github.com/aspose-html-cloud/aspose-html-cloud-dotnet
using System;
using System.IO;
using Com.Aspose.Storage.Api;
using Com.Aspose.Html.Api;
using Com.Aspose.Html.Api.Interfaces;
namespace Aspose.HTML.Cloud.Examples.SDK.HtmlDocument
{
/// <summary>
/// Aspose.HTML Cloud for .NET SDK - examples.
/// =========================================
/// Example that demonstrates how to extract the HTML fragments by XPath query
/// from the HTML document in the cloud storage.
/// </summary>
public class ExtractHtmlFragmentsByXPath : ISdkRunner
{
public void Run()
{
var name = "testpage3_embcss.html";
var xPath = "//ol/li";
StorageApi storageApi = new StorageApi(CommonSettings.AppKey, CommonSettings.AppSID, CommonSettings.BasePath);
// Upload source file to cloud storage (default is AmazonS3)
var srcPath = Path.Combine(CommonSettings.DataFolder, name);
if (File.Exists(srcPath))
storageApi.PutCreate(name, null, null, File.ReadAllBytes(srcPath));
else
throw new Exception(string.Format("Error: file {0} not found.", srcPath));
var response = storageApi.GetIsExist(name, null, null);
IDocumentApi docApi = new DocumentApi(CommonSettings.AppKey, CommonSettings.AppSID, CommonSettings.BasePath);
// call the SDK method that returns a query result in the response stream.
Stream stream = docApi.GetDocumentFragmentByXPath(name, xPath, "json", null, null);
if (stream != null && typeof(FileStream) == stream.GetType())
{
string outFile = $"{Path.GetFileNameWithoutExtension(name)}_fragments.json";
string outPath = Path.Combine(CommonSettings.OutDirectory, outFile);
using (FileStream fstr = new FileStream(outPath, FileMode.Create, FileAccess.Write))
{
stream.Position = 0;
stream.CopyTo(fstr);
fstr.Flush();
Console.WriteLine(string.Format("\nResult file downloaded to: {0}", outPath));
}
}
}
}
}
// For complete examples and data files, please go to https://github.com/aspose-html-cloud/aspose-html-cloud-dotnet
// Example 1
var url = @"https://products.aspose.com/html/net";
var xpath = ".//div[@class=\"container\"]";
Stream stream = DocumentApi.GetDocumentFragmentByXPathByUrl(url, xpath, "plain");
// Example 2
var url = @"https://stallman.org/articles/words-to-greece.html";
var xpath = ".//p";
Stream stream = DocumentApi.GetDocumentFragmentByXPathByUrl(url, xpath, "plain");
// For complete examples and data files, please go to https://github.com/aspose-html-cloud/aspose-html-cloud-dotnet
using System;
using System.IO;
using Com.Aspose.Storage.Api;
using Com.Aspose.Html.Api;
using Com.Aspose.Html.Api.Interfaces;
namespace Aspose.HTML.Cloud.Examples.SDK.HtmlDocument
{
/// <summary>
/// Aspose.HTML Cloud for .NET SDK - examples.
/// =========================================
/// Example that demonstrates how to get all images of the HTML document that is stored
/// in the cloud storage.
/// </summary>
public class ExtractHTMLImagesAll : ISdkRunner
{
public void Run()
{
string name = "testpage5.html.zip"; // storage file name
string folder = "HtmlTemp"; // storage folder name
string filePath = Path.Combine(CommonSettings.DataFolder, name);
StorageApi storageApi = new StorageApi(CommonSettings.AppKey, CommonSettings.AppSID, CommonSettings.BasePath);
string storagePath = string.IsNullOrEmpty(folder) ? name : string.Format("{0}/{1}", folder, name);
if (File.Exists(filePath))
{
// upload source file to the cloud storage (default is AmazonS3)
//filePath = string.Format("file:///{0}", filePath.Replace('\\', '/'));
storageApi.PutCreate(storagePath, null, null, File.ReadAllBytes(filePath));
}
else
throw new Exception(string.Format("Error: file {0} not found.", filePath));
IDocumentApi docApi = new DocumentApi(CommonSettings.AppKey, CommonSettings.AppSID, CommonSettings.BasePath);
// call SDK method that gets a zip archive with all HTML document images
Stream stream = docApi.GetDocumentImages(name, null, folder);
if (stream != null && typeof(FileStream) == stream.GetType())
{
string outFile = $"{Path.GetFileNameWithoutExtension(name)}_images.zip";
string outPath = Path.Combine(CommonSettings.OutDirectory, outFile);
using (FileStream fstr = new FileStream(outPath, FileMode.Create, FileAccess.Write))
{
stream.Position = 0;
stream.CopyTo(fstr);
fstr.Flush();
Console.WriteLine(string.Format("\nResult file downloaded to: {0}", outPath));
}
}
}
}
}
// For complete examples and data files, please go to https://github.com/aspose-html-cloud/aspose-html-cloud-dotnet
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Com.Aspose.Storage;
using Com.Aspose.Storage.Api;
using Com.Aspose.Html;
using Com.Aspose.Html.Api;
using Com.Aspose.Html.Api.Interfaces;
using Aspose.HTML.Cloud.Examples.SDK;
namespace Aspose.HTML.Cloud.SDK.Examples.SDK.HtmlOcr
{
public class RecognizeAndImportToHTML : ISdkRunner
{
public void Run()
{
string srcName = "ocr_test_1.png";
string folder = "HtmlTestDoc";
string storage = null;
OcrApi ocrApi = new OcrApi(CommonSettings.AppKey, CommonSettings.AppSID, CommonSettings.BasePath);
Stream stream = ocrApi.GetRecognizeAndImportToHtml(srcName, "en", folder, storage);
if (stream != null && stream.GetType() == typeof(FileStream))
{
string name = ((FileStream)stream).Name;
string outPath = Path.Combine(CommonSettings.OutDirectory, Path.GetFileName(name));
using (FileStream fstr = new FileStream(outPath, FileMode.Create, FileAccess.Write))
{
stream.CopyTo(fstr);
fstr.Flush();
Console.WriteLine(string.Format("File '{0}' downloaded to: {1}", Path.GetFileName(name), outPath));
}
}
stream.Close();
stream.Dispose();
}
}
}
// For complete examples and data files, please go to https://github.com/aspose-html-cloud/aspose-html-cloud-dotnet
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Com.Aspose.Storage;
using Com.Aspose.Storage.Api;
using Com.Aspose.Html;
using Com.Aspose.Html.Api;
using Com.Aspose.Html.Api.Interfaces;
using Aspose.HTML.Cloud.Examples.SDK;
namespace Aspose.HTML.Cloud.SDK.Examples.SDK.HtmlOcr
{
public class RecognizeAndTranslateToHTML : ISdkRunner
{
string SrcLang { get; set; }
string ResLang { get; set; }
public RecognizeAndTranslateToHTML(string srclang, string reslang)
{
SrcLang = srclang;
ResLang = reslang;
}
public void Run()
{
string srcName = "ocr_test_1.png";
string folder = "HtmlTestDoc";
string storage = null;
OcrApi ocrApi = new OcrApi(CommonSettings.AppKey, CommonSettings.AppSID, CommonSettings.BasePath);
Stream stream = ocrApi.GetRecognizeAndTranslateToHtml(srcName, SrcLang, ResLang, folder, storage);
if (stream != null && stream.GetType() == typeof(FileStream))
{
string name = ((FileStream)stream).Name;
string outPath = Path.Combine(CommonSettings.OutDirectory, Path.GetFileName(name));
using (FileStream fstr = new FileStream(outPath, FileMode.Create, FileAccess.Write))
{
stream.CopyTo(fstr);
fstr.Flush();
Console.WriteLine(string.Format("File '{0}' downloaded to: {1}", Path.GetFileName(name), outPath));
}
}
stream.Close();
stream.Dispose();
}
}
}
using System;
using System.IO;
using Com.Aspose.Html.Api;
using Com.Aspose.Html.Api.Interfaces;
namespace Aspose.HTML.Cloud.Examples.SDK.HtmlTranslate
{
/// <summary>
/// Aspose.HTML Cloud for .NET SDK - examples.
/// =========================================
/// Example that demonstrates how to translate HTML page by its URL
/// and to get the result in the response stream.
/// </summary>
public class TranslateHTMLByUrl : ISdkRunner
{
string SrcLang { get; set; }
string ResLang { get; set; }
public TranslateHTMLByUrl(string srclang, string reslang)
{
SrcLang = srclang;
ResLang = reslang;
}
public void Run()
{
string srcUrl = "https://stallman.org/sayings.html";
//string srcUrl = @"https://www.le.ac.uk/oerresources/bdra/html/page_01.htm";
TranslationApi transApi = new TranslationApi(CommonSettings.AppKey, CommonSettings.AppSID, CommonSettings.BasePath);
Stream stream = transApi.GetTranslateDocumentByUrl(srcUrl, SrcLang, ResLang);
string strName = null;
if (stream != null && stream.GetType() == typeof(FileStream))
{
string name = ((FileStream)stream).Name;
string outPath = Path.Combine(CommonSettings.OutDirectory, Path.GetFileName(name));
using (FileStream fstr = new FileStream(outPath, FileMode.Create, FileAccess.Write))
{
stream.CopyTo(fstr);
fstr.Flush();
Console.WriteLine(string.Format("File '{0}' downloaded to: {1}", Path.GetFileName(name), outPath));
}
}
stream.Close();
stream.Dispose();
}
}
}
using System;
using System.IO;
using Com.Aspose.Storage.Api;
using Com.Aspose.Storage.Model;
using Com.Aspose.Html;
using Com.Aspose.Html.Api;
using Com.Aspose.Html.NativeClient;
namespace Aspose.HTML.Cloud.Examples.SDK.HtmlTranslate
{
/// <summary>
/// Aspose.HTML Cloud for .NET SDK - examples.
/// =========================================
/// Example that demonstrates how to translate the HTML document by its name in the cloud storage
/// and to get tte result in the response stream.
/// </summary>
public class TranslateHTMLFromStorage : ISdkRunner
{
string SrcLang { get; set; }
string ResLang { get; set; }
public TranslateHTMLFromStorage(string srclang, string reslang)
{
SrcLang = srclang;
ResLang = reslang;
}
public void Run()
{
string name = "testpage1.html";
string folder = null;
string storage = null;
string srcPath = Path.Combine(CommonSettings.DataFolder, name);
if (File.Exists(srcPath))
{
var storagePath = !string.IsNullOrEmpty(folder)
? string.Format("{0}/{1}", folder, name) : name;
StorageApi storageApi = new StorageApi(CommonSettings.AppKey, CommonSettings.AppSID, CommonSettings.BasePath);
storageApi.PutCreate(storagePath, null, storage, File.ReadAllBytes(srcPath));
var response = storageApi.GetIsExist(storagePath, null, storage);
if(response.FileExist.IsExist)
{
TranslationApi transApi = new TranslationApi(CommonSettings.AppKey, CommonSettings.AppSID, CommonSettings.BasePath);
Stream stream = transApi.GetTranslateDocument(name, SrcLang, ResLang, folder, storage);
if (stream != null && stream.GetType() == typeof(FileStream))
{
string outName = ((FileStream)stream).Name;
string outPath = Path.Combine(CommonSettings.OutDirectory, Path.GetFileName(outName));
using (FileStream fstr = new FileStream(outPath, FileMode.Create, FileAccess.Write))
{
stream.CopyTo(fstr);
fstr.Flush();
Console.WriteLine(string.Format("File '{0}' downloaded to: {1}", Path.GetFileName(outName), outPath));
}
}
stream.Close();
stream.Dispose();
}
}
else
throw new FileNotFoundException("File not found in the Data folder", name);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment