Home > Code > C# > Add Page Number Field into Document Uploaded at Amazon S3 Storage

Add Page Number Field into Document Uploaded at Amazon S3 Storage

by sher azam   on Jan 06, 2013   Category: C#   |  Views: 857    |  Points: 25   |  Gold 


This technical tip allows developers to insert page number field into the Word document 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, serialize the JSON request content, 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.

Sample Code for Inserting Page Number Field in Doc at Amazon S3 Storage

SaasposeApp.AppKey = "9a******************";
SaasposeApp.AppSID = "77*********************";
//specify product URI
Product.BaseProductUri = @"http://api.saaspose.com/v1.0";

try
{
string fileName = "Sample.doc";
string saveFormat = "doc";
string outputFile = "C:\\Output." + saveFormat;
string amazonS3StorageName = "MyAmazonS3Storage";
string amazonS3BucketName = "MyS3Bucket"; // include folder name if file is not at root folder

//serialize the JSON request content
Field field = new Field();
field.Alignment = "right";
field.Format = "{PAGE} of {NUMPAGES}";
field.IsTop = true;
field.SetPageNumberOnFirstPage = true;

string strJSON = JsonConvert.SerializeObject(field);

//build URI
string strURI = Product.BaseProductUri + "/words/" + fileName + "/insertPageNumbers";
strURI += "?storage=" + amazonS3StorageName + "&folder=" + amazonS3BucketName;
//sign URI
string signedURI = Sign(strURI);
//get response stream
Stream responseStream = ProcessCommand(signedURI, "POST", strJSON, "json");
//build URI
strURI = Product.BaseProductUri + "/words/" + fileName;
strURI += "?format=" + saveFormat + "&storage=" + amazonS3StorageName + "&folder=" + amazonS3BucketName; ;

//sign URI
signedURI = Sign(strURI);
//get response 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);
}
public static string Sign(string url)
{
try
{
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;
builder.Path = builder.Path.TrimEnd('/');
byte[] privateKey = System.Text.Encoding.UTF8.GetBytes(SaasposeApp.AppKey);

System.Security.Cryptography.HMACSHA1 algorithm = new System.Security.Cryptography.HMACSHA1(privateKey);
byte[] sequence = System.Text.ASCIIEncoding.ASCII.GetBytes(builder.Uri.AbsoluteUri);
byte[] hash = algorithm.ComputeHash(sequence);
string signature = Convert.ToBase64String(hash);
signature = signature.TrimEnd('=');
signature = System.Web.HttpUtility.UrlEncode(signature);
signature = System.Text.RegularExpressions.Regex.Replace(signature, "%[0-9a-f]{2}", e => e.Value.ToUpper());
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 Stream ProcessCommand(string strURI, string strHttpCommand, string strContent, string ContentType = "xml")
{
try
{
byte[] arr = System.Text.Encoding.UTF8.GetBytes(strContent);

Uri address = new Uri(strURI);
System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(address);
request.Method = strHttpCommand;
if (ContentType.ToLower() == "xml")
request.ContentType = "application/xml";
else
request.ContentType = "application/json";

request.ContentLength = arr.Length;

Stream dataStream = request.GetRequestStream();
dataStream.Write(arr, 0, arr.Length);
dataStream.Close();

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; }
}
public class Field
{
public string Format { get; set; }
public string Alignment { get; set; }
public bool IsTop { get; set; }
public bool SetPageNumberOnFirstPage { get; set; }
}

More about Saaspose.Words

- Homepage of Saaspose.Words: http://saaspose.com/api/words

- More Technical Tips by Saaspose.Words: http://saaspose.com/docs/display/words/1.2+-+Saaspose.Words+Examples



Post Code  |  Code Snippet Home

User Responses


No response found, be the first to review this code snippet.

Submit feedback about this code snippet

Please sign in to post feedback

Latest Posts