Load password-protected document

GroupDocs.Conversion supports the conversion of documents that are protected with a password.

To load and convert a password-protected document, follow these steps:

  1. Define a Func<LoadOptions> delegate, which should return an instance of document-specific load options with the password specified.
  2. Create an instance of the Converter class and pass the source document path and the load options delegate as the constructor parameters.
  3. Instantiate the appropriate ConvertOptions class e.g. (PdfConvertOptions, WordProcessingConvertOptions, SpreadsheetConvertOptions, etc.)
  4. Call the Convert method of the Converter class instance and pass the filename for the converted document and the instance of the ConvertOptions from the previous step.

The following code snippet shows how to convert password protected document:

With v24.10 and later:

Func<LoadContext, LoadOptions> getLoadOptions = loadContext => new WordProcessingLoadOptions
{
    Password = "12345"
};
using (Converter converter = new Converter("sample_with_password.docx", getLoadOptions))
{
    PdfConvertOptions options = new PdfConvertOptions();
    converter.Convert("converted.pdf", options);
}

Before v24.10:

Func<LoadOptions> getLoadOptions = () => new WordProcessingLoadOptions
{
    Password = "12345"
};
using (Converter converter = new Converter("sample_with_password.docx", getLoadOptions))
{
    PdfConvertOptions options = new PdfConvertOptions();
    converter.Convert("converted.pdf", options);
}

You can also use fluent syntax

With v24.10 and later:

FluentConverter
    .Load("sample_with_password.docx").WithOptions((LoadContext loadContext) => new WordProcessingLoadOptions
    {
        Password = "12345"
    })
    .ConvertTo("converted.pdf")
    .Convert();

Before v24.10:

FluentConverter
    .Load("sample_with_password.docx").WithOptions(() => new WordProcessingLoadOptions
            {
                Password = "12345"
            })
    .ConvertTo("converted.pdf")
    .Convert();