Michael Frans
Michael Frans

Reputation: 623

how to set password on PDF file using ITextSharp without downloading that PDF File?

i create a simple PDF creator application using ASP.NET. that application will create a PDF file on the fly with the password for securing that document. here's my code:

    Sub createPDFFile()
        Dim doc As Document = New Document
        PdfWriter.GetInstance(doc, New FileStream(Request.PhysicalApplicationPath + _
                              "pdf\result.pdf", FileMode.Create))
        doc.Open()
        doc.Add(New Paragraph("Hello World!! only Testing"))
        doc.Close()
        SetPDFPassword(Server.MapPath("~/pdf/result.pdf"), "resultwithpassword.pdf", "12345")
        Response.Redirect("pdf/1.pdf")
    End Sub

and here's my code for add a password to the PDF file:

     Private Sub SetPDFPassword(ByVal FullPathPdfFileName As String, ByVal DownloadPDFFileName As String, ByVal ForOpenPassword As String)
        Dim sname As String = FullPathPdfFileName
        Dim sname1 As String = New System.IO.FileInfo(FullPathPdfFileName).DirectoryName & "/test.pdf"
        Dim reader As New PdfReader(sname)
        Dim n As Integer = reader.NumberOfPages

        Dim document As New Document(reader.GetPageSizeWithRotation(1))
        Dim writer As PdfWriter = PdfWriter.GetInstance(document, New IO.FileStream(sname1, IO.FileMode.Create))
        writer.SetEncryption(PdfWriter.STRENGTH128BITS, ForOpenPassword, Nothing, PdfWriter.AllowPrinting)
        document.Open()
        Dim cb As PdfContentByte = writer.DirectContent
        Dim page As PdfImportedPage
        Dim rotation As Integer
        Dim i As Integer = 0

        While i < n
            i += 1
            document.SetPageSize(reader.GetPageSizeWithRotation(i))
            document.NewPage()
            page = writer.GetImportedPage(reader, i)
            rotation = reader.GetPageRotation(i)
            If rotation = 90 OrElse rotation = 270 Then
                cb.AddTemplate(page, 0, -1.0F, 1.0F, 0, 0, _
                reader.GetPageSizeWithRotation(i).Height)
            Else
                cb.AddTemplate(page, 1.0F, 0, 0, 1.0F, 0, _
                0)

            End If
        End While

        document.Close()
        writer.Close()

        Dim PDFfile As New IO.FileStream(sname1, IO.FileMode.Open)
        Dim FileSize As Long
        FileSize = PDFfile.Length
        Dim buffer As Byte() = New Byte(CInt(FileSize) - 1) {}
        PDFfile.Read(buffer, 0, CInt(FileSize))
        PDFfile.Close()
        System.IO.File.Delete(sname1)
        Response.AddHeader("Content-Disposition", "attachment;filename=" & DownloadPDFFileName)
        Response.ContentType = "application/pdf"
        Response.BinaryWrite(buffer)
        Response.Flush()
        Response.Close()

    End Sub

that code is works perfectly. it can generate a PDF file and add some password to open, but the PDF file will be send to the user. has anyone know how to generate a PDF file with password but that result file still on the server and only can be shown from web browser (not showing a download prompt)?? thanks in advance.. :D

Upvotes: 0

Views: 2261

Answers (1)

Niklas
Niklas

Reputation: 13155

Correct me if I'm wrong but I think that it is up to the browser to show the pdf in the browser or ask you if you want to download it.

Adobe Acrobat plug in can be downloaded at http://www.adobe.com/ and is a plug in to help the user display the pdf in the browser.

How to display the pdf in the broswer with the plugin installed: http://www.okanagan.bc.ca/administration/itservices/edtech/elearn/Configuring_the_browser_to_show_pdf_files.html

Upvotes: 1

Related Questions