Click here to Skip to main content
15,888,802 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Here is the following code which used for converting excel to PDF.I need a solution for PDF to be converted as password protected below code is used for converting the excel to PDF without password.




Dim excelApplication As ApplicationClass = New ApplicationClass()
  Dim excelWorkbook As Workbook = Nothing
  Dim paramSourceBookPath As String = "C:\test\DDB_TEST.xlsx"
  Dim paramExportFilePath As String = "C:\test\Test.pdf"
  Dim paramExportFormat As XlFixedFormatType = _
      XlFixedFormatType.xlTypePDF
  Dim paramExportQuality As XlFixedFormatQuality = _
      XlFixedFormatQuality.xlQualityStandard
  Dim paramOpenAfterPublish As Boolean = False
  Dim paramIncludeDocProps As Boolean = True
  Dim paramIgnorePrintAreas As Boolean = True
  Dim paramFromPage As Object = Type.Missing
  Dim paramToPage As Object = Type.Missing
  Dim executableLocation As String = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)
  Dim xslLocation As String = Path.Combine(executableLocation, "DDL.pdf")
  Dim xslLocation1 As String = Path.Combine(executableLocation, "DDL1.pdf")
  Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
      Try
          ' Open the source workbook.
          excelWorkbook = excelApplication.Workbooks.Open(paramSourceBookPath)
          ' Save it in the target format.
          If Not excelWorkbook Is Nothing Then
              excelWorkbook.ExportAsFixedFormat(paramExportFormat, _
                  xslLocation, paramExportQuality, _
                  paramIncludeDocProps, paramIgnorePrintAreas, _
                  paramFromPage, paramToPage, paramOpenAfterPublish)
              Dim picArray As Byte() = System.IO.File.ReadAllBytes(paramExportFilePath)
              '   System.IO.File.WriteAllBytes("hello.pdf", picArray)
          End If
      Catch ex As Exception
          ' Respond to the error.
      Finally
          ' Close the workbook object.
          If Not excelWorkbook Is Nothing Then
              excelWorkbook.Close(False)
              excelWorkbook = Nothing
          End If
          ' Quit Excel and release the ApplicationClass object.
          If Not excelApplication Is Nothing Then
              excelApplication.Quit()
              excelApplication = Nothing
          End If
          GC.Collect()
          GC.WaitForPendingFinalizers()
          GC.Collect()
          GC.WaitForPendingFinalizers()
         // EncryptPdf(xslLocation, xslLocation1, "text")
      End Try
  End Sub


What I have tried:

i tried with microsoft.interop.excel for converting excel to pdF without password protected(secured).
Posted
Updated 9-Oct-17 0:02am
v9
Comments
Richard MacCutchan 7-Oct-17 10:51am    
And? What is the question?
Richard MacCutchan 7-Oct-17 13:55pm    
Really?
Steve44 7-Oct-17 13:56pm    
To receive an informed answer, could you please add which methods of the Excel interop you where calling with which parameters and what error you received?

Unfortunately, most people here can't read minds (including me), so you will have to provide the details in your question :-)
RollUpBob 17-Oct-17 1:36am    
Excel Interop does not support encrypted PDFs, so I think you have two options:

1. Create PDF with VB.NET by using that ExportAsFixedFormat and after that encrypt it, for instance like shown here (it's a C# example, but hopefully you can translate it to VB.NET with ease).

2. Convert Excel file to PDF in C# / VB.NET like the following:

Dim workbook As ExcelFile = ExcelFile.Load("C:\test\DDB_TEST.xlsx")

Dim options As New PdfSaveOptions()
options.DocumentOpenPassword = "password"

workbook.Save("C:\test\DDB_TEST.pdf", options)

The code uses an API from this Excel library for VB.NET.
Sivachandran R 17-Oct-17 1:52am    
Thanks RollupBob

1 solution

 
Share this answer
 
Comments
Sivachandran R 9-Oct-17 6:07am    
So is there any way to add password to pdf with other option?
Richard MacCutchan 9-Oct-17 6:09am    
No idea. Go to the Adobe website and get a copy of the PDF standard to see how you can add it.
Sivachandran R 9-Oct-17 6:28am    
Thanks Richard

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900