Winnovative HTML to PDF Converter is not just a simple HTML to PDF converter, is also a complete library which you can use to edit existing PDF documents. You can create a Document object from an existing PDF document using the DocumentDocument(String) constructor which takes the full path a PDF file as parameter or the DocumentDocument(Byte) constructor which reads the PDF from a memory buffer. There are also variants of these constructors taking an additional parameter which is the password to open the PDF document: DocumentDocument(String, String) and DocumentDocument(Byte, String).
Each type of constructor has also a variant which accepts the Winnovative Server IP address and port as parameters like DocumentDocument(String, UInt32, String) and also a variant which accepts the HTTP URL of the Winnovative Server as parameter like DocumentDocument(Boolean, String, String)
A common example of editing an existing PDF document is you to add watermarks and stamps to that 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; using System.Drawing; // Use Winnovative Namespace using WinnovativeClient; namespace WnvHtmlToPdfDemo.Controllers.PDF_Editor { public class PDF_Editor_Stamp_PDFController : Controller { private readonly Microsoft.AspNetCore.Hosting.IWebHostEnvironment m_hostingEnvironment; public PDF_Editor_Stamp_PDFController(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: PDF_Editor_Stamp_PDF public ActionResult Index() { SetCurrentViewData(); return View(); } [HttpPost] public ActionResult StampPdf(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"]; // Load the PDF document to stamp string pdfFileToStampPath = m_hostingEnvironment.ContentRootPath + "/wwwroot" + "/DemoAppFiles/Input/PDF_Files/PDF_Document.pdf"; Document pdfDocument = null; if (useTcpService) pdfDocument = new Document(serverIP, serverPort, servicePassword, pdfFileToStampPath, null); else pdfDocument = new Document(true, webServiceUrl, servicePassword, pdfFileToStampPath, null); // Set license key received after purchase to use the converter in licensed mode // Leave it not set to use the converter in demo mode pdfDocument.LicenseKey = "4W9+bn19bn5ue2B+bn1/YH98YHd3d3c="; // 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 = "Stamp_PDF.pdf"; return fileResult; } } }