How to convert PDF to Word in C# (.NET)
Table of contents
Converting PDF files to editable Word documents is a common requirement in document management systems and legal tech platforms. To convert PDF to Word in C#, use Nutrient .NET SDK’s GdPictureDocumentConverter class: Load the PDF with LoadFromFile(), and then call SaveAsDOCX() to create a Word document.
This guide shows how to implement PDF-to-Word conversion in C# and VB.NET using Nutrient .NET SDK.
Nutrient .NET SDK converts PDF to Word (DOCX) using the GdPictureDocumentConverter class. Load a PDF with LoadFromFile() or LoadFromStream(), and then call SaveAsDOCX() to create the Word document.
Prerequisites
Before you begin, ensure you have:
- Visual Studio 2022(opens in a new tab) (17.6+) or Visual Studio Code with C# extension
- .NET 8, .NET 9, or .NET 10(opens in a new tab)
- A Nutrient .NET SDK license key (free trial available)
- Basic familiarity with C# or VB.NET and NuGet package management(opens in a new tab)
Step 1 — Install Nutrient .NET SDK
The recommended way to install Nutrient .NET SDK is through NuGet. Use GdPicture.API for .NET 8.0+ or GdPicture for .NET Framework 4.6.2+. For a list of all available packages, see the Nutrient .NET SDK downloads page.
From the command line (.NET 8.0+):
dotnet add package GdPicture.APIFrom the Package Manager Console in Visual Studio:
Install-Package GdPicture.APIOr add the package reference directly to your .csproj file:
<PackageReference Include="GdPicture.API" Version="14.*" />Step 2 — Activate the trial license
Before using any SDK methods, create a LicenseManager object and register your license key. This should be done once at application startup:
using GdPicture14;
LicenseManager licenseManager = new LicenseManager();licenseManager.RegisterKEY("");Imports GdPicture14
Dim licenseManager As New LicenseManager()licenseManager.RegisterKEY("")Leaving the license key as an empty string activates the SDK in trial mode. After purchasing a commercial license, replace the empty string with your license key. For production applications, store your license key securely using environment variables or a secrets manager.
Step 3 — Convert PDF to DOCX
To save a PDF to a Word document (DOCX), use the SaveAsDOCX method of the GdPictureDocumentConverter class.
The SaveAsDOCX method accepts the following parameters:
FilePath— The file path where the converted file will be saved. If the specified file already exists, it will be overwritten. You must specify a full file path, including the file extension.Stream(overload) — A stream object where the current document is saved as a DOCX file. This stream object must be initialized before it can be sent into this method, and it should stay open for subsequent use.
using GdPictureDocumentConverter converter = new();GdPictureStatus status = converter.LoadFromFile("input.pdf");if (status != GdPictureStatus.OK){ throw new Exception(status.ToString());}
status = converter.SaveAsDOCX("output.docx");if (status != GdPictureStatus.OK){ throw new Exception(status.ToString());}
Console.WriteLine("The input document has been converted to a docx file");Using converter As New GdPictureDocumentConverter() Dim status As GdPictureStatus = converter.LoadFromFile("input.pdf") If status <> GdPictureStatus.OK Then Throw New Exception(status.ToString()) End If
status = converter.SaveAsDOCX("output.docx") If status <> GdPictureStatus.OK Then Throw New Exception(status.ToString()) End If
Console.WriteLine("The input document has been converted to a docx file")End UsingThe LoadFromFile method accepts all supported file formats. However, only PDF will return a high-quality DOCX.
If you use SaveAsDOCX after loading a file that isn’t a PDF, the method will create a DOCX containing the original document as an image. For the best results, ensure the input document is a PDF. If the source document isn’t a PDF, convert it to PDF first with SaveAsPDF, and then use SaveAsDOCX.
The SDK aims to preserve the layout and formatting of the source PDF, including text, tables, images, and page structure.
Configuring image quality
Control image quality in the output DOCX using the DocxImageQuality property. The default value is 75. Higher values produce better images but larger files.
using GdPictureDocumentConverter converter = new();GdPictureStatus status = converter.LoadFromFile("input.pdf");if (status != GdPictureStatus.OK){ throw new Exception(status.ToString());}
// Set image quality.converter.DocxImageQuality = 80;
status = converter.SaveAsDOCX("output.docx");if (status != GdPictureStatus.OK){ throw new Exception(status.ToString());}Using converter As New GdPictureDocumentConverter() Dim status As GdPictureStatus = converter.LoadFromFile("input.pdf") If status <> GdPictureStatus.OK Then Throw New Exception(status.ToString()) End If
' Set image quality converter.DocxImageQuality = 80
status = converter.SaveAsDOCX("output.docx") If status <> GdPictureStatus.OK Then Throw New Exception(status.ToString()) End IfEnd UsingConverting using streams
For web applications or scenarios where you’re working with streams rather than file paths, use the stream-based overloads. The LoadFromStream method requires a DocumentFormat parameter to specify the file type.
using System.IO;using GdPicture14;
public byte[] ConvertPdfToWordFromStream(Stream pdfStream){ using GdPictureDocumentConverter converter = new();
GdPictureStatus status = converter.LoadFromStream(pdfStream, GdPicture14.DocumentFormat.DocumentFormatPDF); if (status != GdPictureStatus.OK) { throw new Exception($"Failed to load PDF: {status}"); }
using MemoryStream outputStream = new(); status = converter.SaveAsDOCX(outputStream); if (status != GdPictureStatus.OK) { throw new Exception($"Failed to convert: {status}"); }
return outputStream.ToArray();}Imports System.IOImports GdPicture14
Public Function ConvertPdfToWordFromStream(pdfStream As Stream) As Byte() Using converter As New GdPictureDocumentConverter() Dim status As GdPictureStatus = converter.LoadFromStream(pdfStream, GdPicture14.DocumentFormat.DocumentFormatPDF) If status <> GdPictureStatus.OK Then Throw New Exception($"Failed to load PDF: {status}") End If
Using outputStream As New MemoryStream() status = converter.SaveAsDOCX(outputStream) If status <> GdPictureStatus.OK Then Throw New Exception($"Failed to convert: {status}") End If
Return outputStream.ToArray() End Using End UsingEnd FunctionThe output stream should be open for both reading and writing. Use a using statement to ensure the converter and stream are properly disposed after processing is complete.
ASP.NET Core Web API example
For web services that need to convert PDFs on demand, here’s a complete ASP.NET Core controller example:
using Microsoft.AspNetCore.Mvc;using GdPicture14;
[ApiController][Route("api/[controller]")]public class ConversionController : ControllerBase{ [HttpPost("pdf-to-word")] public IActionResult ConvertPdfToWord(IFormFile file) { if (file == null || file.Length == 0) { return BadRequest("No file uploaded"); }
if (!file.FileName.EndsWith(".pdf", StringComparison.OrdinalIgnoreCase)) { return BadRequest("Only PDF files are supported"); }
using var inputStream = file.OpenReadStream(); using var outputStream = new MemoryStream(); using var converter = new GdPictureDocumentConverter();
var status = converter.LoadFromStream(inputStream, GdPicture14.DocumentFormat.DocumentFormatPDF); if (status != GdPictureStatus.OK) { return StatusCode(500, $"Failed to load PDF: {status}"); }
status = converter.SaveAsDOCX(outputStream); if (status != GdPictureStatus.OK) { return StatusCode(500, $"Failed to convert: {status}"); }
outputStream.Position = 0; var outputFileName = Path.ChangeExtension(file.FileName, ".docx");
return File(outputStream.ToArray(), "application/vnd.openxmlformats-officedocument.wordprocessingml.document", outputFileName); }}Register the license key in your Program.cs. For trial mode, use an empty string; for production, load the key from configuration:
var builder = WebApplication.CreateBuilder(args);
// Register GdPicture license at startup.// Use "" for trial mode, or load from configuration for production.var licenseManager = new LicenseManager();licenseManager.RegisterKEY(builder.Configuration["GdPicture:LicenseKey"] ?? "");
// ... rest of your configuration.Handling edge cases
Not every PDF converts cleanly out of the box. Below are two common scenarios — scanned documents that need OCR and bulk processing of multiple files — along with ready-to-use code for each.
Scanned PDFs (OCR integration)
For scanned PDFs that contain images of text rather than actual text, apply OCR before conversion:
using GdPicture14;
public void ConvertScannedPdfToWord(string inputPath, string outputPath, string ocrResourcePath){ using GdPicturePDF pdf = new(); pdf.LoadFromFile(inputPath);
for (int i = 1; i <= pdf.GetPageCount(); i++) { pdf.SelectPage(i); // Parameters: language, OCR resource folder, character allowlist, DPI pdf.OcrPage("eng", ocrResourcePath, "", 300); }
string tempOcrPath = Path.GetTempFileName() + ".pdf"; pdf.SaveToFile(tempOcrPath); pdf.CloseDocument();
using GdPictureDocumentConverter converter = new(); converter.LoadFromFile(tempOcrPath); converter.SaveAsDOCX(outputPath);
File.Delete(tempOcrPath);}
// Usage:// ConvertScannedPdfToWord("input.pdf", "output.docx", @"C:\GdPicture.NET 14\Redist\OCR");Imports GdPicture14
Public Sub ConvertScannedPdfToWord(inputPath As String, outputPath As String, ocrResourcePath As String) Using pdf As New GdPicturePDF() pdf.LoadFromFile(inputPath)
For i As Integer = 1 To pdf.GetPageCount() pdf.SelectPage(i) ' Parameters: language, OCR resource folder, character allowlist, DPI pdf.OcrPage("eng", ocrResourcePath, "", 300) Next
Dim tempOcrPath As String = Path.GetTempFileName() + ".pdf" pdf.SaveToFile(tempOcrPath) pdf.CloseDocument()
Using converter As New GdPictureDocumentConverter() converter.LoadFromFile(tempOcrPath) converter.SaveAsDOCX(outputPath) End Using
File.Delete(tempOcrPath) End UsingEnd Sub
' Usage:' ConvertScannedPdfToWord("input.pdf", "output.docx", "C:\GdPicture.NET 14\Redist\OCR")The OCR resource folder contains language data files. The default location is GdPicture.NET 14\Redist\OCR. See the language support guide for information on adding language resources. For the full list of GdPicturePDF methods, including OcrPage, refer to the API reference.
Batch conversion
For processing multiple files efficiently:
public void BatchConvertPdfsToWord(string[] inputPaths, string outputDirectory){ foreach (string inputPath in inputPaths) { using GdPictureDocumentConverter converter = new(); GdPictureStatus status = converter.LoadFromFile(inputPath);
if (status == GdPictureStatus.OK) { string outputPath = Path.Combine( outputDirectory, Path.GetFileNameWithoutExtension(inputPath) + ".docx" );
converter.SaveAsDOCX(outputPath); Console.WriteLine($"Converted: {inputPath}"); } }}Public Sub BatchConvertPdfsToWord(inputPaths As String(), outputDirectory As String) For Each inputPath As String In inputPaths Using converter As New GdPictureDocumentConverter() Dim status As GdPictureStatus = converter.LoadFromFile(inputPath)
If status = GdPictureStatus.OK Then Dim outputPath As String = Path.Combine( outputDirectory, Path.GetFileNameWithoutExtension(inputPath) + ".docx" )
converter.SaveAsDOCX(outputPath) Console.WriteLine($"Converted: {inputPath}") End If End Using NextEnd SubReducing output file size
If output files are larger than expected, use the DocxImageQuality property to reduce image quality:
// Reduce image quality for smaller files.converter.DocxImageQuality = 60;FAQ
Use Nutrient .NET SDK’s GdPictureDocumentConverter class. Load the PDF with LoadFromFile("input.pdf"), and then call SaveAsDOCX("output.docx") to create a Word document.
If the source document isn’t a PDF, the SaveAsDOCX method will create a DOCX where each page contains a bitmap image of the original content. For best results, start with a PDF, or convert your source to PDF first using SaveAsPDF.
Yes. Create a separate GdPictureDocumentConverter instance for each file:
foreach (var pdfPath in pdfFiles){ using var converter = new GdPictureDocumentConverter(); converter.LoadFromFile(pdfPath); converter.SaveAsDOCX(pdfPath.Replace(".pdf", ".docx"));}Nutrient .NET SDK offers a free trial for evaluation. Commercial licensing is based on your use case and deployment requirements. Contact the Sales team or start a free trial to test the conversion.
Conclusion
This guide covered how to convert PDF to Word in C# and VB.NET using Nutrient .NET SDK. You learned how to:
- Install the SDK via NuGet and activate the trial license.
- Use
GdPictureDocumentConverterto load PDFs and save them as DOCX files. - Work with streams for web applications and ASP.NET Core integration.
- Handle scanned PDFs by applying OCR before conversion.
- Process multiple files in batch operations.
Nutrient .NET SDK provides a straightforward API for PDF-to-Word conversion that preserves document layout, text, tables, and images. The SDK supports both .NET 8+ and .NET Framework 4.6.2+, making it suitable for modern and legacy applications.
Start your free trial | Contact Sales