Convert PDF to an editable Word document in C#

PdfToWord converts a PDF to an editable Word document: the text of every page is grouped into paragraphs in reading order, with its headings, lists, tables, pictures, hyperlinks and bookmarks. Call ConvertFile and write the bytes it returns to a .docx file. PdfToExcel and PdfToPowerPoint work the same way.

Compiled and run: Linux x64 · Windows x64

Video walkthrough · 2:42 · Watch on YouTube · Short · 0:53
§STEP BY STEP

Build it step by step.

  1. 1Install the NuGet packages

    SelectPdf.Universal is the library. The native package carries the Chromium engine for one platform: add the one for the platform you run on.

    terminal
    dotnet add package SelectPdf.Universal
    dotnet add package SelectPdf.Universal.Native.win-x64
    terminal
    dotnet add package SelectPdf.Universal
    dotnet add package SelectPdf.Universal.Native.linux-x64
    terminal
    dotnet add package SelectPdf.Universal
    dotnet add package SelectPdf.Universal.Native.linux-arm64
    terminal
    dotnet add package SelectPdf.Universal
    dotnet add package SelectPdf.Universal.Native.osx-arm64
  2. 2Create a console app

    Any .NET 8 or .NET 10 project works. To follow along, create a console app, run the install commands above inside its folder, then replace Program.cs with the code below.

    terminal
    dotnet new console -n PdfSample
    cd PdfSample
  3. 3Create the converter

    Program.cs · lines 3–3
    PdfToWord converter = new PdfToWord();

    PdfToWord converts a PDF to an editable Word document. Its Options set the layout, the output format, the pages converted and what is carried over: pictures, headers and footers, links, bookmarks and fonts.

  4. 4Choose the layout and the format

    Program.cs · lines 5–7
    // Flowing (the default): paragraphs that reflow as you edit, as in any Word document
    converter.Options.Layout = PdfToWordLayout.Flowing;
    converter.Options.Format = PdfToWordFormat.Docx;

    PdfToWordLayout.Flowing, the default, gives a document to edit: its paragraphs flow as in any Word document. PdfToWordLayout.Exact reproduces every page as it is, each block of text in a frame at its place. PdfToWordFormat.Docx is the default format; Rtf, Odt, Html and Markdown are the others.

  5. 5Convert and save

    Program.cs · lines 9–11
    // convert the PDF and write the Word document
    byte[] docx = converter.ConvertFile("report.pdf");
    File.WriteAllBytes("report.docx", docx);

    ConvertFile returns the converted document as an array of bytes, and File.WriteAllBytes writes it as report.docx. The report's headings, table, bulleted lists and chart come through, with its link to a web page and its link to the Outlook section. ConvertStream and ConvertBytes take the PDF from a stream or from memory.

  6. 6See what became of the fonts

    Program.cs · lines 13–15
    // what became of each font: embedded, substituted or named only
    foreach (PdfToWordFont font in converter.ConversionResult.Fonts)
        Console.WriteLine(font);

    After a conversion, ConversionResult.Fonts lists every face of the document's text as a PdfToWordFont, and whether it was embedded, written in a family with the same metrics, or named only. Here Arial and Arial Bold are named only, because Arial is a common system font.

  7. 7Run it

    terminal
    dotnet run

    The program writes report.docx to the working folder. Without a license key, every page carries a trial watermark; set GlobalProperties.LicenseKey before the first conversion to remove it.

§COMPLETE PROGRAM

The complete Program.cs.

This is the exact file our samples pipeline compiled and ran on Linux x64 and Windows x64 to produce the output on this page.

Program.cs
using SelectPdf.Universal;

PdfToWord converter = new PdfToWord();

// Flowing (the default): paragraphs that reflow as you edit, as in any Word document
converter.Options.Layout = PdfToWordLayout.Flowing;
converter.Options.Format = PdfToWordFormat.Docx;

// convert the PDF and write the Word document
byte[] docx = converter.ConvertFile("report.pdf");
File.WriteAllBytes("report.docx", docx);

// what became of each font: embedded, substituted or named only
foreach (PdfToWordFont font in converter.ConversionResult.Fonts)
    Console.WriteLine(font);

Open the project on GitHub → this program and the variations below, ready to dotnet run

§OUTPUT

The Word document it produces.

First page of report.docx, the Word document this sample produces
report.docx, page 1

Download report.docx ↓ the file this sample produced, unedited

§VARIATIONS

Variations you will need next.

Each variation is a separate file in the same project, compiled and run with the program above.

Exact layout: every page as it is

PdfToWordLayout.Exact reproduces every page as it is, each block of text at its place, for a Word document that must look like the PDF. Choose the default Flowing layout when the document is to be edited: its paragraphs reflow as the text changes.

ExactLayout.cs
using SelectPdf.Universal;

static class ExactLayout
{
    public static void Run()
    {
        PdfToWord converter = new PdfToWord();

        // Exact: every page reproduced as it is, each block of text at its place
        converter.Options.Layout = PdfToWordLayout.Exact;

        byte[] docx = converter.ConvertFile("report.pdf");
        File.WriteAllBytes("report-exact.docx", docx);
    }
}

PDF to Markdown

PdfToWordFormat.Markdown writes the document as Markdown: headings as # lines, bulleted lists as - items, links as Markdown links and tables as pipe tables, with the pictures inside the file. Rtf, Odt and Html are the other formats besides Docx.

MarkdownOutput.cs
using SelectPdf.Universal;

static class MarkdownOutput
{
    public static void Run()
    {
        PdfToWord converter = new PdfToWord();

        // Markdown: headings, lists and links as Markdown, tables as pipe tables
        converter.Options.Format = PdfToWordFormat.Markdown;

        byte[] md = converter.ConvertFile("report.pdf");
        File.WriteAllBytes("report.md", md);
    }
}

