Load a PDF file from local storage in C#
To load a document from local storage, use the LoadFromFile method from the GdPicturePDF class.
LoadFromFile supports PDF and TXT input:
- PDF files are loaded directly.
- TXT files are converted to a PDF document.
The optional LoadInMemory Boolean parameter controls whether the file content is loaded into memory:
false(default) — Lower memory usage, but the source file stays locked while the document is open.true— Better manipulation performance and enables you to overwrite/delete the source file while working.
LoadFromFile returns a GdPictureStatus, and you should always verify that it equals GdPictureStatus.OK.
using GdPicture14;
using GdPicturePDF pdf = new GdPicturePDF();
// Load without loading document content into memory.GdPictureStatus status = pdf.LoadFromFile(@"C:\temp\source.pdf", false);if (status != GdPictureStatus.OK){ Console.WriteLine($"Load failed: {status}"); return;}
pdf.CloseDocument();
// Load with document content in memory.status = pdf.LoadFromFile(@"C:\temp\source.pdf", true);if (status != GdPictureStatus.OK){ Console.WriteLine($"Load failed: {status}"); return;}Imports GdPicture14
Using pdf As New GdPicturePDF() ' Load without loading document content into memory. Dim status As GdPictureStatus = pdf.LoadFromFile("C:\temp\source.pdf", False) If status <> GdPictureStatus.OK Then Console.WriteLine($"Load failed: {status}") Return End If
pdf.CloseDocument()
' Load with document content in memory. status = pdf.LoadFromFile("C:\temp\source.pdf", True) If status <> GdPictureStatus.OK Then Console.WriteLine($"Load failed: {status}") Return End IfEnd UsingLoading a password-protected PDF
A PDF can define:
- A user password (open password), required to open the document.
- An owner password (permissions/master password), used to grant full permissions.
Depending on which password is set and which one is provided, behavior differs:
- No passwords set — Anyone can open and edit the PDF.
- Only user password set — The PDF opens only after entering the user password, and then the document can be edited.
- Only owner password set — The PDF can be opened without a password, but owner-level operations require the owner password.
- Both passwords set — Entering the user password enables viewing with restrictions; entering the owner password enables full permissions.
To work with encrypted PDFs:
- Load the file.
- Call the
IsEncryptedmethod. - If encrypted, call the
SetPasswordmethod with a user or owner password. - Verify the return status and check
IsEncryptedagain.
using GdPicture14;
using GdPicturePDF pdf = new GdPicturePDF();GdPictureStatus status = pdf.LoadFromFile(@"C:\temp\source.pdf", false);if (status != GdPictureStatus.OK){ Console.WriteLine($"Load failed: {status}"); return;}
if (pdf.IsEncrypted()){ if (pdf.GetStat() != GdPictureStatus.OK) { Console.WriteLine($"IsEncrypted failed: {pdf.GetStat()}"); return; }
status = pdf.SetPassword("user"); if (status != GdPictureStatus.OK) { Console.WriteLine($"SetPassword failed: {status}"); return; }
bool stillEncrypted = pdf.IsEncrypted(); if (pdf.GetStat() == GdPictureStatus.OK) { Console.WriteLine($"Still encrypted: {stillEncrypted}"); }}Imports GdPicture14
Using pdf As New GdPicturePDF() Dim status As GdPictureStatus = pdf.LoadFromFile("C:\temp\source.pdf", False) If status <> GdPictureStatus.OK Then Console.WriteLine($"Load failed: {status}") Return End If
If pdf.IsEncrypted() Then If pdf.GetStat() <> GdPictureStatus.OK Then Console.WriteLine($"IsEncrypted failed: {pdf.GetStat()}") Return End If
status = pdf.SetPassword("user") If status <> GdPictureStatus.OK Then Console.WriteLine($"SetPassword failed: {status}") Return End If
Dim stillEncrypted As Boolean = pdf.IsEncrypted() If pdf.GetStat() = GdPictureStatus.OK Then Console.WriteLine($"Still encrypted: {stillEncrypted}") End If End IfEnd UsingAfter loading an encrypted PDF, IsEncrypted() returns true (if the call succeeds). After setting a correct password with SetPassword, IsEncrypted() returns false.