2012-02-03 6 views
0

나는 ASP.NET을 사용하여 간단한 PDF 작성자 응용 프로그램을 만듭니다. 해당 응용 프로그램은 해당 문서의 보안을 위해 암호와 함께 즉석에서 PDF 파일을 만듭니다. 여기PDF 파일을 다운로드하지 않고 ITextSharp를 사용하여 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

내가 틀렸다면 정정 해주세요. 그러나 브라우저에 pdf를 표시하거나 다운로드 할 것인지 묻는 것은 브라우저의 책임이라고 생각합니다.

Adobe Acrobat plug inhttp://www.adobe.com/에서 다운로드 할 수 있으며 브라우저에서 PDF를 표시하는 데 도움이되는 플러그인입니다.

설치된 플러그인으로 broswer에 pdf를 표시하는 방법 : http://www.okanagan.bc.ca/administration/itservices/edtech/elearn/Configuring_the_browser_to_show_pdf_files.html

관련 문제