Save PDF files in C#
To save a PDF document to a Stream object, use the SaveToStream method of the GdPicturePDF class.
Common parameters:
Stream— DestinationStream.PackDocument(optional) — Packs the PDF before saving (smaller file, slower save).Linearize(optional) — Enables Fast Web View mode. For details, see linearize PDF.
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.
SaveToStream returns a GdPictureStatus, which should be checked.
Packing removes unused PDF objects and recompresses used objects. You can combine this with RemoveUnusedResources method before saving.
To save a PDF document to a Stream with packing and Fast Web View enabled:
using GdPicture14;using System;using System.IO;
using GdPicturePDF gdPicturePDF = new GdPicturePDF();using MemoryStream streamPDF = new MemoryStream();
GdPictureStatus status = gdPicturePDF.LoadFromFile(@"C:\temp\source.pdf");if (status != GdPictureStatus.OK){ Console.WriteLine($"LoadFromFile failed: {status}"); return;}
status = gdPicturePDF.SaveToStream(streamPDF, true, true);if (status != GdPictureStatus.OK){ Console.WriteLine($"SaveToStream failed: {status}");}Imports GdPicture14Imports System.IO
Using gdPicturePDF As New GdPicturePDF(), streamPDF As New MemoryStream()
Dim status As GdPictureStatus = gdPicturePDF.LoadFromFile("C:\temp\source.pdf") If status <> GdPictureStatus.OK Then Console.WriteLine($"LoadFromFile failed: {status}") Return End If
status = gdPicturePDF.SaveToStream(streamPDF, True, True) If status <> GdPictureStatus.OK Then Console.WriteLine($"SaveToStream failed: {status}") End IfEnd UsingSaving a password-protected PDF to a stream
You can set two password types for a PDF document:
- User password (open password) — Enables opening and viewing a PDF document.
- Owner password (master password or permissions password) — Enables a user to copy, edit, or print a PDF document.
Both password types can be set for the same PDF. This means 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 content depending on the permissions settings, unless they provide the owner password to remove those restrictions.
- Both user and owner passwords are set — The file can only be opened by providing either 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 content.
Encrypting isn’t enabled for PDF/A documents.
To save a password-protected PDF to a Stream, use the SaveToStream method overload.
using GdPicture14;using System;using System.IO;
using GdPicturePDF gdPicturePDF = new GdPicturePDF();using MemoryStream streamPDF = new MemoryStream();
GdPictureStatus status = gdPicturePDF.LoadFromFile(@"C:\temp\source.pdf");if (status != GdPictureStatus.OK){ Console.WriteLine($"LoadFromFile failed: {status}"); return;}
status = gdPicturePDF.SaveToStream( streamPDF, PdfEncryption.PdfEncryption128BitAES, "", "owner", CanPrint: true, CanCopy: true, CanModify: false, CanAddNotes: false, CanFillFields: true, CanCopyAccess: false, CanAssemble: false, CanPrintFull: false);
if (status != GdPictureStatus.OK){ Console.WriteLine($"SaveToStream (encrypted) failed: {status}");}Imports GdPicture14Imports System.IO
Using gdPicturePDF As New GdPicturePDF(), streamPDF As New MemoryStream()
Dim status As GdPictureStatus = gdPicturePDF.LoadFromFile("C:\temp\source.pdf") If status <> GdPictureStatus.OK Then Console.WriteLine($"LoadFromFile failed: {status}") Return End If
status = gdPicturePDF.SaveToStream( streamPDF, PdfEncryption.PdfEncryption128BitAES, "", "owner", CanPrint:=True, CanCopy:=True, CanModify:=False, CanAddNotes:=False, CanFillFields:=True, CanCopyAccess:=False, CanAssemble:=False, CanPrintFull:=False)
If status <> GdPictureStatus.OK Then Console.WriteLine($"SaveToStream (encrypted) failed: {status}") End IfEnd Using