HiQPdf Next Core PDF API for .NET

HiQPdf Next Core PDF API for .NET

The Core component provides the API for creating, manipulating and securing PDF documents programmatically. You can create PDFs by adding text, images and other elements, then apply encryption, digital signatures, permissions, headers, footers and stamps.

You can deploy the library on a variety of Windows and Linux platforms, including Azure App Service and Functions or Docker.

Download Now
Online Demo
Documentation

The Core PDF API component of the HiQPdf Next Library for .NET provides the API for programmatically creating, manipulating and securing PDF documents. You can create PDF documents by adding text, images and other graphical elements and then enhance them with features such as encryption, permissions, digital signatures, custom headers, footers, stamps and other advanced processing options. You can see the list of all HiQPdf Next components on the library page.

Compatibility Platforms

HiQPdf Next for .NET can run on a variety of Windows and Linux platforms in web, console and desktop applications across all modern .NET platforms. The library components can be used in Azure App Service and Azure Functions environments on both Windows and Linux. Deployment to Docker Windows and Linux containers is also supported.
The .NET library targets .NET Standard 2.0, which makes it compatible with a wide range of .NET Core and .NET Framework applications.

Getting Started

The online documentation contains Getting Started guides for Windows, Linux, Azure App Service and Azure Functions, with detailed instructions for integrating the library into your application and complete C# examples for each important feature of the library.
Before running an application using HiQPdf Next Library for .NET, please review the online documentation and the installation guide below for the specific requirements of each platform.
You can see the current capabilities of the library by checking the online demo application for this library and the API reference in the online documentation.

Download Demo Application

You can also download a free trial package for .NET, which includes an ASP.NET demo application project with complete C# source code as a starting point for experimenting with your own usage scenarios.
Running the samples in the demo application that involve HTML to PDF conversion features on Linux platforms might require installing some dependency packages. The documentation includes an entire section dedicated to building, publishing and running the demo application on multiple platforms.

NuGet Packages

To use the Core component in applications targeting Windows, reference the HiQPdf.Next.Core.Windows NuGet Package. For applications targeting Linux, reference the HiQPdf.Next.Core.Linux NuGet Package.
There is also a multiplatform metapackage that references both the Windows and Linux Core packages: HiQPdf.Next.Core.
The Core API also includes the classes used for HTML to PDF and HTML to Image conversion, as well as for HTML headers, footers and stamps, because these are required by other components in the suite. To use these classes, you must reference the additional Core Runtime package for your target platform. This is handled automatically when installing the HTML to PDF Converter NuGet package, which is the recommended approach rather than adding the Core and Core Runtime packages separately. You can find more details on the HiQPdf Next HTML to PDF Converter for .NET component page.

Installation

The Core PDF API component does not require additional dependency installation if you are not using HTML to PDF conversion features.

HiQPdf.Next Namespace

All components of the HiQPdf Next for .NET library share the same HiQPdf.Next namespace and can be used together in the same application. To use the library in your own code, add the using directive at the top of your C# source file, as shown below.
// Include the HiQPdf.Next namespace at the top of your C# file
using HiQPdf.Next;

Sample Code

After adding the NuGet package reference to your project, use the sample code below to create a PDF document with a text element using a standard font, then save it to a memory buffer that you can either write to a file or send to the browser for download.
PdfDocumentCreateSettings pdfCreateSettings = new PdfDocumentCreateSettings()
{
    PageSize = PdfPageSize.A4,
    PageOrientation = PdfPageOrientation.Portrait,
    Margins = new PdfMargins(36, 36, 36, 36)
};

// Create a new PDF document with the specified settings
using PdfDocument pdfDocument = new PdfDocument(pdfCreateSettings);

// Create a standard Helvetica font
PdfFont fontHelvetica = PdfFontManager.CreateStandardFont(PdfStandardFont.Helvetica, 16f, PdfFontStyle.Normal, PdfColor.Blue);

