Save PDF files in C#
To save a PDF document to a file on your local machine, use the SaveToFile method of the GdPicturePDF class.
Common parameters:
FilePath— Output file path. If the file exists, it’s overwritten.PackDocument(optional) — Packs the PDF before saving (smaller file size, slower save).Linearize(optional) — Enables Fast Web View.
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.
SaveToFile returns a GdPictureStatus, which should always be checked.
Packing removes unused PDF objects and recompresses used objects. You can combine this with the RemoveUnusedResources method before saving.
To save a PDF document to a file with packing and Fast Web View enabled, use the following code:
using GdPicture14;using System;
using GdPicturePDF gdPicturePDF = new GdPicturePDF();
GdPictureStatus status = gdPicturePDF.LoadFromFile(@"C:\temp\source.pdf");if (status != GdPictureStatus.OK){ Console.WriteLine($"LoadFromFile failed: {status}"); return;}
status = gdPicturePDF.SaveToFile(@"C:\temp\output.pdf", true, true);if (status != GdPictureStatus.OK){ Console.WriteLine($"SaveToFile failed: {status}");}Imports GdPicture14
Using gdPicturePDF As New GdPicturePDF() 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.SaveToFile("C:\temp\output.pdf", True, True) If status <> GdPictureStatus.OK Then Console.WriteLine($"SaveToFile failed: {status}") End IfEnd UsingSaving a password-protected PDF
Encrypting isn’t enabled for PDF/A documents.
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.
To save a password-protected PDF, use the SaveToFile method overload with encryption parameters.
using GdPicture14;using System;
using GdPicturePDF gdPicturePDF = new GdPicturePDF();
GdPictureStatus status = gdPicturePDF.LoadFromFile(@"C:\temp\source.pdf");if (status != GdPictureStatus.OK){ Console.WriteLine($"LoadFromFile failed: {status}"); return;}
status = gdPicturePDF.SaveToFile( @"C:\temp\output.pdf", 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($"SaveToFile (encrypted) failed: {status}");}Imports GdPicture14
Using gdPicturePDF As New GdPicturePDF() 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.SaveToFile( "C:\temp\output.pdf", 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($"SaveToFile (encrypted) failed: {status}") End IfEnd Using