You are not logged in.

Change Language:

Freelancer? Consultant? Check out: WorkTrail - Time Tracking made easy
Board » Django » Python Programming » Add Page Number Field into Document Uploaded at Amazon S3 Storage

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 inDoc at Amazon S3 Storage

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

try
            {
stringfileName = "Sample.doc";
stringsaveFormat = "doc";
stringoutputFile = "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;

stringstrJSON = JsonConvert.SerializeObject(field);

                //build URI
stringstrURI = Product.BaseProductUri + "/words/" + fileName + "/insertPageNumbers";
strURI += "?storage=" + amazonS3StorageName + "&folder=" + amazonS3BucketName;
                //sign URI
stringsignedURI = 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());
returnstring.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();
returnresponse.GetResponseStream();
        }
catch (System.Net.WebExceptionwebex)
        {
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();
returnresponse.GetResponseStream();
        }
catch (System.Net.WebExceptionwebex)
        {
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];
intlen;
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; }
publicboolIsTop { get; set; }
publicboolSetPageNumberOnFirstPage { get; set; }
}

Detailed sample codes on Word page number inserting, here is the complement on Word page inserting. With this Word page processing tool, we can easily and quickly add and insert single or batch blank pages or a pages extracted from other existing Word file to the current Word document. The new inserted page can be located at any position, the beginning, the first page or the end page.





Powered by Sphene Community Tools