Search code examples
vb.netpdfms-wordpasswordsoffice-interop

How do i set password in my pdf before sending it?


I'm trying to add a password in pdf before sending it from respective owners for security,

This is the code of saving my file into word to pdf. this is where i want to put a password for the pdf, I can't see any reference having a password in their pdf file before sending it.

filepath = "C:\Users\" & Environ("Username") & "\Desktop\WindowsApplication2\Payslips\Payslip " & item.SubItems.Item(1).Text & " " & Format(dtpPayoutDate.Value, "MM-dd-yyyy") & ".pdf"
            oDoc.SaveAs(filepath, Word.WdSaveFormat.wdFormatPDF) 
            'oDoc.Password="trypw" This one did'nt work
            'oDoc.WritePassword = "trypw" This also did'nt work

And this one is sending the file from their outlook or email

  Dim OutlookMessage As outlook.MailItem
                Dim AppOutlook As New outlook.Application
                Dim filereader1 As String = My.Computer.FileSystem.ReadAllText("C:\Users\" & Environ("Username") & "\Desktop\WindowsApplication2\email template.txt")

                    OutlookMessage = AppOutlook.CreateItem(outlook.OlItemType.olMailItem)
                    Dim Recipents As outlook.Recipients = OutlookMessage.Recipients
                    Recipents.Add(item.SubItems.Item(21).Text)
                    OutlookMessage.Subject = "Payslip " & Format(dtpPayoutDate.Value, "MM-dd-yyyy")
                    OutlookMessage.Body = filereader1
                    OutlookMessage.BodyFormat = outlook.OlBodyFormat.olFormatHTML
                    OutlookMessage.Attachments.Add(filepath)
                    OutlookMessage.Send()

Edit : I download PDFsharp in my NuGet then put this code after sending it.

 Dim document As PdfDocument = PdfReader.Open(filepath)
            Dim securitySettings As PdfSecuritySettings = document.SecuritySettings
            securitySettings.UserPassword = "user"
            securitySettings.OwnerPassword = "owner"
            securitySettings.PermitAccessibilityExtractContent = False
            securitySettings.PermitAnnotations = False
            securitySettings.PermitAssembleDocument = False
            securitySettings.PermitExtractContent = False
            securitySettings.PermitFormsFill = True
            securitySettings.PermitFullQualityPrint = False
            securitySettings.PermitModifyDocument = True
            securitySettings.PermitPrint = False

            document.Save(filepath)

Thanks @Eugene Astafiev for giving some reference


Solution

  • You can save a document using the PDF file format and then use the PDFSharp library for setting up a password:

    // Open an existing document. Providing an unrequired password is ignored.
    PdfDocument document = PdfReader.Open(filename, "some text");
    
    PdfSecuritySettings securitySettings = document.SecuritySettings;
    
    // Setting one of the passwords automatically sets the security level to 
    // PdfDocumentSecurityLevel.Encrypted128Bit.
    securitySettings.UserPassword  = "user";
    securitySettings.OwnerPassword = "owner";
    
    // Don't use 40 bit encryption unless needed for compatibility reasons
    //securitySettings.DocumentSecurityLevel = PdfDocumentSecurityLevel.Encrypted40Bit;
    
    // Restrict some rights.
    securitySettings.PermitAccessibilityExtractContent = false;
    securitySettings.PermitAnnotations = false;
    securitySettings.PermitAssembleDocument = false;
    securitySettings.PermitExtractContent = false;
    securitySettings.PermitFormsFill = true;
    securitySettings.PermitFullQualityPrint = false;
    securitySettings.PermitModifyDocument = true;
    securitySettings.PermitPrint = false;
    
    // Save the document...
    document.Save(filename);
    

    See PDFsharp Sample: Protect Document for more information.