PDF to Excel: the tables as cells

PdfToExcel finds the tables of the PDF and writes them as cells: numbers, percentages, currency amounts and dates as typed values, text as text, header rows bold and frozen, merged cells merged. PdfToExcelSheets.PerTable, the default, puts each table on a worksheet of its own and the text outside the tables on a worksheet after them; PerPage gives a worksheet per page. Culture names the culture the values are read in; left empty, it takes the PDF's language, else the formats the values themselves show.

PdfToExcelTables.cs
using SelectPdf.Universal;

static class PdfToExcelTables
{
    public static void Run()
    {
        PdfToExcel converter = new PdfToExcel();

        // a worksheet per table (the default), numbers read as US figures
        converter.Options.Sheets = PdfToExcelSheets.PerTable;
        converter.Options.Culture = "en-US";

        byte[] xlsx = converter.ConvertFile("sales-by-region.pdf");
        File.WriteAllBytes("sales-by-region.xlsx", xlsx);
    }
}

PDF to PowerPoint: a slide per page

PdfToPowerPoint makes a slide of every page: each block of text a text box at its place, the page color the slide's background, boxes and rules shapes, images and vector art pictures. The slides take the first page's size. With Tables = true, the default, the PDF's tables become PowerPoint tables; false keeps their text in text boxes and their lines as shapes. ConvertBytes converts a PDF that is already in memory.

PdfToPowerPointSlides.cs
using SelectPdf.Universal;

static class PdfToPowerPointSlides
{
    public static void Run()
    {
        PdfToPowerPoint converter = new PdfToPowerPoint();

        // the PDF's tables become PowerPoint tables (the default)
        converter.Options.Tables = true;

        // a PDF already in memory: a slide per page
        byte[] pdf = File.ReadAllBytes("quarterly-review.pdf");
        byte[] pptx = converter.ConvertBytes(pdf);
        File.WriteAllBytes("quarterly-review.pptx", pptx);
    }
}

Convert some of the pages, from a stream

PageRanges limits the conversion to the pages it names: "1", "2-4" or "1-3,5,7-"; empty, the default, converts every page. ConvertStream reads the PDF from a readable stream, from its current position. The options of PdfToExcel and PdfToPowerPoint have the same PageRanges property, and all three converters have ConvertFile, ConvertStream and ConvertBytes.

PageRangeFromStream.cs
using SelectPdf.Universal;

static class PageRangeFromStream
{
    public static void Run()
    {
        PdfToWord converter = new PdfToWord();

        // only pages 2 and 3; "1", "2-4" and "1-3,5,7-" work too
        converter.Options.PageRanges = "2-3";

        // ConvertStream reads the PDF from the stream's current position
        using FileStream pdf = File.OpenRead("report.pdf");
        byte[] docx = converter.ConvertStream(pdf);
        File.WriteAllBytes("report-pages-2-3.docx", docx);
    }
}
§TROUBLESHOOTING

Common problems and fixes.

PdfToWordException: the file cannot be found or read

ConvertFile throws PdfToWordException when the file does not exist, the PDF cannot be read or the conversion fails. A relative path is resolved against the current directory, so pass a full path, or read the file yourself and call ConvertStream or ConvertBytes.

The PDF is password protected

Set converter.Options.Password to the password of the encrypted PDF before converting it. A wrong password throws PdfToWordException; PdfToExcelOptions and PdfToPowerPointOptions have the same Password property.

A scanned page comes out as a picture

A scanned page without a text layer comes out as its picture, so there is no text to edit on it.

The converted file carries an evaluation mark

Without a license key, the converted file carries an evaluation mark. Set your license key before converting to get a clean document.

§GOOD TO KNOW

Good to know.

  • PdfToWord groups the text of every page into paragraphs in reading order, with their headings, lists, tables, pictures, hyperlinks and bookmarks. Source
  • PdfToWord writes Word (.docx), RTF, OpenDocument text, HTML or Markdown; the Flowing layout gives a document to edit, the Exact layout reproduces every page as it is. Source
  • Text repeated at the top and bottom of the pages becomes the Word document's headers and footers, with page numbers as page fields. Source
  • PdfToExcel writes the document's tables as cells: numbers, percentages, currency amounts and dates as typed values, header rows bold and frozen, merged cells merged. Source
  • PdfToPowerPoint makes a slide of every page, and by default the PDF's tables become PowerPoint tables. Source
  • PDF to Word, Excel and PowerPoint convert from a file, a stream or an array of bytes, and are available in the full library only. Source
?FAQ

Questions, answered.

Short answers about this guide. The SelectPdf.Universal documentation covers every class and option.

Create a PdfToWord converter, call ConvertFile with the path of the PDF and write the bytes it returns to a .docx file with File.WriteAllBytes. The default Flowing layout gives a document to edit, its paragraphs flowing as in any Word document.
Yes. PdfToExcel writes the tables of the PDF to an Excel workbook (.xlsx), a worksheet per table or per page, and PdfToPowerPoint writes a PowerPoint presentation (.pptx) with a slide per page. Both have the same ConvertFile, ConvertStream and ConvertBytes methods as PdfToWord.
By default, EmbedFonts embeds the document's fonts, so the Word document looks like the PDF on a computer that lacks them. A font whose license forbids embedding, or a common system font, is named only; a family that is neither embedded nor installed is written in a family with the same metrics when there is one, such as Arial for Helvetica. ConversionResult.Fonts says what became of each font.
No. PDF to Word, Excel and PowerPoint are available only in the full commercial SelectPdf library. The free Community Edition converts HTML to PDF only.