// Add a title using the Helvetica font
PdfTextElement pdfText = new PdfTextElement("Hello World !!!", fontHelvetica)
{
    X = 10,
    Y = 10
};
PdfTextRenderInfo textRenderInfo = pdfDocument.AddText(pdfText);

// Save to memory buffer
byte[] outPdfBuffer = pdfDocument.Save();

Features List

HiQPdf Next for .NET offers advanced options for converting HTML to PDF and HTML to images. The library is more than just an HTML to PDF converter. It can also automatically generate a PDF document outline with bookmarks, set security permissions, password protect and digitally sign the generated PDF document.

Create, edit and merge PDF documents

You can create PDF documents by adding text, images and other elements and then enhance them with features such as encryption, permissions, digital signatures, custom headers, footers and stamps.

Available on Both Windows and Linux Platforms

HiQPdf Next for .NET can run on both Windows 64-bit and Linux 64-bit platforms. There are different NuGet packages for Windows and Linux, including the same .NET library but with different native runtimes. For Windows, the minimum required version is Windows 10 or Windows Server 2016.

Built for .NET Standard 2.0 for Maximum Compatibility

The .NET library targets .NET Standard 2.0, making it compatible with a wide range of .NET Core and .NET Framework applications. It is compatible with .NET 10.0, 9.0, 8.0, 7.0, 6.0, .NET Standard 2.0 and .NET Framework 4.6.2 to 4.8.1.

Fully Compatible with Azure App Service and Azure Functions on Both Windows and Linux

The converter can run without restrictions in your Azure App Service and Azure Functions .NET Core applications targeting both Windows and Linux platforms. Web fonts and other features are fully supported by HiQPdf Next for .NET. Online documentation offers detailed usage instructions for Azure applications targeting both Windows and Linux.

NuGet Packages for Windows and Linux

HiQPdf Next for .NET is delivered as NuGet packages for Windows and Linux. The packages include the .NET Standard 2.0 library, the same for both platforms, and the specific native runtime for each platform.
ASP.NET Core Demo Application with C# Code for All Features
The zip package that can be downloaded from the website contains the project for the ASP.NET Core demo application with C# sample code for all major library features.

Simple and Flexible Licensing with a Single License for All Libraries

The license for HiQPdf Next for .NET works with both the classic HiQPdf Library for .NET and the multi-platform client-server solution. There are no additional runtime or deployment costs charged for using our software component in your applications.

HiQPdf Next Core PDF API for .NET - Create a PDF Document C# Code Sample for ASP.NET Core

using System.IO;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

using HiQPdf.Next;

namespace HiQPdf_Next_AspNetDemo.Controllers
{
    public class Create_PDF_DocumentsController : Controller
    {
        private readonly IWebHostEnvironment m_hostingEnvironment;

        public Create_PDF_DocumentsController(IWebHostEnvironment hostingEnvironment)
        {
            m_hostingEnvironment = hostingEnvironment;
        }

