SelectPdf for .NET 10 - Convert Pdf to Word - C# / .NET 10 Sample

This sample shows how to use the SelectPdf pdf to Word converter to convert a pdf document to an editable Word document. Headings, paragraphs, lists, tables, pictures and links become Word content, read across the page's columns in reading order. The document can also be written as rtf, OpenDocument text, html or markdown. The conversion runs in-process; no engine is launched.

The sample converts the following pdf, which Microsoft Word saved from the Word to Pdf sample's document, or a pdf you upload:
Test pdf document

Pdf document (optional):

Layout:


Output Format:


Pages (empty for all, e.g. 1,3-5):


Images and vector art
Page headers and footers
Links
Bookmarks
Embed the fonts (docx)

Sample code · C#
using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc.Rendering;
using SelectPdf.Universal;

namespace SelectPdf.Samples.Pages
{
    public class PdfToWordModel : PageModel
    {
        public void OnGet()
        {
            DdlLayout = "Flowing";
            DdlFormat = "Docx";
            TxtPageRanges = "";
            ChkImages = true;
            ChkHeadersAndFooters = true;
            ChkLinks = true;
            ChkBookmarks = true;
            ChkEmbedFonts = true;
        }

        // An uploaded pdf, or nothing - the shipped files/document.pdf is converted then.
        [BindProperty]
        public IFormFile FilePdf { get; set; }

        [BindProperty]
        public string DdlLayout { get; set; }

        public List<SelectListItem> Layouts { get; } = new List<SelectListItem>
        {
            new SelectListItem { Value = "Flowing", Text = "Flowing - a document to edit" },
            new SelectListItem { Value = "Exact", Text = "Exact - looks like the pdf page for page" },
        };

        [BindProperty]
        public string DdlFormat { get; set; }

        public List<SelectListItem> Formats { get; } = new List<SelectListItem>
        {
            new SelectListItem { Value = "Docx", Text = "Word (.docx)" },
            new SelectListItem { Value = "Rtf", Text = "Rich Text Format (.rtf)" },
            new SelectListItem { Value = "Odt", Text = "OpenDocument text (.odt)" },
            new SelectListItem { Value = "Html", Text = "Web page (.html)" },
            new SelectListItem { Value = "Markdown", Text = "Markdown (.md)" },
        };

        [BindProperty]
        public string TxtPageRanges { get; set; }

        [BindProperty]
        public bool ChkImages { get; set; }

        [BindProperty]
        public bool ChkHeadersAndFooters { get; set; }

        [BindProperty]
        public bool ChkLinks { get; set; }

        [BindProperty]
        public bool ChkBookmarks { get; set; }

        [BindProperty]
        public bool ChkEmbedFonts { get; set; }

        public IActionResult OnPost()
        {
            // read parameters from the webpage
            PdfToWordLayout layout = SampleForm.ParseEnum(DdlLayout, PdfToWordLayout.Flowing);
            PdfToWordFormat format = SampleForm.ParseEnum(DdlFormat, PdfToWordFormat.Docx);

            // instantiate a pdf to word converter object
            PdfToWord converter = new PdfToWord();

            // set converter options: how the pages are laid out, the format written, the pages
            // converted (empty for all, or ranges such as "1,3-5") and what is carried into the document
            converter.Options.Layout = layout;
            converter.Options.Format = format;
            converter.Options.PageRanges = TxtPageRanges ?? string.Empty;
            converter.Options.Images = ChkImages;
            converter.Options.HeadersAndFooters = ChkHeadersAndFooters;
            converter.Options.Links = ChkLinks;
            converter.Options.Bookmarks = ChkBookmarks;
            converter.Options.EmbedFonts = ChkEmbedFonts;

            // convert the uploaded pdf, or the sample one shipped with the application
            byte[] document;
            if (FilePdf != null && FilePdf.Length > 0)
            {
                using (Stream uploaded = FilePdf.OpenReadStream())
                {
                    document = converter.ConvertStream(uploaded);
                }
            }
            else
            {
                string pdfFile = Path.Combine(Startup.ServerPath, "wwwroot", "files", "document.pdf");
                document = converter.ConvertFile(pdfFile);
            }

            // return the converted document
            string extension;
            string contentType;
            switch (format)
            {
                case PdfToWordFormat.Rtf:
                    extension = "rtf"; contentType = "application/rtf"; break;
                case PdfToWordFormat.Odt:
                    extension = "odt"; contentType = "application/vnd.oasis.opendocument.text"; break;
                case PdfToWordFormat.Html:
                    extension = "html"; contentType = "text/html"; break;
                case PdfToWordFormat.Markdown:
                    extension = "md"; contentType = "text/markdown"; break;
                default:
                    extension = "docx";
                    contentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
                    break;
            }
            FileResult fileResult = new FileContentResult(document, contentType);
            fileResult.FileDownloadName = "Document." + extension;
            return fileResult;
        }
    }
}