PDF Viewer Settings

HiQPdf HTML to PDF Converter for .NET offers the possibility to control how the generated PDF document is displayed in a PDF viewer like Adobe Reader. There are options to hide the PDF viewer menu and toolbar, to initially display the Bookmarks, Thumbnails or Attachments panel, to display the PDF pages in one or two columns.

The PDF viewer settings are encapsulated in HiQPdf.ChromiumPdfViewer class. An object of this type is exposed by the PdfDocumentControlViewer property and an object of PdfDocumentControl class is exposed by the Document property of the HiQPdf.ChromiumHtmlToPdf class.

PDF Viewer Settings Demo Source Code

C#
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

using HiQPdf.Chromium;

namespace HiQPdf_Chrome_AspNetDemo.Controllers
{
    public class PdfViewerSettingsController : Controller
    {
        IFormCollection m_formCollection;

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

        [HttpPost]
        public ActionResult ConvertToPdf(IFormCollection collection)
        {
            m_formCollection = 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==";

            // create the HTML to PDF converter
            HtmlToPdf htmlToPdfConverter = new HtmlToPdf();

            // Set the PDF Viewer Preferences

            // Set page layout to continuous one column, single page, two column left, two column right
            htmlToPdfConverter.Document.Viewer.PageLayout = GetSelectedPageLayout();
            // Set page mode to default, display bookmarks, display thumbnails, display attachments
            htmlToPdfConverter.Document.Viewer.PageMode = GetSelectedPageMode();

            // Hide the viewer menu
            htmlToPdfConverter.Document.Viewer.HideMenuBar = collection["checkBoxHideMenuBar"].Count > 0;
            // Hide the viewer toolbar
            htmlToPdfConverter.Document.Viewer.HideToolbar = collection["checkBoxHideToolbar"].Count > 0;
            // Hide scroll bars and navigation controls
            htmlToPdfConverter.Document.Viewer.HideWindowUI = collection["checkBoxHideWindowUI"].Count > 0;

            // Display the document title in viewer title bar
            htmlToPdfConverter.Document.Viewer.DisplayDocTitle = collection["checkBoxDisplayDocTitle"].Count > 0;

            // convert URL to a PDF memory buffer
            string url = collection["textBoxUrl"];

            byte[] pdfBuffer = htmlToPdfConverter.ConvertUrlToMemory(url);

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

            return fileResult;
        }

        private PdfPageLayout GetSelectedPageLayout()
        {
            switch (m_formCollection["dropDownListPageLayout"])
            {
                case "Single Page":
                    return PdfPageLayout.SinglePage;
                case "One Column":
                    return PdfPageLayout.OneColumn;
                case "Two Column Left":
                    return PdfPageLayout.TwoColumnLeft;
                case "Two Column Right":
                    return PdfPageLayout.TwoColumnRight;
                default:
                    return PdfPageLayout.OneColumn;
            }
        }

        private PdfPageMode GetSelectedPageMode()
        {
            switch (m_formCollection["dropDownListPageMode"])
            {
                case "Default":
                    return PdfPageMode.None;
                case "Outlines":
                    return PdfPageMode.Outlines;
                case "Thumbnails":
                    return PdfPageMode.Thumbnails;
                case "Full Screen":
                    return PdfPageMode.FullScreen;
                case "Optional Content":
                    return PdfPageMode.OptionalContent;
                case "Attachments":
                    return PdfPageMode.Attachments;
                default:
                    return PdfPageMode.None;
            }
        }
    }
}

See Also