Save PDF files in C#

To Stream

To save a PDF document to a Stream object, use the SaveToStream method of the GdPicturePDF class. This method uses the following parameters:

  • Stream — The Stream object where you want to save the PDF document.
  • PackDocument — Optional: A Boolean value that specifies if the PDF is packed before saving it to a Stream object. Packing reduces the file size but slows down the saving process.
  • Linearize — Optional: A Boolean value that specifies if the PDF has the Fast Web View mode enabled. For more information, refer to the linearize PDF guide.

Packing a PDF document before saving it removes unused PDF objects and compresses used objects. This process creates a new PDF document by cloning all existing pages of the current document to a new document. You can also combine this mechanism with the RemoveUnusedResources method to delete all unused resources from your PDF document.

The Fast Web View mode makes it possible to download the content one page at a time from the web. This feature is useful for large documents.

To save a PDF document to a Stream object with the packing and the Fast Web View mode enabled, use the following code:

using GdPicturePDF gdPicturePDF = new GdPicturePDF();
// Create an empty `Stream` object.
MemoryStream streamPDF = new MemoryStream();
// Load a PDF document to GdPicture.
gdPicturePDF.LoadFromFile(@"C:\temp\source.pdf");
// Save the loaded PDF document to the stream.
gdPicturePDF.SaveToStream(streamPDF, true, false);

Saving a password-protected PDF to a stream

Encrypting isn’t allowed for PDF/A documents.

You can set two password types for a PDF document:

  • User password (open password) — Allows opening and viewing a PDF document.
  • Owner password (master password or permissions password) — Allows a user to copy, edit, or print a PDF document.

Both password types can be set for the same PDF. This means that there are four possible scenarios:

  • No passwords are set — Anyone can open and edit a PDF document.
  • User password is set — The PDF document can be opened after providing the password. After opening the file, the user can then edit the PDF document.
  • Owner password is set — The PDF document can be opened without the password. The user may have limited access to printing, editing, and copying the content depending on the permissions settings, unless they provide the owner password to remove those restrictions.
  • Both the user password and owner password are set — The file can only be opened by providing either the user or the owner password. With the user password, access to printing, editing, and copying content might be restricted depending on the permission settings. With the owner password, the user has no restrictions and can print, edit, and copy the content.

To save a password-protected PDF document to a Stream object, use the SaveToStream method of the GdPicturePDF class with the following parameters:

  • Stream — The Stream object you want to save the PDF document to.
  • EncryptionScheme — A member of the PdfEncryption enumeration that specifies which encryption algorithm to use.
  • UserPass — The user password (open password), which allows opening and viewing a PDF document. If you don’t want to set the user password, use an empty string.
  • OwnerPass — The owner password (master password or permissions password), which allows a user to copy, edit, or print a PDF document. If you don’t want to set the owner password, use an empty string.
  • CanPrint — A Boolean value that specifies if a user is able to print the PDF document.
  • CanCopy — A Boolean value that specifies if a user is able to copy or extract text and graphics from the PDF document.
  • CanModify — A Boolean value that specifies if a user is able to modify the PDF document.
  • CanAddNotes — A Boolean value that specifies if a user is able to add annotations to the PDF document.
  • CanFillFields — A Boolean value that specifies if a user is able to fill in form fields in the PDF document. This parameter only works with 128-bit encryption.
  • CanCopyAccess — A Boolean value that specifies if a user is able to copy or extract content from the PDF document using the accessibility features. This parameter only works with 128-bit encryption.
  • CanAssemble — A Boolean value that specifies if a user is able to assemble the PDF document. This parameter only works with 128-bit encryption.
  • CanPrintFull — A Boolean value that specifies if a user is able to print the PDF document in high resolution. This parameter only works with 128-bit encryption.
using GdPicturePDF gdPicturePDF = new GdPicturePDF();
// Create an empty `Stream` object.
MemoryStream streamPDF = new MemoryStream();
// Load a PDF document to GdPicture.
gdPicturePDF.LoadFromFile(@"C:\temp\source.pdf");
// Save the loaded PDF document to the stream with password-protection enabled.
gdPicturePDF.SaveToStream(streamPDF, PdfEncryption.PdfEncryption128BitAES, "", "owner",
CanPrint: true,
CanCopy: true,
CanModify: false,
CanAddNotes: false,
CanFillFields: true,
CanCopyAccess: false,
CanAssemble: false,
CanPrintFull: false);