HiQPdf Documentation

Edit PDF Documents

HiQPdf Client for .NET Core

The HiQPdf software is able to load external PDF documents and to add new objects over the existing content.

Edit PDF Demo

In this demo you can learn how to load a PDF document from a file and layout various PDF objects on top of the existing content.

The demo will make two changes to the PDF document to edit: will create an orange border for each PDF page of the loaded document and will create a canvas whose content is automatically repeated on each PDF page. The content from a HTML document will be laid out in the repeated canvas.

The HTML document does not have a background which makes visible the existing content from PDF and even more, the PNG image used in HTML is also transparent to create a very special effect.

Demo Source Code

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 HiQPdfClient;

namespace HiQPdf_Demo.Controllers
    public class EditPdfController : Controller
        IWebHostEnvironment m_hostingEnvironment;
        public EditPdfController(IWebHostEnvironment hostingEnvironment)
            m_hostingEnvironment = hostingEnvironment;

        // GET: EditPdf
        public ActionResult Index()

            return View();

        public ActionResult CreatePdf(IFormCollection collection)
            string serverIP = collection["textBoxServerIP"];
            uint serverPort = uint.Parse(collection["textBoxServerPort"]);
            string serverPassword = collection["textBoxServerPassword"];

            // the path PDF document to edit
            string pdfDocumentToEdit = m_hostingEnvironment.WebRootPath + "/DemoFiles/Pdf/WikiPdf.pdf";

            // assign a string to this variable if the PDF document to edit is password protected
            string pdfOpenPassword = null;

            // load the PDF document to edit
            PdfDocument document = PdfDocument.FromFile(serverIP, serverPort, serverPassword, pdfDocumentToEdit, pdfOpenPassword);

            // set a demo serial number
            document.SerialNumber = "YCgJMTAE-BiwJAhIB-EhlWTlBA-UEBRQFBA-U1FOUVJO-WVlZWQ==";

            #region Add an orange border to each page of the loaded PDF document

            // add an orange border to each PDF page in the loaded PDF document
            foreach (PdfPage pdfPage in document.Pages)
                float crtPdfPageWidth = pdfPage.Size.Width;
                float crtPdfPageHeight = pdfPage.Size.Height;

                // create a PdfRectangle object
                PdfRectangle pdfRectangle = new PdfRectangle(2, 2, crtPdfPageWidth - 4, crtPdfPageHeight - 4);
                pdfRectangle.LineStyle.LineWidth = 2;
                pdfRectangle.ForeColor = PdfColor.OrangeRed;

                // layout the rectangle in PDF page

            #endregion Add an orange border to each page of the loaded PDF document

            #region Layout HTML in a canvas to be repeated on each page of the loaded PDF document

            PdfPage pdfPage1 = document.Pages[0];
            float pdfPageWidth = pdfPage1.Size.Width;
            float pdfPageHeight = pdfPage1.Size.Height;

            // the width of the HTML logo in pixels
            int htmlLogoWidthPx = 400;
            // the width of the HTML logo in points
            float htmlLogoWidthPt = PdfDpiTransform.FromPixelsToPoints(htmlLogoWidthPx);
            float htmlLogoHeightPt = 100;

            // create a canvas to be repeated in the center of each PDF page
            // the canvas is a PDF container that can contain PDF objects ( HTML, text, images, etc )
            PdfRepeatCanvas repeatedCanvas = document.CreateRepeatedCanvas(new RectangleFloat((pdfPageWidth - htmlLogoWidthPt) / 2, (pdfPageHeight - htmlLogoHeightPt) / 2,
                        htmlLogoWidthPt, htmlLogoHeightPt));

            // the HTML file giving the content to be placed in the repeated canvas
            string htmlFile = "http://www.hiqpdf.com/ClientDemo" + @"/DemoFiles/Html/Logo.Html";

            // the HTML object to be laid out in repeated canvas
            PdfHtml htmlLogo = new PdfHtml(0, 0, repeatedCanvas.Width, repeatedCanvas.Height, htmlFile);
            // the browser width when rendering the HTML
            htmlLogo.BrowserWidth = htmlLogoWidthPx;
            // the HTML content will fit the destination hight which is the same with repeated canvas height
            htmlLogo.FitDestHeight = true;

            // layout the HTML object in the repeated canvas

            #endregion Layout HTML in a canvas to be repeated on each page of the loaded PDF document

                // write the PDF document to a memory buffer
                byte[] pdfBuffer = document.WriteToMemory();

                FileResult fileResult = new FileContentResult(pdfBuffer, "application/pdf");
                fileResult.FileDownloadName = "EditPdf.pdf";

                return fileResult;

        private void SetCrtPageUri()
            HttpRequest httpRequest = this.ControllerContext.HttpContext.Request;
            UriBuilder uriBuilder = new UriBuilder();
            uriBuilder.Scheme = httpRequest.Scheme;
            uriBuilder.Host = httpRequest.Host.Host;
            if (httpRequest.Host.Port != null)
                uriBuilder.Port = (int)httpRequest.Host.Port;
            uriBuilder.Path = httpRequest.PathBase.ToString() + httpRequest.Path.ToString();
            uriBuilder.Query = httpRequest.QueryString.ToString();

            ViewData["CrtPageUri"] = uriBuilder.Uri.AbsoluteUri;
See Also

Other Resources