The HiQPdf software allows you add external PDF documents loaded from a file or from data to a PdfDocument object using the PdfDocumentAddDocument(String) and PdfDocumentAddDocument(Byte) methods.
Merge PDF Demo
In this demo you can learn how to merge multiple PDF document into a single PDF document. Initially is created an empty document which will become the final document. Then the two PDF files are loaded into two PdfDocuments objects which are added to the empty document.
Demo Source Code
C#
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 MergePdfController : Controller { IWebHostEnvironment m_hostingEnvironment; public MergePdfController(IWebHostEnvironment hostingEnvironment) { m_hostingEnvironment = hostingEnvironment; } // GET: MergePdf public ActionResult Index() { SetCrtPageUri(); return View(); } [HttpPost] public ActionResult CreatePdf(IFormCollection collection) { string serverIP = collection["textBoxServerIP"]; uint serverPort = uint.Parse(collection["textBoxServerPort"]); string serverPassword = collection["textBoxServerPassword"]; // create an empty document which will become the final document after merge PdfDocument resultDocument = new PdfDocument(serverIP, serverPort); // use server password if necessary if (serverPassword.Length > 0) resultDocument.ServerPassword = serverPassword; // set a demo serial number resultDocument.SerialNumber = "YCgJMTAE-BiwJAhIB-EhlWTlBA-UEBRQFBA-U1FOUVJO-WVlZWQ=="; // load the first document to be merged from a file string pdfFile1 = m_hostingEnvironment.WebRootPath + "/DemoFiles/Pdf/WikiHtml.pdf"; // The second document to be merged will be loaded from a memory buffer string pdfFile2 = m_hostingEnvironment.WebRootPath + "/DemoFiles/Pdf/WikiPdf.pdf"; byte[] pdfData2 = System.IO.File.ReadAllBytes(pdfFile2); // add the two documents to the result document resultDocument.AddDocument(pdfFile1); resultDocument.AddDocument(pdfData2); try { // write the PDF document to a memory buffer byte[] pdfBuffer = resultDocument.WriteToMemory(); FileResult fileResult = new FileContentResult(pdfBuffer, "application/pdf"); fileResult.FileDownloadName = "MergePdf.pdf"; return fileResult; } finally { // close the result document resultDocument.Close(); } } 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