Back to Devexpress

PdfDocumentProcessor.SaveDocument(Stream, PdfSaveOptions) Method

officefileapi-devexpress-dot-pdf-dot-pdfdocumentprocessor-dot-savedocument-x28-system-dot-io-dot-stream-devexpress-dot-pdf-dot-pdfsaveoptions-x29.md

latest6.9 KB
Original Source

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.v25.2.dll

NuGet Package : DevExpress.Document.Processor

Declaration

csharp
public void SaveDocument(
    Stream stream,
    PdfSaveOptions options
)
vb
Public Sub SaveDocument(
    stream As Stream,
    options As PdfSaveOptions
)

Parameters

NameTypeDescription
streamStream

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

csharp
using DevExpress.Pdf;
using System.IO;

namespace PDFPasswordProtection {
    class Program{
        static void Main(string[] args) {
            using (PdfDocumentProcessor pdfDocumentProcessor = new PdfDocumentProcessor()) {
                // Load a PDF document.
                pdfDocumentProcessor.LoadDocument(@"..\..\Demo.pdf");

                // Initialize encryption options with owner and user passwords.
                PdfEncryptionOptions encryptionOptions = new PdfEncryptionOptions(
                    ownerPassword: "OwnerPassword",
                    userPassword: "UserPassword"
                );

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

                // 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
                    });
                }
            }
        }
    }
}
vb
Imports DevExpress.Pdf
Imports System.IO

Module PDFPasswordProtection
    Sub Main()
        Using pdfDocumentProcessor As New PdfDocumentProcessor()

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

            ' Initialize encryption options with owner and user passwords.
            Dim encryptionOptions As New PdfEncryptionOptions(
                ownerPassword:="OwnerPassword",
                userPassword:="UserPassword"
            )

            ' Specify printing, data extraction, modification, and interactivity permissions.
            encryptionOptions.PrintingPermissions = PdfDocumentPrintingPermissions.Allowed
            encryptionOptions.DataExtractionPermissions = PdfDocumentDataExtractionPermissions.NotAllowed
            encryptionOptions.ModificationPermissions = PdfDocumentModificationPermissions.DocumentAssembling
            encryptionOptions.InteractivityPermissions = PdfDocumentInteractivityPermissions.Allowed

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

            ' Save the protected document with encryption settings.
            Using stream As New FileStream("Result.pdf", FileMode.Create, FileAccess.ReadWrite)
                pdfDocumentProcessor.SaveDocument(stream, New PdfSaveOptions() With {
                    .EncryptionOptions = encryptionOptions
                })
            End Using
        End Using
    End Sub
End Module

The following code snippet (auto-collected from DevExpress Examples) contains a reference to the SaveDocument(Stream, PdfSaveOptions) method.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

office-file-api-in-web-api-app/CS/Controllers/PdfController.cs#L79

csharp
encryptionOptions.Algorithm = PdfEncryptionAlgorithm.AES256;
processor.SaveDocument(result, new PdfSaveOptions() { EncryptionOptions = encryptionOptions });
result.Seek(0, SeekOrigin.Begin);

See Also

PdfDocumentProcessor Class

PdfDocumentProcessor Members

DevExpress.Pdf Namespace