Guest User

Untitled

a guest
Sep 8th, 2020
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.89 KB | None | 0 0
  1. //Main controller ViewerController.ashx
  2. /// <summary>
  3. /// Get document page
  4. /// </summary>
  5. /// <param name="postedData">Post data</param>
  6. /// <returns>Document page object</returns>
  7. [Action("loadDocumentPages")]
  8. public List<PageDescriptionEntity> LoadDocumentPages(PostedDataEntity postedData)
  9. {
  10.     // get/set parameters
  11.         string documentGuid = postedData.guid;
  12.  
  13.         if (!Path.IsPathRooted(documentGuid))
  14.         {
  15.             documentGuid = sTmpDir + "/" + documentGuid;
  16.         }
  17.  
  18.         var password = (string.IsNullOrWhiteSpace(postedData.password)) ? null : postedData.password;
  19.         // get document info options
  20.         var documentInfoOptions = new DocumentInfoOptions();
  21.         // set password for protected document                
  22.         documentInfoOptions.Password = password;
  23.         // get document info container              
  24.         var documentInfoContainer = this.GetHandler().GetDocumentInfo(documentGuid, documentInfoOptions);
  25.         return postedData.pages.Select(pageNumber =>
  26.         {
  27.             PageDescriptionEntity page = GetPageDescriptionEntities(documentInfoContainer.Pages[pageNumber - 1]);
  28.             page.SetData(GetPageContent(documentInfoContainer.Pages[pageNumber - 1], password, documentGuid));
  29.             // return loaded page object
  30.             return page;
  31.         }).ToList();            
  32. }
  33.  
  34. private PageDescriptionEntity GetPageDescriptionEntities(PageData page)
  35. {
  36.         PageDescriptionEntity pageDescriptionEntity = new PageDescriptionEntity();
  37.         pageDescriptionEntity.number = page.Number;
  38.         pageDescriptionEntity.angle = page.Angle;
  39.         pageDescriptionEntity.height = page.Height;
  40.         pageDescriptionEntity.width = page.Width;
  41.         return pageDescriptionEntity;
  42. }    
  43.  
  44. private string GetPageContent(PageData page, string password, string documentGuid)
  45. {
  46.         if (htmlMode)
  47.         {
  48.         HtmlOptions htmlOptions = new HtmlOptions();
  49.         SetOptions(htmlOptions, password, page.Number);
  50.         // get page HTML              
  51.         return this.GetHandler().GetPages(documentGuid, htmlOptions)[0].HtmlContent;
  52.  
  53.         }
  54.         else
  55.         {
  56.         ImageOptions imageOptions = new ImageOptions();
  57.         SetOptions(imageOptions, password, page.Number);
  58.         byte[] bytes;
  59.         using (var memoryStream = new MemoryStream())
  60.         {  
  61.             this.GetHandler().GetPages(documentGuid, imageOptions)[0].Stream.CopyTo(memoryStream);
  62.             bytes = memoryStream.ToArray();
  63.         }
  64.         string encodedImage = Convert.ToBase64String(bytes);
  65.         return encodedImage;
  66.         }
  67. }  
  68. private void SetOptions(HtmlOptions options, string password, int pageNumber)
  69. {
  70.     options.EmbedResources = true;
  71.     // set password for protected document
  72.     if (!string.IsNullOrEmpty(password))
  73.     {
  74.         options.Password = password;
  75.     }
  76.     if (pageNumber != 0)
  77.     {
  78.         options.PageNumber = pageNumber;
  79.         options.CountPagesToRender = 1;
  80.     }
  81.     options.CellsOptions.ShowGridLines = true;
  82. }
  83.  
  84.  
Add Comment
Please, Sign In to add comment