diff --git a/Telerik.Examples.Mvc/NuGet.config b/Telerik.Examples.Mvc/NuGet.config index 753bc83..dafeff5 100644 --- a/Telerik.Examples.Mvc/NuGet.config +++ b/Telerik.Examples.Mvc/NuGet.config @@ -4,6 +4,9 @@ + + + diff --git a/Telerik.Examples.Mvc/Telerik.Examples.Mvc/Controllers/Editor/WatermarkController.cs b/Telerik.Examples.Mvc/Telerik.Examples.Mvc/Controllers/Editor/WatermarkController.cs new file mode 100644 index 0000000..5535942 --- /dev/null +++ b/Telerik.Examples.Mvc/Telerik.Examples.Mvc/Controllers/Editor/WatermarkController.cs @@ -0,0 +1,114 @@ +using System; +using System.IO; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Hosting; +using Telerik.Windows.Documents.Fixed.Model; +using Telerik.Windows.Documents.Fixed.FormatProviders.Pdf.Streaming; +using Telerik.Windows.Documents.Fixed.Model.Editing; +using Telerik.Documents.Primitives; +using Telerik.Windows.Documents.Fixed.Model.ColorSpaces; +using Kendo.Core.Export; + +namespace Telerik.Examples.Mvc.Controllers.Editor +{ + public class WatermarkController : Controller + { + private readonly IWebHostEnvironment _hostingEnvironment; + + public WatermarkController(IWebHostEnvironment webHostEnvironment) + { + this._hostingEnvironment = webHostEnvironment; + } + public IActionResult Watermark() + { + return View(); + } + [HttpPost] + public ActionResult Export(EditorExportData data) + { + string fileDownloadName = data.FileName + ".pdf"; + string mimeType = "application/pdf"; + + var settings = new EditorDocumentsSettings(); + settings.HtmlImportSettings.LoadImageFromUri += HtmlImportSettings_LoadImageFromUri; + + try + { + var filestream = EditorExport.Export(data, settings).FileStream; + + RadFixedPage foregroundContentOwner = GenerateForegroundTextContent("CONFIDENTIAL"); + MemoryStream ms = new MemoryStream(); + byte[] renderedBytes = null; + + using (PdfFileSource fileSource = new PdfFileSource(filestream)) + { + using (PdfStreamWriter fileWriter = new PdfStreamWriter(ms, true)) + { + foreach (PdfPageSource pageSource in fileSource.Pages) + { + using (PdfPageStreamWriter pageWriter = fileWriter.BeginPage(pageSource.Size, pageSource.Rotation)) + { + pageWriter.WriteContent(pageSource); + + using (pageWriter.SaveContentPosition()) + { + double xCenteringTranslation = (pageSource.Size.Width - foregroundContentOwner.Size.Width) / 2; + double yCenteringTranslation = (pageSource.Size.Height - foregroundContentOwner.Size.Height) / 2; + pageWriter.ContentPosition.Translate(xCenteringTranslation, yCenteringTranslation); + pageWriter.WriteContent(foregroundContentOwner); + } + } + } + } + } + renderedBytes = ms.ToArray(); + return File(renderedBytes, mimeType, fileDownloadName); + } + catch + { + return RedirectToAction("Error", "Home"); + } + } + private void HtmlImportSettings_LoadImageFromUri(object sender, Telerik.Windows.Documents.Flow.FormatProviders.Html.LoadImageFromUriEventArgs e) + { + var uri = e.Uri; + var absoluteUrl = uri.StartsWith("http://") || uri.StartsWith("www."); + + if (!absoluteUrl) + { + var trimmedUri = uri.Trim().TrimStart(Path.AltDirectorySeparatorChar).Replace("aspnet-core" + Path.AltDirectorySeparatorChar.ToString(), "").Replace('/','\\'); + var webRootPath = _hostingEnvironment.WebRootPath; + var filePath = Path.Combine(webRootPath, trimmedUri); + + using (var fileStream = System.IO.File.OpenRead(filePath)) + { + using (var memoryStream = new MemoryStream()) + { + fileStream.CopyTo(memoryStream); + e.SetImageInfo(memoryStream.ToArray(),"png"); + } + } + } + } + private static RadFixedPage GenerateForegroundTextContent(string text) + { + Block block = new Block(); + block.BackgroundColor = new RgbColor(50, 0, 0, 0); + block.GraphicProperties.FillColor = new RgbColor(255, 0, 0); + block.TextProperties.FontSize = 120; + block.InsertText(text); + Size horizontalTextSize = block.Measure(); + double rotatedTextSquareSize = (horizontalTextSize.Width + horizontalTextSize.Height) / Math.Sqrt(2); + + RadFixedPage foregroundContentOwner = new RadFixedPage(); + foregroundContentOwner.Size = new Size(rotatedTextSquareSize, rotatedTextSquareSize); + FixedContentEditor foregroundEditor = new FixedContentEditor(foregroundContentOwner); + foregroundEditor.Position.Translate(horizontalTextSize.Height / Math.Sqrt(2), 0); + foregroundEditor.Position.Rotate(45); + foregroundEditor.DrawBlock(block); + + return foregroundContentOwner; + } + + } +} diff --git a/Telerik.Examples.Mvc/Telerik.Examples.Mvc/Telerik.Examples.Mvc.csproj b/Telerik.Examples.Mvc/Telerik.Examples.Mvc/Telerik.Examples.Mvc.csproj index 1c76b86..cdc12c8 100644 --- a/Telerik.Examples.Mvc/Telerik.Examples.Mvc/Telerik.Examples.Mvc.csproj +++ b/Telerik.Examples.Mvc/Telerik.Examples.Mvc/Telerik.Examples.Mvc.csproj @@ -21,6 +21,9 @@ + + + diff --git a/Telerik.Examples.Mvc/Telerik.Examples.Mvc/Views/Editor/Watermark.cshtml b/Telerik.Examples.Mvc/Telerik.Examples.Mvc/Views/Editor/Watermark.cshtml new file mode 100644 index 0000000..55e0d37 --- /dev/null +++ b/Telerik.Examples.Mvc/Telerik.Examples.Mvc/Views/Editor/Watermark.cshtml @@ -0,0 +1,59 @@ +@{ + ViewData["Title"] = "Editor Watermark PDF Export"; +} + +

