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

发布于 2025-01-01 14:57:22 字数 2745 浏览 0 评论 0原文

我使用 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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

哎呦我呸! 2025-01-08 14:57:22

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

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

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文