To ensure the confidentiality and integrity of the information contained within, PDF documents can be encrypted. This can be done by specifying a user password and a master password. The reader then needs the user password to open the document and read its contents. If a master password is provided, certain settings and restrictions can be changed. These include content editing, copying, and printing.
Create PDFs Programmatically
This article describes how to programmatically generate PDF documents from HTML content in a .NET Console App.
Prerequisites
The following tutorial requires a trial version of TX Text Control .NET Server for ASP.NET.
Preparing the Application
For the purposes of this demo, a .NET 6 console application is built.
-
In Visual Studio, create a new Console App using .NET 6.
-
In the Solution Explorer, select your created project and choose Manage NuGet Packages... from the Project main menu.
Select Text Control Offline Packages from the Package source drop-down.
Install the latest versions of the following package:
- TXTextControl.TextControl.ASP.SDK
Adding the Code
-
Open the Program.cs file and add the following code:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersusing (TXTextControl.ServerTextControl tx = new TXTextControl.ServerTextControl()) { tx.Create(); TXTextControl.Selection selection = new TXTextControl.Selection(); selection.Text = "Hello PDF!"; selection.FontSize = 360; selection.FontName = "Arial"; selection.ForeColor = System.Drawing.Color.Red; tx.Selection = selection; tx.Save("output.pdf", TXTextControl.StreamType.AdobePDFA); }
Running the Application
-
Run the application and check the output folder for the generated PDF document.
Encrypt with User Password
If a user password is used for document encryption, the user will be prompted for the password in order to open the document. In order to do this, the Save ╰ TX Text Control .NET Server for ASP.NET
╰ TXTextControl Namespace
╰ SaveSettings Class
The SaveSettings class provides properties for advanced settings and information during save operations. are used to provide the user password.
using (TXTextControl.ServerTextControl tx = new TXTextControl.ServerTextControl()) | |
{ | |
tx.Create(); | |
TXTextControl.Selection selection = new TXTextControl.Selection(); | |
selection.Text = "Hello PDF!"; | |
selection.FontSize = 360; | |
selection.FontName = "Arial"; | |
selection.ForeColor = System.Drawing.Color.Red; | |
tx.Selection = selection; | |
TXTextControl.SaveSettings saveSettings = new TXTextControl.SaveSettings() | |
{ | |
UserPassword = "textcontrol" | |
}; | |
tx.Save("output.pdf", TXTextControl.StreamType.AdobePDF, saveSettings); | |
} |
When this document is opened in Acrobat Reader, the user is prompted for the password to open the document.
In Acrobat Reader, you can verify document security using the document security settings. These documents will be encrypted and will not be visible to search engines on the web as well.
Encrypt with Master Password
When you specify the master password, you can set a variety of restrictions, such as printing, copying, and editing.
using (TXTextControl.ServerTextControl tx = new TXTextControl.ServerTextControl()) | |
{ | |
tx.Create(); | |
TXTextControl.Selection selection = new TXTextControl.Selection(); | |
selection.Text = "Hello PDF!"; | |
selection.FontSize = 360; | |
selection.FontName = "Arial"; | |
selection.ForeColor = System.Drawing.Color.Red; | |
tx.Selection = selection; | |
TXTextControl.SaveSettings saveSettings = new TXTextControl.SaveSettings() | |
{ | |
UserPassword = "textcontrol", | |
MasterPassword = "textcontrol", | |
DocumentAccessPermissions = TXTextControl.DocumentAccessPermissions.AllowGeneralEditing | | |
TXTextControl.DocumentAccessPermissions.AllowLowLevelPrinting | | |
TXTextControl.DocumentAccessPermissions.AllowExtractContents | |
}; | |
tx.Save("output.pdf", TXTextControl.StreamType.AdobePDF, saveSettings); | |
} |
These settings will be reflected in the security settings of the Actobat document.
The following restrictions can be set:
AllowAll
After the document has been opened no further document access is restricted.
AllowAuthoring
Allows comments to be added and interactive form fields (including signature fields) to be filled in.
AllowAuthoringFields
Allows existing interactive form fields (including signature fields) to be filled in.
AllowContentAccessibility
Allows content access for the visually impaired only.
AllowDocumentAssembly
Allows the document to be to assembled (insert, rotate or delete pages and create bookmarks or thumbnails).
AllowExtractContents
Allows text and/or graphics to be extraced.
AllowGeneralEditing
Allows the document contents to be modified.
AllowHighLevelPrinting
Allows the document to be printed.
AllowLowLevelPrinting
Allows the document to be printed (low-level).
Learn More
With TX Text Control, adding digital and electronic signatures to documents is a simple task. This article shows how to sign the entire document with a digital certificate and how to sign individual signature fields.