Winnovative HTML to PDF Converter allows you to add watermarks and stamps to generated PDF document. The watermarks and stamps are implemented using WinnovativeClientTemplate objects which are repeated in each page of the generated PDF document. In a template you can add any PDF element that you can normally add in a PDF page, including HtmlToPdfElement objects or HtmlToPdfVariableElement.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Hosting;
// Use Winnovative Namespace
using WinnovativeClient;
namespace WnvHtmlToPdfDemo.Controllers.HTML_to_PDF.Watermarks_Stamps
{
public class Add_Watermarks_StampsController : Controller
{
private readonly Microsoft.AspNetCore.Hosting.IWebHostEnvironment m_hostingEnvironment;
public Add_Watermarks_StampsController(IWebHostEnvironment hostingEnvironment)
{
m_hostingEnvironment = hostingEnvironment;
}
private void SetCurrentViewData()
{
ViewData["ContentRootPath"] = m_hostingEnvironment.ContentRootPath + "/wwwroot";
HttpRequest request = this.ControllerContext.HttpContext.Request;
UriBuilder uriBuilder = new UriBuilder();
uriBuilder.Scheme = request.Scheme;
uriBuilder.Host = request.Host.Host;
if (request.Host.Port != null)
uriBuilder.Port = (int)request.Host.Port;
uriBuilder.Path = request.PathBase.ToString() + request.Path.ToString();
uriBuilder.Query = request.QueryString.ToString();
ViewData["CurrentPageUrl"] = uriBuilder.Uri.AbsoluteUri;
}
// GET: Add_Watermarks_Stamps
public ActionResult Index()
{
SetCurrentViewData();
return View();
}
[HttpPost]
public ActionResult ConvertHtmlToPdf(IFormCollection collection)
{
// Get the server options
string serverIP = collection["textBoxServerIP"];
uint serverPort = uint.Parse(collection["textBoxServerPort"]);
string servicePassword = collection["textBoxServicePassword"];
bool useServicePassword = servicePassword.Length > 0;
bool useTcpService = collection["ServerType"] == "radioButtonUseTcpService";
string webServiceUrl = collection["textBoxWebServiceUrl"];
// Create the HTML to PDF converter object
HtmlToPdfConverter htmlToPdfConverter = null;
if (useTcpService)
htmlToPdfConverter = new HtmlToPdfConverter(serverIP, serverPort);
else
htmlToPdfConverter = new HtmlToPdfConverter(true, webServiceUrl);
// Set optional service password
if (useServicePassword)
htmlToPdfConverter.ServicePassword = servicePassword;
// Set license key received after purchase to use the converter in licensed mode
// Leave it not set to use the converter in demo mode
htmlToPdfConverter.LicenseKey = "4W9+bn19bn5ue2B+bn1/YH98YHd3d3c=";
// Convert a HTML page to a PDF document object
Document pdfDocument = htmlToPdfConverter.ConvertUrlToPdfDocumentObject(collection["urlTextBox"]);
// Get the stamp width and height
float stampWidth = float.Parse(collection["stampWidthTextBox"]);
float stampHeight = float.Parse(collection["stampHeightTextBox"]);
// Center the stamp at the top of PDF page
float stampXLocation = (pdfDocument.GetPage(0).PageSize.Width - stampWidth) / 2;
float stampYLocation = 0;
// Create the stamp template to be repeated in each PDF page
Template stampTemplate = pdfDocument.AddTemplate(stampXLocation, stampYLocation, stampWidth, stampHeight);
// Create the HTML element to add in stamp template
HtmlToPdfElement stampHtmlElement = new HtmlToPdfElement(collection["htmlStringTextBox"], collection["baseUrlTextBox"]);
// Set the HTML viewer width for the HTML added in stamp
stampHtmlElement.HtmlViewerWidth = 600;
// Fit the HTML content in stamp template
stampHtmlElement.FitWidth = true;
stampHtmlElement.FitHeight = true;
// Add HTML to stamp template
stampTemplate.AddElement(stampHtmlElement);
// Save the PDF document in a memory buffer
byte[] outPdfBuffer = pdfDocument.Save();
// Send the PDF file to browser
FileResult fileResult = new FileContentResult(outPdfBuffer, "application/pdf");
fileResult.FileDownloadName = "Watermarks_and_Stamps.pdf";
return fileResult;
}
}
}