@ViewData["Title"]

+ +@(Html.Kendo().Editor() + .Name("editor") + .HtmlAttributes(new { style = "height:440px; width: 100%; height:470px", aria_label = "editor" }) + .Tools(tools => tools + .Clear() + .ExportAs(export => { + export.Add("PDF","pdf"); + }) + ) + .ExportAs(exportAs => exportAs + .FileName("Watermark_Example") + .Proxy("Export", "Watermark") + ) + .Value(@ +
+

+ + One of the Most Beautiful Islands on Earth - Tenerife + +

+

+ + Overview + +

+

+ Tenerife is the largest and most populated island of the eight Canary Islands. It is also the most populated island of Spain, with a land area of 2,034.38 square kilometers(785 sq mi) and 904,713 inhabitants, 43 % of the total population of the Canary Islands. The archipelago's beaches, climate, and important natural attractions make it a major tourist destination with over 12 million visitors per year. +

+
+ +

+ + Trip Highlights in Tenerife + +

+
    +
  • + Trip to Loro Parque
    Our top tip is to visit the famous Loro Parque or 'Loro Park. It is a 13.5-hectare zoo on the outskirts of Puerto de la Cruz in Tenerife, Spain where it houses an extensive and diverse reserve of animal and plant species. +
    +
    +
  • +
  • + Whale and Dolphin Watching Tour 
    Another great option is to take boat excursion with almost guaranteed sightings of whales and dolphins. This is a day-long trip that includes lunch, island visits, fishing, and amazing views of ocean sceneries. +
    +
    +
  • +
  • + Teide National Park Stargazing
    Last, but not least you can take a stargazing trip to Teide National Park, the 3rd best place in the world to view stars and described by NASA as a window to the universe. +
    +
  • +
+
) +) \ No newline at end of file diff --git a/Telerik.Examples.Mvc/Telerik.Examples.Mvc/wwwroot/shared/editor/tenerife.png b/Telerik.Examples.Mvc/Telerik.Examples.Mvc/wwwroot/shared/editor/tenerife.png new file mode 100644 index 0000000..905df3b Binary files /dev/null and b/Telerik.Examples.Mvc/Telerik.Examples.Mvc/wwwroot/shared/editor/tenerife.png differ diff --git a/Telerik.Examples.Mvc/nuget/Telerik.Core.Export.Trial.2021.3.914.nupkg b/Telerik.Examples.Mvc/nuget/Telerik.Core.Export.Trial.2021.3.914.nupkg new file mode 100644 index 0000000..8516e31 Binary files /dev/null and b/Telerik.Examples.Mvc/nuget/Telerik.Core.Export.Trial.2021.3.914.nupkg differ diff --git a/Telerik.Examples.Mvc/nuget/Telerik.Documents.Fixed.Trial.2021.3.909.nupkg b/Telerik.Examples.Mvc/nuget/Telerik.Documents.Fixed.Trial.2021.3.909.nupkg new file mode 100644 index 0000000..b70eba8 Binary files /dev/null and b/Telerik.Examples.Mvc/nuget/Telerik.Documents.Fixed.Trial.2021.3.909.nupkg differ diff --git a/Telerik.Examples.Mvc/nuget/Telerik.Zip.Trial.2021.3.909.nupkg b/Telerik.Examples.Mvc/nuget/Telerik.Zip.Trial.2021.3.909.nupkg new file mode 100644 index 0000000..20b8cf0 Binary files /dev/null and b/Telerik.Examples.Mvc/nuget/Telerik.Zip.Trial.2021.3.909.nupkg differ