Skip to content
Permalink
Branch: master
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
115 lines (90 sloc) 4.28 KB
using Aspose.Storage.Cloud.Sdk.Api;
using Aspose.Storage.Cloud.Sdk.Model.Requests;
using Aspose.Words.Cloud.Sdk;
using Aspose.Words.Cloud.Sdk.Model;
using Aspose.Words.Cloud.Sdk.Model.Requests;
using System.IO;
namespace AsposeWordsCloudSDKExamples
{
class InsertHeadersAndPageNumbers
{
private WordsApi wordsApi;
private StorageApi storageApi;
private string fileName = "Sample-Document.docx";
public InsertHeadersAndPageNumbers(WordsApi wordsApi, StorageApi storageApi)
{
this.wordsApi = wordsApi;
this.storageApi = storageApi;
}
public void insertHeadersAndPageNumbers()
{
// Upload Input file to Cloud Storage
uploadFileToCloudStorage();
// Insert Headers
insertHeaders();
// Set DifferentFirstPageHeaderFooter property to true
setDifferentFirstPageHeaderFooterProperty();
// Add Text to Headers
addTextToHeaders();
// Apply Formatting to Header Text
applyFormattingToHeaderText();
// Insert Page Numbers
insertPageNumbers();
}
private void uploadFileToCloudStorage()
{
// Upload source document to Cloud Storage
PutCreateRequest request = new PutCreateRequest(fileName, File.OpenRead(WordsExamples.PathToDataFiles + @"\DocumentElements\HeaderFooters\" + fileName), null, null);
storageApi.PutCreate(request);
}
// Insert headers
private void insertHeaders()
{
string folder = null; // File exists at the root of the storage
// Insert Header for the first page
var putHeaderFooterRequest = new PutHeaderFooterRequest(fileName, "HeaderFirst", folder);
var actual = wordsApi.PutHeaderFooter(putHeaderFooterRequest);
// Insert Header for all other pages
putHeaderFooterRequest = new PutHeaderFooterRequest(fileName, "HeaderPrimary", folder);
actual = wordsApi.PutHeaderFooter(putHeaderFooterRequest);
}
// Update page setup of section
private void setDifferentFirstPageHeaderFooterProperty()
{
var sectionIndex = 0;
var body = new PageSetup
{
DifferentFirstPageHeaderFooter = true
};
var pageSetupRequest = new UpdateSectionPageSetupRequest(fileName, sectionIndex, body);
var actual = wordsApi.UpdateSectionPageSetup(pageSetupRequest);
}
// Add Text to Headers
private void addTextToHeaders()
{
var run = new Run { Text = "ASPOSE" };
var runRequest = new PutRunRequest(fileName, "sections/0/headersfooters/1/paragraphs/0", run);
var actual = wordsApi.PutRun(runRequest);
run = new Run { Text = "Cloud File Format APIs" };
runRequest = new PutRunRequest(fileName, "sections/0/headersfooters/0/paragraphs/0", run);
actual = wordsApi.PutRun(runRequest);
}
// Apply Formatting to Header Text
private void applyFormattingToHeaderText()
{
var runIndex = 0;
var fontDto = new Font { Bold = true, Name = "Verdana", Size = 14, Color = new XmlColor { Web = "#1000ff" } };
var documentParagraphRunFontRequest = new PostDocumentParagraphRunFontRequest(fileName, fontDto, "sections/0/headersfooters/1/paragraphs/0", runIndex);
var actual = wordsApi.PostDocumentParagraphRunFont(documentParagraphRunFontRequest);
documentParagraphRunFontRequest = new PostDocumentParagraphRunFontRequest(fileName, fontDto, "sections/0/headersfooters/0/paragraphs/0", runIndex);
actual = wordsApi.PostDocumentParagraphRunFont(documentParagraphRunFontRequest);
}
// Insert Page Numbers
private void insertPageNumbers()
{
var body = new PageNumber { Alignment = "right", Format = "{PAGE} of {NUMPAGES}", IsTop = true, SetPageNumberOnFirstPage = true };
var insertPageNumbersRequest = new PostInsertPageNumbersRequest(fileName, body);
var actual = wordsApi.PostInsertPageNumbers(insertPageNumbersRequest);
}
}
}
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.