Skip to content

Instantly share code, notes, and snippets.

@joshi-kumar
Last active April 2, 2018 12:26
Show Gist options
  • Save joshi-kumar/8310c586680cb472c5ac5274687c5272 to your computer and use it in GitHub Desktop.
Save joshi-kumar/8310c586680cb472c5ac5274687c5272 to your computer and use it in GitHub Desktop.
Protect PDF file
using iTextSharp.text.pdf;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProtectPDF
{
class Program
{
static void Main(string[] args)
{
protectPdf();
}
protected static void protectPdf()
{
//Set the file location. (get the file or save the protected file)
string WorkingFolder = Environment.GetFolderPath(Environment.SpecialFolder.MyMusic);
//Get existing pdf file.
string InputFile = Path.Combine(WorkingFolder, "ticket.pdf");
//Save procted file in same path.
string OutputFile = Path.Combine(WorkingFolder, "myticket.pdf");
using (Stream input = new FileStream(InputFile, FileMode.Open))
{
using (Stream output = new FileStream(OutputFile, FileMode.Create, FileAccess.Write))
{
PdfReader reader = new PdfReader(input);
PdfEncryptor.Encrypt(reader, output, true, "admin", null, PdfWriter.ALLOW_SCREENREADERS);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment