PDF에 권한 설정

기존 PDF 파일에 권한 설정

PDF 파일의 권한을 설정하려면 PdfFileSecurity 객체를 생성하고 SetPrivilege 메서드를 호출합니다. DocumentPrivilege 객체를 사용하여 권한을 지정한 다음 이 객체를 SetPrivilege 메서드에 전달할 수 있습니다. 다음 코드 조각은 PDF 파일의 권한을 설정하는 방법을 보여줍니다.

// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void SetPrivilege1()
{
    // The path to the documents directory
    var dataDir = RunExamples.GetDataDir_AsposePdf_SecuritySignatures();
    
    // Create DocumentPrivileges object and set needed privileges
    var privilege = Aspose.Pdf.Facades.DocumentPrivilege.ForbidAll;
    privilege.ChangeAllowLevel = 1;
    privilege.AllowPrint = true;
    privilege.AllowCopy = true;

    using (var fileSecurity = new Aspose.Pdf.Facades.PdfFileSecurity())
    {
        // Bind PDF document
        fileSecurity.BindPdf(dataDir + "sample.pdf");
        // Set privilege
        fileSecurity.SetPrivilege(privilege);
        // Save PDF document
        fileSecurity.Save(dataDir + "SamplePrivileges_out.pdf");
    }
}

비밀번호를 지정하는 다음 메서드를 참조하세요:

// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void SetPrivilegeWithPassword()
{
    // The path to the documents directory
    var dataDir = RunExamples.GetDataDir_AsposePdf_SecuritySignatures();
    
    // Create DocumentPrivileges object and set needed privileges
    var privilege = Aspose.Pdf.Facades.DocumentPrivilege.ForbidAll;
    privilege.ChangeAllowLevel = 1;
    privilege.AllowPrint = true;
    privilege.AllowCopy = true;

    using (var fileSecurity = new Aspose.Pdf.Facades.PdfFileSecurity())
    {
        // Bind PDF document
        fileSecurity.BindPdf(dataDir + "sample.pdf");
        // Set privilege and passwords
        fileSecurity.SetPrivilege(string.Empty, "P@ssw0rd", privilege);
        // Save PDF document
        fileSecurity.Save(dataDir + "SamplePrivilegesPassword_out.pdf");
    }
}

PDF에서 확장된 권한 기능 제거

PDF 문서는 사용자가 Adobe Acrobat Reader를 사용하여 양식 필드에 데이터를 입력하고 채워진 양식의 복사본을 저장할 수 있도록 하는 확장된 권한 기능을 지원합니다. 그러나 이는 PDF 파일이 수정되지 않도록 보장하며, PDF 구조에 대한 수정이 이루어지면 확장된 권한 기능이 손실됩니다. 이러한 문서를 볼 때 문서가 수정되었기 때문에 확장된 권한이 제거되었다는 오류 메시지가 표시됩니다. 최근에 PDF 문서에서 확장된 권한을 제거하라는 요구가 있었습니다.

PDF 파일에서 확장된 권한을 제거하려면 PdfFileSignature 클래스에 RemoveUsageRights()라는 새로운 메서드가 추가되었습니다. 소스 PDF에 확장된 권한이 포함되어 있는지 확인하기 위해 ContainsUsageRights()라는 또 다른 메서드가 추가되었습니다.

다음 코드는 문서에서 사용 권한을 제거하는 방법을 보여줍니다:

// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void RemoveExtendedRights()
{
    // The path to the documents directory
    var dataDir = RunExamples.GetDataDir_AsposePdfFacades_SecuritySignatures();
    
    using (var pdfSign = new Aspose.Pdf.Facades.PdfFileSignature())
    {
        // Bind PDF document
        pdfSign.BindPdf(dataDir + "DigitallySign.pdf");
        if (pdfSign.ContainsUsageRights())
        {
            pdfSign.RemoveUsageRights();
        }
        // Save PDF document
        pdfSign.Document.Save(dataDir + "RemoveRights_out.pdf");
    }
}