NEWS

[DEVEXPRESS] Đặt mật khẩu và bỏ mật khẩu tập tin file PDF

[DEVEXPRESS] Đặt mật khẩu và bỏ mật khẩu tập tin file PDF
Đăng bởi: Thảo Meo - Lượt xem: 3423 09:14:59, 12/04/2021C#   In bài viết

Xin chào các bạn, bài viết hôm nay mình sẻ tiếp tục hướng dẫn các bạn cách đặt mật khẩu và bỏ mật khẩu tập tin file PDF trong lập trình C#, Winform.

[DEVEXPRESS] Create and Remove Password PDF File C# Winform

Hôm trước, có Anh hỏi mình làm cách nào để đặt mật khẩu cho tài liệu PDF, và để muốn đọc được tập tin PDF này.

Thì mình sẽ viết một ứng dụng nhỏ để đọc tài liệu mà mình đã truyền sẵn mật khẩu vào.

Nhằm mục đích bảo mật, tránh người dùng copy và in.

Tuy nhiên, Anh em nào chịu khó mà chụp màn hình file PDF thành hình ảnh thì cũng thua  :)

Dưới đây là giao diện demo ứng dụng của mình:

encrypt_pdf_file

Trong Devexpress, đã cung cấp cho chúng ta sẵn cách mã hóa và giải mã với tập tin PDF.

Để sử dụng, các bạn cần import thư viện vào:

using DevExpress.Pdf;

1. Đặt mật khẩu cho File PDF 

private void btnEnctypt_Click(object sender, EventArgs e)
{
    var dlg = new OpenFileDialog();
    if(dlg.ShowDialog() == DialogResult.OK)
    {
        using (PdfDocumentProcessor pdfDocumentProcessor = new PdfDocumentProcessor())
        {
          
            pdfDocumentProcessor.LoadDocument(dlg.FileName);
            PdfEncryptionOptions encryptionOptions = new PdfEncryptionOptions();
            encryptionOptions.PrintingPermissions = PdfDocumentPrintingPermissions.Allowed;
            encryptionOptions.DataExtractionPermissions = PdfDocumentDataExtractionPermissions.NotAllowed;
            encryptionOptions.ModificationPermissions = PdfDocumentModificationPermissions.DocumentAssembling;
            encryptionOptions.InteractivityPermissions = PdfDocumentInteractivityPermissions.Allowed;                  
            encryptionOptions.OwnerPasswordString = txtPassword.Text;
            encryptionOptions.UserPasswordString = txtPassword.Text;
            encryptionOptions.Algorithm = PdfEncryptionAlgorithm.AES256;
            pdfDocumentProcessor.SaveDocument("ProtectedDocument.pdf", new PdfSaveOptions() { EncryptionOptions = encryptionOptions });
        }
    }
}

2. Hủy bỏ mật khẩu cho tập tin File PDF

private void btnDecrypt_Click(object sender, EventArgs e)
{
    var dlg = new OpenFileDialog();
    if (dlg.ShowDialog() == DialogResult.OK)
    {
        using (PdfDocumentProcessor pdfDocumentProcessor = new PdfDocumentProcessor())
        {
            pdfDocumentProcessor.PasswordRequested += PdfDocumentProcessor_PasswordRequested;
            pdfDocumentProcessor.LoadDocument(dlg.FileName);
            pdfDocumentProcessor.PasswordRequested -= PdfDocumentProcessor_PasswordRequested;
            pdfDocumentProcessor.SaveDocument("UnProtectedDocument.pdf");
            
        }
    }
}

Để đọc được file tập tin PDF đã mã hóa, các bạn cần Trigger Event PasswordRequested vào, để truyền mật khẩu vào cho PdfDocumentProcessor giải mã.

private void PdfDocumentProcessor_PasswordRequested(object sender, PdfPasswordRequestedEventArgs e)
{
    e.PasswordString = txtPassword.Text;
}

Thanks for watching!

DOWNLOAD SOURCE

THÔNG TIN TÁC GIẢ

BÀI VIẾT LIÊN QUAN

[DEVEXPRESS] Đặt mật khẩu và bỏ mật khẩu tập tin file PDF
Đăng bởi: Thảo Meo - Lượt xem: 3423 09:14:59, 12/04/2021C#   In bài viết

CÁC BÀI CÙNG CHỦ ĐỀ

Đọc tiếp
.