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
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;
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; ;
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");
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);
{
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);
}
}
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";
{
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);
}
}
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);
{
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";
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();
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);
}
}
}
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; }
}
{
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; }
}
Overview: Saaspose.Words
Saaspose.Words is a platform independent REST API used for cloud based document creation, manipulation & conversion. It allows converting document to DOC, DOCX, XPS, TIFF, PDF, HTML, SWF & many other formats. It can be used languages like .NET, Java, PHP, Ruby, Rails, Python, jQuery & many others. It can also be integrated with other cloud services to process documents. Other features include Create & modify watermark, content & formatting manipulation, mail merge abilities, reporting features.
More about Saaspose.Words
- Homepage of Saaspose.Words
- More Technical Tips by Saaspose.Words
- Ask technical questions/queries from Saaspose Support Team
Contact Information
Aspose Pty Ltd, Suite 163,
79 Longueville Road
Lane Cove, NSW, 2066
Australia
Saaspose - Your File Format Experts 2.0
sales@aspose.com
Phone: 1.214.329.1520
Fax: 866.810.9465
79 Longueville Road
Lane Cove, NSW, 2066
Australia
Saaspose - Your File Format Experts 2.0
sales@aspose.com
Phone: 1.214.329.1520
Fax: 866.810.9465
No comments:
Post a Comment