1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Add Password in PDF File

Discussion in 'ASP.NET' started by naimish, Jul 1, 2009.

  1. naimish

    naimish New Member

    Joined:
    Jun 29, 2009
    Messages:
    1,046
    Likes Received:
    18
    Trophy Points:
    0
    Occupation:
    Software Engineer
    Location:
    On Earth

    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:
    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
    
     
    shabbir and (deleted member) like this.
  2. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,646
    Likes Received:
    86
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    What are the Libraries required to implement this?
     
  3. naimish

    naimish New Member

    Joined:
    Jun 29, 2009
    Messages:
    1,046
    Likes Received:
    18
    Trophy Points:
    0
    Occupation:
    Software Engineer
    Location:
    On Earth
    Libraries ? The code itself works, refer to my previous post for steps.
     
    Last edited: Jul 2, 2009
  4. nimesh

    nimesh New Member

    Joined:
    Apr 13, 2009
    Messages:
    770
    Likes Received:
    20
    Trophy Points:
    0
    Occupation:
    Oracle Apps Admin
    Location:
    Mumbai
    Home Page:
    You will need iTextSharp.dll
    Naimish had mentioned this in the other article.
     
  5. naimish

    naimish New Member

    Joined:
    Jun 29, 2009
    Messages:
    1,046
    Likes Received:
    18
    Trophy Points:
    0
    Occupation:
    Software Engineer
    Location:
    On Earth
    Thanks for clerifying nimesh :)
     
  6. night.rider

    night.rider New Member

    Joined:
    Jul 13, 2009
    Messages:
    14
    Likes Received:
    1
    Trophy Points:
    0
    Occupation:
    create and implement Logic..
    Location:
    somewhere
    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
     
  7. night.rider

    night.rider New Member

    Joined:
    Jul 13, 2009
    Messages:
    14
    Likes Received:
    1
    Trophy Points:
    0
    Occupation:
    create and implement Logic..
    Location:
    somewhere
    Re: Add Password in PDF File in C#.net


    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 likes this.
  8. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,292
    Likes Received:
    365
    Trophy Points:
    83
  9. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,292
    Likes Received:
    365
    Trophy Points:
    83
  10. dasli

    dasli New Member

    Joined:
    Aug 13, 2009
    Messages:
    15
    Likes Received:
    0
    Trophy Points:
    0
    ooo...
    thanks:)
     
  11. naimish

    naimish New Member

    Joined:
    Jun 29, 2009
    Messages:
    1,046
    Likes Received:
    18
    Trophy Points:
    0
    Occupation:
    Software Engineer
    Location:
    On Earth
    The Pleasure is mine and shabbir too ;)
     
  12. naimish

    naimish New Member

    Joined:
    Jun 29, 2009
    Messages:
    1,046
    Likes Received:
    18
    Trophy Points:
    0
    Occupation:
    Software Engineer
    Location:
    On Earth
    Please check out my new article on Add Text WaterMark In PDF Files :D
     
  13. szoasis

    szoasis New Member

    Joined:
    Nov 23, 2010
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Home Page:
    Thanks for clerifying nimesh - _ -~!
     
  14. jana596

    jana596 New Member

    Joined:
    Nov 11, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Retiree
    Location:
    Bangalore
    Thanks for the article
     
  15. tinywooz

    tinywooz New Member

    Joined:
    Oct 21, 2011
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Thank you for sharing us the code library to add password in pdf file, although I am not familiar with this way, last time i needed to add password to pdf which is already existed. I know Acrobat may be the right stuff to achieve this. But I don't want to pay for it for this tiny problem.
    Try free or lower price tool to add password to pdf or use the JavaScript method is ideal way for me.
     
  16. sbglobal123

    sbglobal123 New Member

    Joined:
    Jul 17, 2010
    Messages:
    16
    Likes Received:
    0
    Trophy Points:
    0
    Home Page:
    thanks for good tips for PDF safty
     
  17. tatillo

    tatillo New Member

    Joined:
    Jan 11, 2012
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Hello i follow the example about Add Password in PDF File but i have a problem:

    When i run AddPasswordToPDF(input, output, password) return access denied in c:\pdf

    My S.O. is Windows 7, i tried to set all permitions and i disabled Uac but not function.

    I tried also to change directory and disk but nothing.

    Please help me.

    Tanks
     

Share This Page