Convert Password-Protected PDF file to DOCX in C# and .NET
Working with PDF files, especially password-protected ones, is a common task in document automation. In this guide, we'll explain how to convert a password-protected PDF to DOCX using PDF Focus .NET from SautinSoft SDK, implementing all the necessary steps in C# and .NET.
Protected PDF files are widely used to maintain document confidentiality. You may need to automatically extract their content for processing or editing. Using a specialized library helps make this process reliable and fast.
Task update:
- Document workflow automation: converting PDFs to editable formats. The code allows for integration into automated scripts, bots, or server-side applications.
- Processing protected documents: sometimes, for example, a company receives passwords and needs to automate the work.
- Compatibility: DOCX files allow for easy editing and integration with other systems.
- Accuracy: Preserving styles and formatting after conversion ensures professional results.
Important points when using the sample code:
- The code requires the correct password (`PdfPassword`).
- A page check is performed before conversion.
- A license key is required to use the library, or you can try the free demo version.
Sectors where this code is needed:
- Legal practice: extracting data from password-protected documents.
- Accounting and finance: automating the processing of financial reports protected by PDF.
- Contract processing: converting protected contracts into an editable format.
- Educational institutions: preparing password-protected educational materials.
Complete code
using System;
using System.IO;
namespace Sample
{
class Sample
{
static void Main(string[] args)
{
// Before starting, we recommend to get a free key:
// https://sautinsoft.com/start-for-free/
// Apply the key here:
// SautinSoft.PdfFocus.SetLicense("...");
string pdfFile = Path.GetFullPath(@"..\..\..\Example.pdf");
string wordFile = "Result.docx";
//Convert PDF file to Text file
SautinSoft.PdfFocus f = new SautinSoft.PdfFocus();
//Set a password for password-protected PDF documents. You need to set this option before "f.OpenPdf"
f.Password = "123456789";
f.OpenPdf(pdfFile);
if (f.PageCount > 0)
{
int result = f.ToWord(wordFile);
//Show Text document
if (result == 0)
{
System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(wordFile) { UseShellExecute = true });
}
}
}
}
}
Imports System
Imports System.IO
Namespace Sample
Friend Class Sample
Shared Sub Main(ByVal args() As String)
' Before starting, we recommend to get a free key:
' https://sautinsoft.com/start-for-free/
' Apply the key here
' SautinSoft.PdfFocus.SetLicense("...");
Dim pdfFile As String = Path.GetFullPath("..\..\..\Example.pdf")
Dim wordFile As String = "Result.docx"
'Convert PDF file to Text file
Dim f As New SautinSoft.PdfFocus()
'Set a password for password-protected PDF documents. You need to set this option before "f.OpenPdf"
f.Password = "123456789"
f.OpenPdf(pdfFile)
If f.PageCount > 0 Then
Dim result As Integer = f.ToWord(wordFile)
'Show Text document
If result = 0 Then
System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(wordFile) With {.UseShellExecute = True})
End If
End If
End Sub
End Class
End NamespaceIf you need a new code example or have a question: email us at support@sautinsoft.com or ask at Online Chat (right-bottom corner of this page) or use the Form below: