hi
I want to protect a PDF file with a password and re-cancel the password from reading the file in my program.
My entire project depends on a library
iTextSharp
code encrypt with password work perfect
Sub EncryptPdf(ByVal sInFilePath As String, ByVal sOutFilePath As String, ByVal sPassword As String)
Dim oPdfReader As iTextSharp.text.pdf.PdfReader = New iTextSharp.text.pdf.PdfReader(sInFilePath)
Dim oPdfDoc As New iTextSharp.text.Document()
Dim oPdfWriter As PdfWriter = PdfWriter.GetInstance(oPdfDoc, New FileStream(sOutFilePath, FileMode.Create))
oPdfWriter.SetEncryption(PdfWriter.STRENGTH40BITS, sPassword, sPassword, PdfWriter.AllowCopy)
oPdfDoc.Open()
oPdfDoc.SetPageSize(iTextSharp.text.PageSize.LEDGER.Rotate())
Dim oDirectContent As iTextSharp.text.pdf.PdfContentByte = oPdfWriter.DirectContent
Dim iNumberOfPages As Integer = oPdfReader.NumberOfPages
Dim iPage As Integer = 0
Do While (iPage < iNumberOfPages)
iPage += 1
oPdfDoc.SetPageSize(oPdfReader.GetPageSizeWithRotation(iPage))
oPdfDoc.NewPage()
Dim oPdfImportedPage As iTextSharp.text.pdf.PdfImportedPage = oPdfWriter.GetImportedPage(oPdfReader, iPage)
Dim iRotation As Integer = oPdfReader.GetPageRotation(iPage)
If (iRotation = 90) Or (iRotation = 270) Then
oDirectContent.AddTemplate(oPdfImportedPage, 0, -1.0F, 1.0F, 0, 0, oPdfReader.GetPageSizeWithRotation(iPage).Height)
Else
oDirectContent.AddTemplate(oPdfImportedPage, 1.0F, 0, 0, 1.0F, 0, 0)
End If
Loop
oPdfDoc.Close()
End Sub
code decrypt with password not work and give error Byte ??
Dim rafa As RandomAccessFileOrArray = New RandomAccessFileOrArray(txtfilefrom.Text)
If rafa IsNot Nothing Then
Dim objms As MemoryStream = New MemoryStream()
Dim pdfReader As PdfReader = New PdfReader(txtfileto.Text, txtPasswor.Text)//error Byte ??
Dim noofpages As Integer = pdfReader.NumberOfPages
Dim pdfDoc As Document = New Document()
Dim pdfCopy As PdfCopy = New PdfCopy(pdfDoc, objms)
pdfDoc.Open()
Dim i As Integer = 0
While i < noofpages
pdfCopy.AddPage(pdfCopy.GetImportedPage(pdfReader, i + 1))
i += 1
End While
pdfDoc.Close()
End If