ExpertPdf Anywhere ASP.NET Core samples · .NET 10 and .NET 11

ExpertPdf Anywhere - Word to Pdf Converter - C# / ASP.NET Sample

This sample shows how to convert a Word document to PDF with the Word to PDF converter of ExpertPdf Anywhere. Headings, tables, pictures, charts and links are kept; the headings become the bookmarks of the PDF. The pages can keep the sizes of the Word document, or the document can be laid out again on the page size chosen below.

The sample converts this Word document: document.docx

Page setup:


Page size (when laid out again):


Page orientation:
Bookmarks:


PDF standard:


Page numbers in the footer

C# code of this sample

Pages/ConvertWordToPdf.cshtml.cs
using System;
using System.IO;
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc.Rendering;
using ExpertPdf.Anywhere.WordToPdf;

namespace ExpertPdf.Samples.Pages
{
    public class ConvertWordToPdfModel : PageModel
    {
        public void OnGet()
        {
        }

        [BindProperty]
        public string DdlPageSetup { get; set; } = "Document";

        [BindProperty]
        public string DdlPageSize { get; set; } = "A4";

        [BindProperty]
        public string DdlPageOrientation { get; set; } = "Portrait";

        [BindProperty]
        public string DdlBookmarks { get; set; } = "Headings";

        [BindProperty]
        public string DdlStandard { get; set; } = "Full";

        [BindProperty]
        public bool ChkPageNumbers { get; set; } = true;

        public List<SelectListItem> PageSetups { get; } = new List<SelectListItem>
        {
            new SelectListItem { Value = "Document", Text = "Keep the page sizes of the document" },
            new SelectListItem { Value = "Options", Text = "Lay the document out on the page size below" },
        };

        public List<SelectListItem> PageSizes { get; } = new List<SelectListItem>
        {
            new SelectListItem { Value = "A4", Text = "A4" },
            new SelectListItem { Value = "A5", Text = "A5" },
            new SelectListItem { Value = "Letter", Text = "Letter" },
            new SelectListItem { Value = "Legal", Text = "Legal" },
        };

        public List<SelectListItem> PageOrientations { get; } = new List<SelectListItem>
        {
            new SelectListItem { Value = "Portrait", Text = "Portrait" },
            new SelectListItem { Value = "Landscape", Text = "Landscape" },
        };

        public List<SelectListItem> BookmarkSources { get; } = new List<SelectListItem>
        {
            new SelectListItem { Value = "Headings", Text = "From the headings" },
            new SelectListItem { Value = "Bookmarks", Text = "From the Word bookmarks" },
            new SelectListItem { Value = "None", Text = "No bookmarks" },
        };

        public List<SelectListItem> Standards { get; } = new List<SelectListItem>
        {
            new SelectListItem { Value = "Full", Text = "Full PDF" },
            new SelectListItem { Value = "Pdf_A_2b", Text = "PDF/A-2b" },
            new SelectListItem { Value = "Pdf_A_3a", Text = "PDF/A-3a (tagged)" },
        };

        public IActionResult OnPost()
        {
            string wordFile = Path.Combine(Startup.ServerPath, "wwwroot", "files", "document.docx");

            // instantiate the word to pdf converter
            PdfConverter converter = new PdfConverter();
            // converter.LicenseKey = "your license key";

            // keep the page sizes of the document, or lay it out again on the page size of the options
            converter.PageSetup = (WordPageSetup)Enum.Parse(typeof(WordPageSetup), DdlPageSetup);
            converter.PdfDocumentOptions.PdfPageSize = (PdfPageSize)Enum.Parse(typeof(PdfPageSize), DdlPageSize);
            converter.PdfDocumentOptions.PdfPageOrientation =
                (PDFPageOrientation)Enum.Parse(typeof(PDFPageOrientation), DdlPageOrientation);

            // what the bookmarks of the pdf are built from
            converter.Bookmarks = (WordBookmarks)Enum.Parse(typeof(WordBookmarks), DdlBookmarks);

            // the PDF standard; PDF/A-3a is also tagged (accessible)
            converter.PdfStandardSubset = (PdfStandardSubset)Enum.Parse(typeof(PdfStandardSubset), DdlStandard);

            // page numbers in the footer
            if (ChkPageNumbers)
            {
                converter.PdfDocumentOptions.ShowFooter = true;
                converter.PdfFooterOptions.ShowPageNumber = true;
            }

            // convert the word document to pdf
            byte[] pdf = converter.GetPdfBytesFromWordFile(wordFile);

            // return resulted pdf document
            FileResult fileResult = new FileContentResult(pdf, "application/pdf");
            fileResult.FileDownloadName = "Sample.pdf";
            return fileResult;
        }
    }
}