        public IActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult CreatePdf(IFormCollection collection)
        {
            // Replace the demo serial number with the serial number received upon purchase
            // to run the converter in licensed mode
            Licensing.SerialNumber = "YCgJMTAE-BiwJAhIB-EhlWTlBA-UEBRQFBA-U1FOUVJO-WVlZWQ==";

            PdfDocumentCreateSettings pdfCreateSettings = new PdfDocumentCreateSettings()
            {
                PageSize = PdfPageSize.A4,
                PageOrientation = PdfPageOrientation.Portrait,
                Margins = new PdfMargins(36, 36, 36, 36)
            };

            // Create a new PDF document with the specified settings
            using PdfDocument pdfDocument = new PdfDocument(pdfCreateSettings);

            const int startXPos = 0;
            const int startYPos = 0;
            const int ySeparator = 15;

            int currentPage = pdfDocument.PageNumber;
            int crtXPos = startXPos;
            int crtYPos = startYPos;

            // Create a standard Helvetica font
            PdfFont fontHelvetica = PdfFontManager.CreateStandardFont(PdfStandardFont.Helvetica, 16f,
                PdfFontStyle.Bold | PdfFontStyle.Underline, PdfColor.Black);

            // Add a title using the Helvetica font
            PdfTextElement pdfTitle1 = new PdfTextElement("Transparent PNG Image with Custom Width", fontHelvetica)
            {
                X = crtXPos,
                Y = crtYPos
            };
            PdfTextRenderInfo textRenderInfo = pdfDocument.AddText(pdfTitle1);

            // Update the Y position after rendering the title
            crtYPos = textRenderInfo.LastPageRectangle.Bounds.Bottom + ySeparator;

            string imagesPath = GetDemoImagesPath();

            // Add a transparent PNG image with custom width
            PdfImageElement pdfPngImage = new PdfImageElement(Path.Combine(imagesPath, "transparent.png"))
            {
                X = crtXPos,
                Y = crtYPos,
                Width = 150
            };
            PdfImageRenderInfo imageRenderInfo = pdfDocument.AddImage(pdfPngImage);

            crtYPos = imageRenderInfo.BoundingBox.Bottom + ySeparator;

            // Create a standard Courier font
            PdfFont fontCourier = PdfFontManager.CreateStandardFont(PdfStandardFont.Courier, 16f,
                PdfFontStyle.Bold | PdfFontStyle.Italic, PdfColor.Green);

            // Add a title for the JPEG image
            PdfTextElement pdfTitle2 = new PdfTextElement("JPEG Image with Custom Height", fontCourier)
            {
                X = crtXPos,
                Y = crtYPos
            };
            textRenderInfo = pdfDocument.AddText(pdfTitle2);

            crtYPos = textRenderInfo.LastPageRectangle.Bounds.Bottom + ySeparator;

            // Ensure there is enough vertical space on the current page for the image.
            // Add a new page and reset Y position if needed
            EnsureSpaceOnPage(ref crtYPos, 200, pdfDocument);

            // Add a JPEG image with custom height
            PdfImageElement pdfJpgImage = new PdfImageElement(Path.Combine(imagesPath, "image.jpg"))
            {
                X = crtXPos,
                Y = crtYPos,
                Height = 150
            };
            imageRenderInfo = pdfDocument.AddImage(pdfJpgImage);

            crtYPos = imageRenderInfo.BoundingBox.Bottom + ySeparator;

            string fontsPath = GetDemoFontsPath();
            string textsPath = GetDemoTextsPath();

            string alphabetFilePath = Path.Combine(textsPath, "Alphabet.txt");
            string alfabetString = System.IO.File.ReadAllText(alphabetFilePath);

            // Create a blue bold Courier font
            PdfFont fontCourier2 = PdfFontManager.CreateStandardFont(PdfStandardFont.Courier, 16f,
                PdfFontStyle.Bold, PdfColor.Blue);

            // Add a title for the Unicode text
            PdfTextElement pdfTitle3 = new PdfTextElement("Multi Page Unicode Text with Custom Font", fontCourier2)
            {
                Y = crtYPos,
                Alignment = PdfTextAlignment.Center
            };
            textRenderInfo = pdfDocument.AddText(pdfTitle3);

            crtYPos = textRenderInfo.LastPageRectangle.Bounds.Bottom + ySeparator;

            string dejaVuRegularFontFile = Path.Combine(fontsPath, "DejaVuSerif.ttf");

            // Create an Unicode TrueType font from file
            PdfFont trueTypeFont = PdfFontManager.CreateFont(dejaVuRegularFontFile, 16f,
                PdfFontStyle.Normal, PdfColor.Black);

            // Add long Unicode text using the TrueType font, allowing continuation on next pages
            PdfTextElement pdfText1 = new PdfTextElement(alfabetString, trueTypeFont)
            {
                X = crtXPos,
                Y = crtYPos,
                Alignment = PdfTextAlignment.Left,
                ContinueOnNextPage = true
            };

            // Draw a rectangle around rendered text in each page
            pdfText1.OnPageRendered = (pageNumber, boundingBox) =>
            {
                PdfRectangleElement border = new PdfRectangleElement(boundingBox.X, boundingBox.Y,
                    boundingBox.Width, boundingBox.Height + 5)
                {
                    BorderColor = PdfColor.Blue,
                };
                pdfDocument.AddRectangle(border);
            };

            textRenderInfo = pdfDocument.AddText(pdfText1);

            // Set the next page to landscape A4
            pdfDocument.SetPageSize(PdfPageSize.A4, PdfPageOrientation.Landscape);

            // Add a new page
            pdfDocument.AddPage();

            // Reset positions
            crtXPos = 0;
            crtYPos = 0;

            // Add the same text again, with the same font, centered
            PdfTextElement pdfText2 = new PdfTextElement(alfabetString, trueTypeFont)
            {
                X = crtXPos,
                Y = crtYPos,
                Alignment = PdfTextAlignment.Center,
                ContinueOnNextPage = true
            };

            pdfText2.OnPageRendered = (pageNumber, boundingBox) =>
            {
                PdfRectangleElement border = new PdfRectangleElement(boundingBox.X, boundingBox.Y,
                    boundingBox.Width, boundingBox.Height + 5)
                {
                    BorderColor = PdfColor.Purple,
                };
                pdfDocument.AddRectangle(border);
            };

            textRenderInfo = pdfDocument.AddText(pdfText2);

            // Add a new page
            pdfDocument.AddPage();

            // Reset positions
            crtXPos = 0;
            crtYPos = 0;

            PdfFont rtlTileFont = PdfFontManager.CreateStandardFont(PdfStandardFont.Courier, 16f, 
                PdfFontStyle.Normal, PdfColor.Purple);

            PdfTextElement rtlTitle = new PdfTextElement("Add Right to Left Text", rtlTileFont)
            {
                X = crtXPos,
                Y = crtYPos
            };
            textRenderInfo = pdfDocument.AddText(rtlTitle);
            crtYPos = textRenderInfo.LastPageRectangle.Bounds.Bottom + ySeparator;

            // Create a Unicode font that supports Arabic (RTL)
            string notoSansArabicRegularFontFile = Path.Combine(fontsPath, "NotoSansArabic-Regular.ttf");

            PdfFont trueTypeFontArabic = PdfFontManager.CreateFont(notoSansArabicRegularFontFile, 16f,
                PdfFontStyle.Normal, PdfColor.Black);

            string rtlFilePath = Path.Combine(textsPath, "RightToLeft.txt");
            string rtlString = System.IO.File.ReadAllText(rtlFilePath);

            // Render the right to left text
            PdfTextElement pdfTextRtl = new PdfTextElement(rtlString, trueTypeFontArabic)
            {
                X = crtXPos,
                Y = crtYPos,
                Direction = PdfTextDirection.RightToLeft
            };
            textRenderInfo = pdfDocument.AddText(pdfTextRtl);

            // Save to memory buffer
            byte[] outPdfBuffer = pdfDocument.Save();

            // Send PDF to browser
            FileResult fileResult = new FileContentResult(outPdfBuffer, "application/pdf");
            fileResult.FileDownloadName = "PdfDocument.pdf";

            return fileResult;
        }

        private void EnsureSpaceOnPage(ref int crtYPos, int requestedHeight, PdfDocument pdfDocument)
        {
            if (crtYPos + requestedHeight > pdfDocument.ContentHeight)
            {
                pdfDocument.AddPage();
                crtYPos = 0;
            }
        }

        private string GetDemoFilesPath()
        {
            return m_hostingEnvironment.ContentRootPath + "/wwwroot" + "/DemoFiles/";
        }

        private string GetDemoImagesPath()
        {
            return Path.Combine(GetDemoFilesPath(), "Images");
        }

        private string GetDemoFontsPath()
        {
            return Path.Combine(GetDemoFilesPath(), "Fonts");
        }

        private string GetDemoTextsPath()
        {
            return Path.Combine(GetDemoFilesPath(), "Text");
        }
    }
}