Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(demos): add editor watermark with pdf export example #54

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions Telerik.Examples.Mvc/NuGet.config
Expand Up @@ -4,6 +4,9 @@
<clear /> <!-- ensure only the sources defined below are used -->
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
<add key="Telerik.UI.for.AspNet.Core.Trial.2022.2.510" value=".\\nuget" />
<add key="Telerik.Zip.Trial.2021.3.909" value=".\\nuget" />
<add key="Telerik.Core.Export.Trial.2021.3.914" value=".\\nuget" />
<add key="Telerik.Documents.Fixed.Trial.2021.3.909" value=".\\nuget" />
</packageSources>
</configuration>

@@ -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;
}

}
}
Expand Up @@ -21,6 +21,9 @@
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.0.0" />
<PackageReference Include="System.Drawing.Common" Version="5.0.2" />
<PackageReference Include="Telerik.UI.for.AspNet.Core.Trial" Version="2022.2.510" />
<PackageReference Include="Telerik.Zip.Trial" Version="2021.3.909" />
<PackageReference Include="Telerik.Documents.Fixed.Trial" Version="2021.3.909" />
<PackageReference Include="Telerik.Core.Export.Trial" Version="2021.3.914" />
</ItemGroup>

<ItemGroup>
Expand Down
@@ -0,0 +1,59 @@
@{
ViewData["Title"] = "Editor Watermark PDF Export";
}

<h3>@ViewData["Title"]</h3>

@(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(@<text>
<br/>
<p style="text-align:center;">
<span style="font-family:Verdana, Geneva, sans-serif;font-size:large;">
<strong>One of the Most Beautiful Islands on Earth - Tenerife</strong>
</span>
</p>
<p>
<span style="font-family:Verdana, Geneva, sans-serif;font-size:medium;">
<strong>Overview</strong>
</span>
</p>
<p style="font-size: small;">
<strong>Tenerife </strong>is the largest and most populated island of the eight <a href="https://en.wikipedia.org/wiki/Canary_Islands" target="_blank"> Canary Islands</a>. It is also the most populated island of <strong>Spain</strong>, with a land area of 2,034.38 square kilometers(785 sq mi) and 904,713 inhabitants, 43 % of the total population of the <strong>Canary Islands</strong>. The archipelago's beaches, climate, and important natural attractions make it a major tourist destination with over 12 million visitors per year.
</p>
<br />
<img src="@Url.Content("~/shared/editor/tenerife.png")" style="float: right;" alt="" width="350" height="206" />
<p>
<span style="font-family:Verdana, Geneva, sans-serif;font-size:medium;">
<strong>Trip Highlights in Tenerife</strong>
</span>
</p>
<ul>
<li>
<strong style="color: rgb(0,80,5);">Trip to Loro Parque </strong> <br /> Our top tip is to visit the famous <em>Loro Parque</em> 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.
<br />
<br />
</li>
<li>
<strong><span style="color: rgb(46,125,50);">Whale and Dolphin Watching Tour&nbsp;<br /></span></strong> 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.
<br />
<br />
</li>
<li>
<strong><span style="color: rgb(96,173,94);">Teide National Park Stargazing</span></strong><br />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.
<br />
</li>
</ul>
</text>)
)
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
Binary file not shown.