Answered by:
Password Protected PDF using iTextSharp

Question
-
hello
i create pdf files using iTextSharp in vb.net .
i did it but now i need to create these pdf files as password protected and i can't find any method for this,
how can i create password protect a pdf using iTextSharp ?
- Moved by Franklin ChenMicrosoft employee Monday, January 20, 2014 12:31 PM 3rd- party
Sunday, January 12, 2014 7:36 PM
Answers
-
That is better suited to itextsharp support, I don't think it is a vb net issue.
Edit, here is a sample:
Imports ITextSharp Imports ITextSharp.text Imports ITextSharp.text.pdf Imports System.IO Imports System.Text Public Class Form1 Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click Dim doc As New Document(ITextSharp.text.PageSize.LETTER, 10, 10, 42, 35) doc.AddAuthor("Jerry O'Brien") doc.AddCreator("The Creator") doc.AddSubject("ITextSharp Sample") doc.AddTitle("A treatise on the production of Portable Document Format files") Dim writer As PdfWriter = PdfWriter.GetInstance(doc, New FileStream("D:\sample.pdf", FileMode.Create)) Dim Upass As String = "MumboJumbo" Dim OPass As String = "HeebieJeebie" Dim userpass() As Byte = Encoding.ASCII.GetBytes(Upass) Dim ownerpass() As Byte = Encoding.ASCII.GetBytes(OPass) writer.SetEncryption(userpass, ownerpass, 255, True) doc.Open() Dim para As New Paragraph("This is my first paragraph") Dim pharse As New Phrase("This is my first phrase" & vbNewLine) Dim chunk_ As New Chunk("This is my first chunk") doc.Add(para) doc.Add(pharse) doc.Add(chunk_) doc.Close() End Sub End Class
FYI - I have NO IDEA what the 255 and True mean, I just put something in there that fit.
- Edited by Devon_Nullman Sunday, January 12, 2014 8:54 PM
- Proposed as answer by Reed KimbleMVP Monday, January 13, 2014 10:39 PM
- Marked as answer by Just Karl Tuesday, February 11, 2014 11:34 PM
Sunday, January 12, 2014 8:23 PM -
http://weblog.kevinattard.com/2011/08/itextsharp-disable-pdf-printing.html
also see section 7.6.3.1 of this - http://www.adobe.com/content/dam/Adobe/en/devnet/acrobat/pdfs/PDF32000_2008.pdf
- Proposed as answer by Reed KimbleMVP Monday, January 13, 2014 10:39 PM
- Marked as answer by Just Karl Tuesday, February 11, 2014 11:34 PM
Monday, January 13, 2014 10:33 PM
All replies
-
That is better suited to itextsharp support, I don't think it is a vb net issue.
Edit, here is a sample:
Imports ITextSharp Imports ITextSharp.text Imports ITextSharp.text.pdf Imports System.IO Imports System.Text Public Class Form1 Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click Dim doc As New Document(ITextSharp.text.PageSize.LETTER, 10, 10, 42, 35) doc.AddAuthor("Jerry O'Brien") doc.AddCreator("The Creator") doc.AddSubject("ITextSharp Sample") doc.AddTitle("A treatise on the production of Portable Document Format files") Dim writer As PdfWriter = PdfWriter.GetInstance(doc, New FileStream("D:\sample.pdf", FileMode.Create)) Dim Upass As String = "MumboJumbo" Dim OPass As String = "HeebieJeebie" Dim userpass() As Byte = Encoding.ASCII.GetBytes(Upass) Dim ownerpass() As Byte = Encoding.ASCII.GetBytes(OPass) writer.SetEncryption(userpass, ownerpass, 255, True) doc.Open() Dim para As New Paragraph("This is my first paragraph") Dim pharse As New Phrase("This is my first phrase" & vbNewLine) Dim chunk_ As New Chunk("This is my first chunk") doc.Add(para) doc.Add(pharse) doc.Add(chunk_) doc.Close() End Sub End Class
FYI - I have NO IDEA what the 255 and True mean, I just put something in there that fit.
- Edited by Devon_Nullman Sunday, January 12, 2014 8:54 PM
- Proposed as answer by Reed KimbleMVP Monday, January 13, 2014 10:39 PM
- Marked as answer by Just Karl Tuesday, February 11, 2014 11:34 PM
Sunday, January 12, 2014 8:23 PM -
After you create the pdf file open the file in PdfReader and use the PdfEncryptor.Encrypt method.Sunday, January 12, 2014 8:23 PM
-
Here is a sample you can start with.
Dim InputFileStream As System.IO.FileStream = New System.IO.FileStream("C:\Test.pdf", System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read) Dim OutputFileStream As System.IO.FileStream = New System.IO.FileStream("C:\TestPassword.pdf", System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.ReadWrite, System.IO.FileShare.None) OutputFileStream.SetLength(0) Dim PdfReader As PdfReader = New PdfReader(InputFileStream) PdfEncryptor.Encrypt(PdfReader, OutputFileStream, True, password, password, PdfWriter.ALLOW_SCREENREADERS) InputFileStream.Close() InputFileStream.Dispose()
Sunday, January 12, 2014 8:29 PM -
Why does a posted code block suddenly look so strange ?
Is this just me ?
Monday, January 13, 2014 1:23 AM -
Why does a posted code block suddenly look so strange ?
Is this just me ?
Monday, January 13, 2014 1:48 AM -
Must be just you, try clearing your browser's cache maybe?
Mine seems fine.- Edited by Jayson Furr Monday, January 13, 2014 4:30 AM add ss
Monday, January 13, 2014 4:29 AM -
Hmmm - the actual codeblock I posted earlier now looks normal, Courier font, etc. The image I posted was a screenshot of that very same codeblock taken earlier. Now the reply from Jayson Furr has a different font than anything I have seen before but still legible.
Monday, January 13, 2014 4:44 AM -
My post was a screenshot of the same image you screenshot and posted. But yes it does not resemble what it should, looks completely different now.
Strange it keeps changing.
Monday, January 13, 2014 5:26 AM -
ok
i done it with the help of your code.
but i still have doubt about ownerpassword and userpassword ? i don't see ownerpassword useful for this task because file is only opened with userpassword ? so why need to have ownerpassword.
one more thing i find that 255 is permission but i am not familiar with permission process in windows (in linux its 4=R,2=W,1=Ex) so what permission should i set if i want that file should be opened by only administrative user.
Monday, January 13, 2014 3:41 PM -
http://weblog.kevinattard.com/2011/08/itextsharp-disable-pdf-printing.html
also see section 7.6.3.1 of this - http://www.adobe.com/content/dam/Adobe/en/devnet/acrobat/pdfs/PDF32000_2008.pdf
- Proposed as answer by Reed KimbleMVP Monday, January 13, 2014 10:39 PM
- Marked as answer by Just Karl Tuesday, February 11, 2014 11:34 PM
Monday, January 13, 2014 10:33 PM -
agreed: this is OT for these forums as 3rd party products are not supported here
Reed Kimble - "When you do things right, people won't be sure you've done anything at all"
Monday, January 13, 2014 10:40 PM -
Why does a posted code block suddenly look so strange ?
Is this just me ?
Reed Kimble - "When you do things right, people won't be sure you've done anything at all"
Monday, January 13, 2014 10:42 PM -
My Chrome bookmark points to "social.msdn.microsoft.com/Forums/en-US/vbgeneral/threads"
Oddly, most code blocks are what I am used to (courier new font) but suddenly as I said a few are really odd looking.
Tuesday, January 14, 2014 1:17 AM -
what i want is that nobody can convert the pdf document to other formats like .doc,.txt etc.
for this task what permission i have to set there in setEncryption method. i think its read permission so that user can't access this file for the purpose to convert the document in different format (using 3rd party software).
so i was wondering how to use setEncryption method to give read permission not write permission.
one more question is that the file opens with userpassword then why not set null where it ask for owner password . i don't see the use of ownerpassword ?
could somebody explain on these issues ?
Wednesday, January 15, 2014 7:28 AM -
Hi,
Because this is a 3rd party product, as Devon suggested, you'd better to ask question at its official website for better support.
Thank you for your understanding.
Best Regards,
Franklin
We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey.- Edited by Franklin ChenMicrosoft employee Monday, January 20, 2014 12:32 PM
Monday, January 20, 2014 12:31 PM