.NET Framework 4.5.2+
.NET Framework 4.5.2+
.NET Standard 2.0+

PdfDocumentProcessor.SaveDocument(Stream, PdfSaveOptions) Method

Saves the current PDF document to the specified stream with encryption settings and document signature.

Namespace: DevExpress.Pdf

Assembly: DevExpress.Docs.v21.1.dll

Declaration

public void SaveDocument(
    Stream stream,
    PdfSaveOptions options
)

Parameters

Name Type Description
stream Stream

A Stream value, specifying the location of the saved document.

options PdfSaveOptions

A PdfSaveOptions that contains the encryption settings and document signature that should be saved.

Remarks

When you dispose of an output stream that has not been detached, an attempt to apply further changes to a document may cause errors. If you want to close the stream when a document is saved, call the SaveDocument method overload with the detachStream parameter enabled.

If you load and save a document to the same stream, it may lead to unexpected results. Use the PdfDocumentProcessor.SaveDocument(Stream, PdfSaveOptions, Boolean) method overload for safer results.

Important

The PdfDocumentProcessor removes existing signatures from a document when it is saved. However, if you use PdfDocumentProcessor to apply a signature, it is retained.

Example

This example shows how a PDF document can be protected using both the owner and user passwords.

Refer to the following topic fro more information: Document Protection

using DevExpress.Pdf;

namespace PDFPasswordProtection
{
  class Program
  {
    static void Main(string[] args)
    {

      using (PdfDocumentProcessor pdfDocumentProcessor = new PdfDocumentProcessor()) {

        // Load a PDF document.
        pdfDocumentProcessor.LoadDocument("..\\..\\Demo.pdf");

        // Specify printing, data extraction, modification, and interactivity permissions. 
        PdfEncryptionOptions encryptionOptions = new PdfEncryptionOptions();
        encryptionOptions.PrintingPermissions = PdfDocumentPrintingPermissions.Allowed;
        encryptionOptions.DataExtractionPermissions = PdfDocumentDataExtractionPermissions.NotAllowed;
        encryptionOptions.ModificationPermissions = PdfDocumentModificationPermissions.DocumentAssembling;
        encryptionOptions.InteractivityPermissions = PdfDocumentInteractivityPermissions.Allowed;

        // Specify the owner and user passwords for the document.
        encryptionOptions.OwnerPasswordString = "OwnerPassword";
        encryptionOptions.UserPasswordString = "UserPassword";

        // Specify the 256-bit AES encryption algorithm.
         encryptionOptions.Algorithm = PdfEncryptionAlgorithm.AES256;

        // Save the protected document with encryption settings.
        using (FileStream stream = new FileStream("Result.pdf", FileMode.Create, FileAccess.ReadWrite))
        {
        pdfDocumentProcessor.SaveDocument(stream, new PdfSaveOptions()
           { EncryptionOptions = encryptionOptions });
        }
      }
    }
  }
}
See Also