Load a PDF File from Local Storage in C#
To load a PDF document to your program from your local storage, use the LoadFromFile
method from the GdPicturePDF
class. This class is used for almost all PDF features that GdPicture.NET SDK offers. The LoadFromFile
method requires a string parameter, which is the full path of the document you want to load to your program. It’s possible to specify whether the file is loaded into your local machine’s memory with the optional LoadInMemory
Boolean parameter. By default, it’s set to false
. Loading a document directly into the memory results in a better manipulation performance, but it consumes more memory.
It’s possible to load both PDF and TXT files with the
LoadFromFile
method. If you load a TXT file, the method will automatically convert it to a PDF file.
To load a PDF document from your local storage, use the following code:
using GdPicturePDF gdpicturePDF2 = new GdPicturePDF(); // Load a PDF document from the specified path without loading it to memory. gdpicturePDF2.LoadFromFile(@"C:\temp\source.pdf"); using GdPicturePDF gdpicturePDF1 = new GdPicturePDF(); // Load a PDF document from the specified path into memory. gdpicturePDF1.LoadFromFile(@"C:\temp\source.pdf", true);
Using gdpicturePDF2 As GdPicturePDF = New GdPicturePDF() ' Load a PDF document from the specified path without loading it to memory. gdpicturePDF2.LoadFromFile("C:\temp\source.pdf") End Using Using gdpicturePDF1 As GdPicturePDF = New GdPicturePDF() ' Load a PDF document from the specified path into memory. gdpicturePDF1.LoadFromFile("C:\temp\source.pdf", True) End Using
Loading a Password-Protected PDF
There are two possible password types that can be set 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 has to provide the owner password to edit the document.
-
Both user password and owner password are set — When entering the user password, the user can only view the file. If the owner password is entered, then the user can view and edit the document.
To open a password-encrypted PDF document, use the SetPassword
method. It only requires one string parameter, which is the password with which you want to open the PDF document.
To open a password-encrypted PDF document, use the following code:
using GdPicturePDF gdpicturePDF = new GdPicturePDF(); // Load a password-protected PDF document. gdpicturePDF.LoadFromFile(@"C:\temp\source.pdf"); // Check if the file is encrypted. if (gdpicturePDF.IsEncrypted()) { // Set the correct password for the PDF document. gdpicturePDF.SetPassword("user"); // Check if the file is still encrypted. Console.WriteLine(gdpicturePDF.IsEncrypted()); }
Using gdpicturePDF As GdPicturePDF = New GdPicturePDF() ' Load a password-protected PDF document. gdpicturePDF.LoadFromFile("C:\temp\source.pdf") ' Check if the file is encrypted. If gdpicturePDF.IsEncrypted() Then ' Set the correct password for the PDF document. gdpicturePDF.SetPassword("user") ' Check if the file is still encrypted. Console.WriteLine(gdpicturePDF.IsEncrypted()) End If End Using
After loading the PDF, the IsEncrypted
method returns a False
value. After setting the correct password with the SetPassword
method, the return value of the IsEncrypted
method is True
.