Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Main controller ViewerController.ashx
- /// <summary>
- /// Get document page
- /// </summary>
- /// <param name="postedData">Post data</param>
- /// <returns>Document page object</returns>
- [Action("loadDocumentPages")]
- public List<PageDescriptionEntity> LoadDocumentPages(PostedDataEntity postedData)
- {
- // get/set parameters
- string documentGuid = postedData.guid;
- if (!Path.IsPathRooted(documentGuid))
- {
- documentGuid = sTmpDir + "/" + documentGuid;
- }
- var password = (string.IsNullOrWhiteSpace(postedData.password)) ? null : postedData.password;
- // get document info options
- var documentInfoOptions = new DocumentInfoOptions();
- // set password for protected document
- documentInfoOptions.Password = password;
- // get document info container
- var documentInfoContainer = this.GetHandler().GetDocumentInfo(documentGuid, documentInfoOptions);
- return postedData.pages.Select(pageNumber =>
- {
- PageDescriptionEntity page = GetPageDescriptionEntities(documentInfoContainer.Pages[pageNumber - 1]);
- page.SetData(GetPageContent(documentInfoContainer.Pages[pageNumber - 1], password, documentGuid));
- // return loaded page object
- return page;
- }).ToList();
- }
- private PageDescriptionEntity GetPageDescriptionEntities(PageData page)
- {
- PageDescriptionEntity pageDescriptionEntity = new PageDescriptionEntity();
- pageDescriptionEntity.number = page.Number;
- pageDescriptionEntity.angle = page.Angle;
- pageDescriptionEntity.height = page.Height;
- pageDescriptionEntity.width = page.Width;
- return pageDescriptionEntity;
- }
- private string GetPageContent(PageData page, string password, string documentGuid)
- {
- if (htmlMode)
- {
- HtmlOptions htmlOptions = new HtmlOptions();
- SetOptions(htmlOptions, password, page.Number);
- // get page HTML
- return this.GetHandler().GetPages(documentGuid, htmlOptions)[0].HtmlContent;
- }
- else
- {
- ImageOptions imageOptions = new ImageOptions();
- SetOptions(imageOptions, password, page.Number);
- byte[] bytes;
- using (var memoryStream = new MemoryStream())
- {
- this.GetHandler().GetPages(documentGuid, imageOptions)[0].Stream.CopyTo(memoryStream);
- bytes = memoryStream.ToArray();
- }
- string encodedImage = Convert.ToBase64String(bytes);
- return encodedImage;
- }
- }
- private void SetOptions(HtmlOptions options, string password, int pageNumber)
- {
- options.EmbedResources = true;
- // set password for protected document
- if (!string.IsNullOrEmpty(password))
- {
- options.Password = password;
- }
- if (pageNumber != 0)
- {
- options.PageNumber = pageNumber;
- options.CountPagesToRender = 1;
- }
- options.CellsOptions.ShowGridLines = true;
- }
Add Comment
Please, Sign In to add comment