DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

David has posted 34 posts at DZone. View Full User Profile

OLE Data Extraction from Specific Drawing Object at Amazon S3 Storage

01.16.2013
| 3305 views |
  • submit to reddit
SaasposeApp.AppKey = "9a******************";
            SaasposeApp.AppSID = "77*********************";
            //specify product URI
            Product.BaseProductUri = @"http://api.saaspose.com/v1.0";
            try
            {
                int drawingObjectIndex = 1;
                string fileName = "SampleDrawingObject.doc";
                string saveFormat = "xls"; //Choose from jpeg, tiff, png and bmp
                string outputFile = "C:\\OutputDrawingObject." + saveFormat;
                string amazonS3StorageName = "MyAmazonS3Storage";
                string amazonS3BucketName = "MyS3Bucket"; // include folder name if file is not at root folder 

                //build URI
                string strURI = Product.BaseProductUri + "/words/" + fileName + "/drawingObjects/" + drawingObjectIndex;
                strURI += "/oleData?storage=" + amazonS3StorageName + "&folder=" + amazonS3BucketName;

                //sign URI
                string signedURI = Sign(strURI);

                //get response stream
                Stream responseStream = ProcessCommand(signedURI, "GET");

                using (Stream fileStream = System.IO.File.OpenWrite(outputFile))
                {
                    CopyStream(responseStream, fileStream);
                }
                responseStream.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            } 

//Following are the required methods and classes 

        public static string Sign(string url)
        {
            try
            {
                // Add appSID parameter.
                UriBuilder builder = new UriBuilder(url);
                if (builder.Query != null && builder.Query.Length > 1)
                    builder.Query = builder.Query.Substring(1) + "&appSID=" + SaasposeApp.AppSID;
                else
                    builder.Query = "appSID=" + SaasposeApp.AppSID;
                // Remove final slash here as it can be added automatically.
                builder.Path = builder.Path.TrimEnd('/');
                // Compute the hash.

                byte[] privateKey = System.Text.Encoding.UTF8.GetBytes(SaasposeApp.AppKey);

                System.Security.Cryptography.HMACSHA1 algorithm = new System.Security.Cryptography.HMACSHA1(privateKey);
                //System.Text.ASCIIEncoding
                byte[] sequence = System.Text.ASCIIEncoding.ASCII.GetBytes(builder.Uri.AbsoluteUri);
                byte[] hash = algorithm.ComputeHash(sequence);
                string signature = Convert.ToBase64String(hash);
                // Remove invalid symbols.
                signature = signature.TrimEnd('=');
                signature = System.Web.HttpUtility.UrlEncode(signature);
                // Convert codes to upper case as they can be updated automatically.
                signature = System.Text.RegularExpressions.Regex.Replace(signature, "%[0-9a-f]{2}", e => e.Value.ToUpper());
                
                // Add the signature to query string.
                return string.Format("{0}&signature={1}", builder.Uri.AbsoluteUri, signature);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public static Stream ProcessCommand(string strURI, string strHttpCommand)
        {
            try
            {
                Uri address = new Uri(strURI);
                System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(address);
                request.Method = strHttpCommand;
                request.ContentType = "application/json";
                request.ContentLength = 0;
                System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
                return response.GetResponseStream();
            }
            catch (System.Net.WebException webex)
            {
                throw new Exception(webex.Message);
            }
            catch (Exception Ex)
            {
                throw new Exception(Ex.Message);
            }
        }
        public static void CopyStream(Stream input, Stream output)
        {
            try
            {
                byte[] buffer = new byte[8 * 1024];
                int len;
                while ((len = input.Read(buffer, 0, buffer.Length)) > 0)
                {
                    output.Write(buffer, 0, len);
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
    }
    public class Product
    {
       public static string BaseProductUri { get; set; }
    }
    public class SaasposeApp
    {
       public static string AppSID { get; set; }
       public static string AppKey { get; set; }
    }
This technical tip allows developers to extract ole data from a specific drawing object using Saaspose.Words REST API in your .NET applications. You need to upload file to Amazon S3 storage before running this example.  Some important steps for performing this task are to specify product URI, Choose from jpeg, tiff, png and bmp, build URI, sign URI, get response stream, required methods and classes are also given in detail. Please check Saaspose example section for more details on how to upload files to Amazon S3 storage.