Go4Expert

Go4Expert (http://www.go4expert.com/)
-   ASP.NET (http://www.go4expert.com/articles/asp-net-tutorials/)
-   -   Add Password in PDF File (http://www.go4expert.com/articles/add-password-pdf-file-t18273/)

naimish 1Jul2009 09:53

Add Password in PDF File
 

Background



This article continues my previous one, to add watermark in PDF files, now we will go ahead and look how to add password in PDF File. Please check Add WaterMark In PDF Files.

The code



Code: VB.NET

Imports System
Imports System.IO
Imports iTextSharp.text
Imports iTextSharp.text.pdf
Public Class Form1
Inherits System.Windows.Forms.Form
    Private Sub brnOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles brnOpen.Click
        Dim FldBrow As OpenFileDialog = New OpenFileDialog()
        FldBrow.Title = "Select PDF File To Add WaterMark"
        FldBrow.InitialDirectory = "c:\"
        FldBrow.Filter = "PDF Files (*.pdf)|*.pdf"
        FldBrow.FilterIndex = 2
        FldBrow.RestoreDirectory = True
        If FldBrow.ShowDialog() = Windows.Forms.DialogResult.OK Then
            txtPath.Text = FldBrow.FileName
        End If
    End Sub
   
    Private Sub breimgopen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles breimgopen.Click
        MessageBox.Show(txtpass.Text)
    End Sub

    Private Sub btnoutput_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnoutput.Click
        Dim sfd1 As SaveFileDialog = New SaveFileDialog()
        sfd1.Title = "Save Output File"
        sfd1.InitialDirectory = "c:\"
        sfd1.Filter = "PDF files (*.pdf)|*.pdf"
        sfd1.FilterIndex = 2
        sfd1.RestoreDirectory = True
        If sfd1.ShowDialog() = Windows.Forms.DialogResult.OK Then
            txtoutpath.Text = sfd1.FileName
        End If
    End Sub

    Private Sub btnexit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnexit.Click
        End
    End Sub
   
    Private Sub btnAddWaterMark_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddWaterMark.Click
        Dim input As String = ""
        Dim output As String = ""
        Dim password As String = ""
        input = txtPath.Text
        password = txtpass.Text
        output = txtoutpath.Text

        AddPasswordToPDF(input, output, password)
    End Sub

    Private Sub AddPasswordToPDF(ByVal sourceFile As String, ByVal outputFile As String, ByVal password As String)
        Dim pReader As PdfReader = New PdfReader(sourceFile)
        PdfEncryptor.Encrypt(pReader, New FileStream(outputFile, FileMode.Open), PdfWriter.STRENGTH128BITS, password, Nothing, PdfWriter.AllowScreenReaders)
    End Sub
End Class


pradeep 2Jul2009 11:54

Re: Add Password in PDF File
 
What are the Libraries required to implement this?

naimish 2Jul2009 12:02

Re: Add Password in PDF File
 
Libraries ? The code itself works, refer to my previous post for steps.

nimesh 2Jul2009 13:04

Re: Add Password in PDF File
 
Quote:

Originally Posted by pradeep (Post 51066)
What are the Libraries required to implement this?

You will need iTextSharp.dll
Naimish had mentioned this in the other article.

naimish 2Jul2009 13:58

Re: Add Password in PDF File
 
Thanks for clerifying nimesh :)

night.rider 13Jul2009 22:06

Re: Add Password in PDF File in C#.net
 
sir really ... this adding password to pdf works and i wrote in C#.net
it really works... awesome yaar...

sir can u tell me if before encryption the pdf is encrypted then wat to do becoz the pdfencrypter gives error...

sir please , if more u can tell abt all this and more

night.rider 13Jul2009 22:07

Re: Add Password in PDF File in C#.net
 
Quote:

Originally Posted by night.rider (Post 52440)
sir really ... this adding password to pdf works and i wrote in C#.net
it really works... awesome yaar...

sir can u tell me if before encryption the pdf is encrypted then wat to do becoz the pdfencrypter gives error...

sir please , if more u can tell abt all this and more


C#.net code as below
Code:

        private void btnopen_Click(object sender, EventArgs e)
        {
            OpenFileDialog FldBrow = new OpenFileDialog();
            FldBrow.Title = "Select PDF To Add Password";
            FldBrow.InitialDirectory="c:\\";
            FldBrow.Filter = "PDF Files (*.pdf)|*.pdf";
            FldBrow.FilterIndex = 2;
            FldBrow.RestoreDirectory=true;
            if(FldBrow.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                textBox1.Text=FldBrow.FileName;
        }

        private void Save_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd1=new SaveFileDialog();
            sfd1.Title = "Save Output File";
            sfd1.InitialDirectory = "My Documents\\";
            sfd1.Filter = "PDF files (*.pdf)|*.pdf";
            sfd1.FilterIndex = 2;
            sfd1.RestoreDirectory = true;
            if(sfd1.ShowDialog()==System.Windows.Forms.DialogResult.OK)
                textBox2.Text=sfd1.FileName;
        }

        private void addpwd_Click(object sender, EventArgs e)
        {
            string input = "";
            string output = "";
            string password = "";
            input = textBox1.Text;
            output = textBox2.Text;
            password = txtpass.Text;

            Add_pwd_to_pdf(input, output, password);
        }

        private void Add_pwd_to_pdf(string input, string output, string password)
        {
            //string ownpwd = "123";
            PdfReader preader = new PdfReader(input);
            PdfEncryptor.Encrypt(preader, new FileStream(output, FileMode.Append), PdfWriter.STRENGTH128BITS, password,"",PdfWriter.ALLOW_SCREENREADERS);
        }


shabbir 15Jul2009 23:21

Re: Add Password in PDF File
 
Start voting for this article for Article of the month - June 2009

shabbir 3Aug2009 14:35

Re: Add Password in PDF File
 
Nominate this article for Article of the month - Jul 2009

dasli 13Aug2009 16:03

Re: Add Password in PDF File
 
ooo...
thanks:)


All times are GMT +5.5. The time now is 10:04.