如何使用ITextSharp在PDF文件上设置密码而不下载该PDF文件?

时间:2012-02-03 09:10:34

标签: asp.net pdf encryption

我使用ASP.NET创建一个简单的PDF创建者应用程序。该应用程序将动态创建一个PDF文件,其中包含用于保护该文档的密码。这是我的代码:

    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

这是我为PDF文件添加密码的代码:

     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

该代码完美无缺。它可以生成PDF文件并添加一些密码来打开,但PDF文件将发送给用户。有谁知道如何生成一个带有密码的PDF文件,但结果文件仍在服务器上,只能从网页浏览器显示(不显示下载提示)?在此先感谢..:D

1 个答案:

答案 0 :(得分:1)

如果我错了,请纠正我,但我认为由浏览器在浏览器中显示pdf或者询问您是否要下载它。

Adobe Acrobat plug in可以在http://www.adobe.com/下载,是一个插件,可以帮助用户在浏览器中显示pdf。

如何在安装了插件的broswer中显示pdf:http://www.okanagan.bc.ca/administration/itservices/edtech/elearn/Configuring_the_browser_to_show_pdf_files.html