How to disallow text copying and printing in PDF using C# and VB.NET?
Syncfusion Essential PDF is the .NET PDF library used to create, read, and edit PDF documents. Using this library, you can disallow text copying and printing the PDF document.
Steps to disallow text copying and printing PDF file programmatically:
- Create a new C# console application project.
- Install the Syncfusion.Pdf.WinForms NuGet package as reference to your .NET Framework applications from NuGet.org.
- Include the following namespaces in the Program.cs file.
C#
using Syncfusion.Pdf.Parsing; using Syncfusion.Pdf.Security;
VB.NET
Imports Syncfusion.Pdf.Parsing Imports Syncfusion.Pdf.Security
- Use the following C# or VB.NET code snippet to disallow text copying and printing in a PDF document.
C#
//Load the existing PDF document PdfLoadedDocument document = new PdfLoadedDocument("input.pdf"); //Set the user password document.Security.UserPassword = "user"; //Set the owner password document.Security.OwnerPassword = "owner"; //Set permission to default to disallowing text copy and printing document.Security.Permissions = PdfPermissionsFlags.Default; //Save the document document.Save("output.pdf"); //Close the document document.Close(true);
VB.NET
'Load the existing PDF document Dim document As New PdfLoadedDocument("input.pdf") 'Set the user password document.Security.UserPassword = "user" 'Set the owner password document.Security.OwnerPassword = "owner" 'Set permission to default to disallowing text copy and printing document.Security.Permissions = PdfPermissionsFlags.Default 'Save the document document.Save("output.pdf") 'Close the document document.Close(True)
By executing the program, you will get the PDF document as follows.
A complete working sample can be downloaded from SecuritySample.zip
You can also disallow the text copying and printing without using document open password by removing the following code.
//Set the user password document.Security.UserPassword = "user";
Take a moment to peruse the documentation, where you can find other options like encrypting, decrypting and digitalizing a PDF document.
Refer here to explore the rich set of Syncfusion Essential PDF features.
Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to link to learn about generating and registering Syncfusion license key in your application to use the components without trail message.