Skip to content

Commit

Permalink
Merge pull request #150 from aliahmedgroupdocs/master
Browse files Browse the repository at this point in the history
MVC Modern UI v1.2
  • Loading branch information
Ali Ahmed Sahi authored Jul 24, 2017
2 parents c062732 + b7bdfd7 commit e40a3aa
Show file tree
Hide file tree
Showing 8 changed files with 178 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@

var ngApp = angular.module('GroupDocsViewer', ['ngMaterial', 'ngResource']);

ngApp.constant('FilePath', 'http://groupdocs.com/images/banner/carousel2/signature.png');
ngApp.constant('FilePath', '');

ngApp.constant('Watermark', {
Text: "Watermark Text",
Color: 16711680,
Position: 'Diagonal',
Width: null,
Opacity : 255
});

ngApp.factory('FilesFactory', function ($resource) {
return $resource('/files', {}, {
Expand All @@ -22,32 +30,53 @@ ngApp.factory('DocumentPagesFactory', function ($resource) {
});
});

ngApp.controller('ToolbarController', function ToolbarController($rootScope, $scope, $mdSidenav, FilePath) {
ngApp.controller('ToolbarController', function ToolbarController($rootScope, $scope, $mdSidenav, Watermark, FilePath) {
$scope.toggleLeft = function () {
$mdSidenav('left').toggle().then(function () {
$rootScope.$broadcast('md-sidenav-toggle-complete', $mdSidenav('left'));
});
};

$scope.watermark = {
Text: Watermark.Text,
Color: Watermark.Color,
Position: Watermark.Position,
Width: Watermark.Width,
Opacity: Watermark.Opacity
};
$scope.$on('selected-file-changed', function ($event, selectedFile) {
$scope.selectedFile = selectedFile;
$rootScope.selectedFile = selectedFile;
});


$scope.nextDocument = function () {
if ($rootScope.list.indexOf($rootScope.selectedFile) + 1 == $rootScope.list.length) {
$rootScope.$broadcast('selected-file-changed', $rootScope.list[0]);
}
else {
$rootScope.$broadcast('selected-file-changed', $rootScope.list[$rootScope.list.indexOf($rootScope.selectedFile) + 1]);
}
};
$scope.previousDocument = function () {
if ($rootScope.list.indexOf($rootScope.selectedFile) - 1 == -1) {
$rootScope.$broadcast('selected-file-changed', $rootScope.list[$rootScope.list.length-1]);
}
else {
$rootScope.$broadcast('selected-file-changed', $rootScope.list[$rootScope.list.indexOf($rootScope.selectedFile) - 1]);
}
};
});

ngApp.controller('ThumbnailsController',
function ThumbnailsController($rootScope, $scope, $sce, $mdSidenav, DocumentPagesFactory, FilePath) {
function ThumbnailsController($rootScope, $scope, $sce, $mdSidenav, DocumentPagesFactory, FilePath, Watermark) {
$scope.isLeftSidenavVislble = false;
if (FilePath) {
$scope.selectedFile = FilePath;
$rootScope.selectedFile = FilePath;
$scope.docInfo = DocumentPagesFactory.query({
filename: FilePath
});

}
$scope.$on('selected-file-changed', function (event, selectedFile) {
$scope.selectedFile = selectedFile;
$rootScope.selectedFile = selectedFile;
$scope.docInfo = DocumentPagesFactory.query({
filename: selectedFile
});
Expand All @@ -71,61 +100,91 @@ ngApp.controller('ThumbnailsController',
};
$scope.createThumbnailUrl = function (selectedFile, itemNumber) {
if ($scope.isLeftSidenavVislble) {
return $sce.trustAsResourceUrl('/page/image?width=300&file=' + selectedFile + '&page=' + itemNumber);
return $sce.trustAsResourceUrl('/page/image?width=300&file='+ selectedFile
+ '&page=' + itemNumber
+ '&watermarkText=' + Watermark.Text
+ '&watermarkColor=' + Watermark.Color
+ '&watermarkPosition=' + Watermark.Position
+ '&watermarkWidth=' + Watermark.Width
+ '&watermarkOpacity=' + Watermark.Opacity);
}
};
$scope.createAttachmentThumbnailPageUrl = function (selectedFile,attachment,itemNumber) {
if ($scope.isLeftSidenavVislble) {
return $sce.trustAsResourceUrl('/attachment/image?width=300&file=' + selectedFile + '&attachment=' + attachment + '&page=' + itemNumber);
return $sce.trustAsResourceUrl('/attachment/image?width=300&file='+ selectedFile
+ '&attachment=' + attachment
+ '&page=' + itemNumber
+ '&watermarkText=' + Watermark.Text
+ '&watermarkColor=' + Watermark.Color
+ '&watermarkPosition=' + Watermark.Position
+ '&watermarkWidth=' + Watermark.Width
+ '&watermarkOpacity=' + Watermark.Opacity);
}
};


}
);

ngApp.controller('PagesController',
function ThumbnailsController($scope, $sce, $document, DocumentPagesFactory, FilePath) {
function ThumbnailsController($rootScope, $scope, $sce, $document, DocumentPagesFactory, FilePath, Watermark) {
if (FilePath) {
$scope.selectedFile = FilePath;
$rootScope.selectedFile = FilePath;
$scope.docInfo = DocumentPagesFactory.query({
filename: FilePath
});

}
$scope.$on('selected-file-changed', function (event, selectedFile) {
$scope.selectedFile = selectedFile;
$rootScope.selectedFile = selectedFile;
$scope.docInfo = DocumentPagesFactory.query({
filename: selectedFile
});
});

$scope.createPageUrl = function (selectedFile, itemNumber) {
return $sce.trustAsResourceUrl('/page/html?file=' + selectedFile + '&page=' + itemNumber);

return $sce.trustAsResourceUrl('/page/html?file='
+ selectedFile + '&page=' + itemNumber
+ '&watermarkText=' + Watermark.Text
+ '&watermarkColor=' + Watermark.Color
+ '&watermarkPosition=' + Watermark.Position
+ '&watermarkWidth=' + Watermark.Width
+ '&watermarkOpacity=' + Watermark.Opacity);
};
$scope.createAttachmentPageUrl = function (selectedFile,attachmentName, itemNumber) {
return $sce.trustAsResourceUrl('/attachment/html?file=' + selectedFile + '&attachment=' + attachmentName + '&page=' + itemNumber);
return $sce.trustAsResourceUrl('/attachment/html?file=' + selectedFile
+ '&attachment=' + attachmentName
+ '&page=' + itemNumber
+ '&watermarkText=' + Watermark.Text
+ '&watermarkColor=' + Watermark.Color
+ '&watermarkPosition=' + Watermark.Position
+ '&watermarkWidth=' + Watermark.Width
+ '&watermarkOpacity=' + Watermark.Opacity);
};
$scope.onLoad = function () {
};
}
);

ngApp.controller('AvailableFilesController', function AvailableFilesController($rootScope, $scope, FilesFactory,DocumentPagesFactory, FilePath) {
ngApp.controller('AvailableFilesController', function AvailableFilesController($rootScope, $scope, FilesFactory, DocumentPagesFactory, FilePath) {
$rootScope.list = FilesFactory.query();
if (FilePath) {
$scope.selectedFile = FilePath;
$rootScope.list = [FilePath];
$rootScope.selectedFile = $rootScope.list[0];
$rootScope.$broadcast('selected-file-changed', $rootScope.selectedFile);
$scope.docInfo = DocumentPagesFactory.query({
filename: FilePath
});
}
}

$scope.onOpen = function () {
$scope.list = FilesFactory.query();
$rootScope.list = FilesFactory.query();

};

$scope.onChange = function ($event) {
$rootScope.$broadcast('selected-file-changed', $scope.selectedFile);
$scope.onChange = function (item) {
$rootScope.$broadcast('selected-file-changed', item);
};

});


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class AttachmentHtmlController : Controller
{
private static string _cachePath = AppDomain.CurrentDomain.GetData("DataDirectory") + "\\cache";
[Route("")]
public ActionResult Get(string file, string attachment, int page)
public ActionResult Get(string file, string attachment, int page, string watermarkText, int? watermarkColor, WatermarkPosition? watermarkPosition, int? watermarkWidth, byte watermarkOpacity)
{
var attachmentPath = "cache\\"+ Path.GetFileNameWithoutExtension(file) + Path.GetExtension(file).Replace(".", "_") + "\\attachments\\" + attachment;
ViewerHtmlHandler handler = Utils.CreateViewerHtmlHandler();
Expand All @@ -30,6 +30,8 @@ public ActionResult Get(string file, string attachment, int page)
o.PageNumber = page;
o.CountPagesToRender = 1;
o.HtmlResourcePrefix = "/attachment/resource?file="+file+"&attachment="+attachment+"&page="+page+"&resource=";
if (watermarkText != "")
o.Watermark = Utils.GetWatermark(watermarkText, watermarkColor, watermarkPosition, watermarkWidth, watermarkOpacity);
string fullHtml = "";
var attachmentFile = _cachePath +"\\" + Path.GetFileNameWithoutExtension(file) + Path.GetExtension(file).Replace(".", "_") + "\\attachments";
if (Directory.Exists(attachmentFile.Replace(@"\\", @"\")))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace Viewer_Modren_UI.Controllers
public class AttachmentImageController : Controller
{
[Route("")]
public ActionResult Get(int? width, int? height, string file,string attachment, int page)
public ActionResult Get(int? width, int? height, string file,string attachment, int page, string watermarkText, int? watermarkColor, WatermarkPosition? watermarkPosition, int? watermarkWidth, byte watermarkOpacity)
{
ViewerImageHandler handler = Utils.CreateViewerImageHandler();
ImageOptions o = new ImageOptions();
Expand All @@ -26,6 +26,8 @@ public ActionResult Get(int? width, int? height, string file,string attachment,
o.PageNumbersToRender = pageNumberstoRender;
o.PageNumber = page;
o.CountPagesToRender = 1;
if (watermarkText != "")
o.Watermark = Utils.GetWatermark(watermarkText, watermarkColor, watermarkPosition, watermarkWidth, watermarkOpacity);
if (width.HasValue)
{
o.Width = Convert.ToInt32(width);
Expand All @@ -40,7 +42,7 @@ public ActionResult Get(int? width, int? height, string file,string attachment,
// Iterate over the attachments collection
foreach (AttachmentBase attachmentBase in info.Attachments.Where(x=>x.Name == attachment))
{
List<PageImage> pages = handler.GetPages(attachmentBase);
List<PageImage> pages = handler.GetPages(attachmentBase,o);
foreach (PageImage attachmentPage in pages.Where(x=>x.PageNumber == page)) { stream = attachmentPage.Stream; };

}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
using GroupDocs.Viewer.Handler;
using GroupDocs.Viewer.Domain;
using GroupDocs.Viewer.Domain.Options;
using GroupDocs.Viewer.Handler;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Viewer_Modren_UI.Helpers;

Expand All @@ -13,14 +12,17 @@ namespace Viewer_Modren_UI.Controllers
public class DownloadPdfController : Controller
{
[Route("")]
public ActionResult Get(string file)
public ActionResult Get(string file, string watermarkText, int? watermarkColor, WatermarkPosition? watermarkPosition, int? watermarkWidth, byte watermarkOpacity)
{
ViewerHtmlHandler handler = Utils.CreateViewerHtmlHandler();

Stream pdf = null;
try
{
pdf = handler.GetPdfFile(file).Stream;
PdfFileOptions o = new PdfFileOptions();
if(watermarkText!="")
o.Watermark = Utils.GetWatermark(watermarkText, watermarkColor, watermarkPosition, watermarkWidth, watermarkOpacity);
pdf = handler.GetPdfFile(file,o).Stream;
}
catch (Exception x)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,31 @@
using System.Web;
using System.Web.Mvc;
using Viewer_Modren_UI.Helpers;
using static Viewer_Modren_UI.Helpers.Utils;

namespace Viewer_Modren_UI.Controllers
{
[RoutePrefix("page/html")]
public class PageHtmlController : Controller
{
[Route("")]
public ActionResult Get(string file, int page)
public ActionResult Get(string file, int page, string watermarkText, int? watermarkColor, WatermarkPosition? watermarkPosition, int? watermarkWidth, byte watermarkOpacity)
{

if (Utils.IsValidUrl(file))
file = Utils.DownloadToStorage(file);
ViewerHtmlHandler handler = Utils.CreateViewerHtmlHandler();

List<int> pageNumberstoRender = new List<int>();
pageNumberstoRender.Add(page);
HtmlOptions o = new HtmlOptions();
o.PageNumbersToRender = pageNumberstoRender;
o.PageNumber = page;
o.CountPagesToRender = 1;
o.HtmlResourcePrefix = "/page/resource?file="+file+"&page="+page+"&resource=";
if(watermarkText!="")
o.Watermark = Utils.GetWatermark(watermarkText, watermarkColor, watermarkPosition, watermarkWidth, watermarkOpacity);

List<PageHtml> list = Utils.LoadPageHtmlList(handler, file, o);
string fullHtml = "";
foreach (PageHtml pageHtml in list.Where(x => x.PageNumber == page)) { fullHtml = pageHtml.HtmlContent; };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace Viewer_Modren_UI.Controllers
public class PageImageController : Controller
{
[Route("")]
public ActionResult Get(int? width, int? height,string file, int page)
public ActionResult Get(int? width, int? height,string file, int page, string watermarkText, int? watermarkColor, WatermarkPosition? watermarkPosition, int? watermarkWidth, byte watermarkOpacity)
{
if (Utils.IsValidUrl(file))
file = Utils.DownloadToStorage(file);
Expand All @@ -28,6 +28,8 @@ public ActionResult Get(int? width, int? height,string file, int page)
o.PageNumbersToRender = pageNumberstoRender;
o.PageNumber = page;
o.CountPagesToRender = 1;
if(watermarkText!="")
o.Watermark = Utils.GetWatermark(watermarkText, watermarkColor, watermarkPosition, watermarkWidth, watermarkOpacity);
if (width.HasValue)
{
o.Width = Convert.ToInt32(width);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
using GroupDocs.Viewer;
using GroupDocs.Viewer.Config;
using GroupDocs.Viewer.Converter.Options;
using GroupDocs.Viewer.Domain;
using GroupDocs.Viewer.Domain.Containers;
using GroupDocs.Viewer.Domain.Html;
using GroupDocs.Viewer.Domain.Image;
using GroupDocs.Viewer.Handler;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
Expand Down Expand Up @@ -42,6 +44,7 @@ public static ViewerConfig CreateViewerConfig()
};
return cfg;
}

public static List<PageHtml> LoadPageHtmlList(ViewerHtmlHandler handler, String filename, HtmlOptions options)
{
try
Expand Down Expand Up @@ -222,6 +225,59 @@ public static List<PageHtml> LoadAttachmentHtmlList(ViewerHtmlHandler handler, S
}
}

public static Watermark GetWatermark(string watermarkText, int? watermarkColor, WatermarkPosition? watermarkPosition, float? watermarkWidth, byte watermarkOpacity)
{
if (string.IsNullOrWhiteSpace(watermarkText))
return null;

byte red = 0;
byte green = 0;
byte blue = 0;
byte opacity = 255;
if (watermarkColor.HasValue)
{
red = Color.FromArgb(watermarkColor.Value).R;
green = Color.FromArgb(watermarkColor.Value).G;
blue = Color.FromArgb(watermarkColor.Value).B;
opacity = watermarkOpacity;
}
return new Watermark(watermarkText)
{
Color = watermarkColor.HasValue
? Color.FromArgb(opacity, red, green, blue)
: Color.Red,
Position = ToWatermarkPosition(watermarkPosition),
Width = watermarkWidth,
FontName = "MS Gothic"
};
}
private static GroupDocs.Viewer.Domain.WatermarkPosition? ToWatermarkPosition(WatermarkPosition? watermarkPosition)
{
if (!watermarkPosition.HasValue)
return GroupDocs.Viewer.Domain.WatermarkPosition.Diagonal;

switch (watermarkPosition.Value)
{
case WatermarkPosition.Diagonal:
return GroupDocs.Viewer.Domain.WatermarkPosition.Diagonal;
case WatermarkPosition.TopLeft:
return GroupDocs.Viewer.Domain.WatermarkPosition.TopLeft;
case WatermarkPosition.TopCenter:
return GroupDocs.Viewer.Domain.WatermarkPosition.TopCenter;
case WatermarkPosition.TopRight:
return GroupDocs.Viewer.Domain.WatermarkPosition.TopRight;
case WatermarkPosition.BottomLeft:
return GroupDocs.Viewer.Domain.WatermarkPosition.BottomLeft;
case WatermarkPosition.BottomCenter:
return GroupDocs.Viewer.Domain.WatermarkPosition.BottomCenter;
case WatermarkPosition.BottomRight:
return GroupDocs.Viewer.Domain.WatermarkPosition.BottomRight;
default:
throw new ArgumentOutOfRangeException();
}
}


}


Expand Down
Loading

0 comments on commit e40a3aa

Please sign in to comment.