Documents for Excel, Java Edition Documentation
Manage File Operations / Export to a PDF File / Support Security Options
In This Topic
    Support Security Options
    In This Topic

    Sometimes, users need to secure their digital documents with user/owner passwords, print permission, content permission, annotation permission etc. PDF documents have always been the preferred format for sharing digital files among professionals. The GcExcel library supports Security Options while saving Excel spreadsheets to PDF files. It helps in securing a PDF Document by restricting the PDF¡¯s access to unauthorized users as per the options specified.

    With GcExcel's PdfSecurityOptions class, you can restrict access to your PDF document, while converting Excel spreadsheet to PDF document. You can choose through the following security properties in the PdfSecurityOptions class:

    Properties

    Description

    getUserPassword / setUserPassword Gets or sets the user password of the PDF document.
    getOwnerPassword / setOwnerPassword Gets or sets the owner password of the PDF document. This password is required to change the permissions for the PDF document.
    getPrintPermission / setPrintPermission Gets or sets the permission to print the PDF document. The default value is true for this property.
    getFullQualityPrintPermission / setFullQualityPrintPermission Gets or sets the permission to print in high quality. The default value is true for this property, and it only works when PrintPermission property is set to true.
    getExtractContentPermission / setExtractContentPermission Gets or sets the permission to copy or extract content. The default value is true for this property.
    getModifyDocumentPermission / setModifyDocumentPermission Gets or sets the permission to modify the PDF document. The default value is true for this property.
    getAssembleDocumentPermission / setAssembleDocumentPermission Gets or sets the permission to insert, rotate or delete pages, and create bookmarks/thumbnail images. The default value is true for this property. If you want to prevent a user from inserting, rotating or deleting pages, you need to set ModifyDocumentPermission property to false as well.
    getModifyAnnotationsPermission / setModifyAnnotationsPermission Gets or sets the permission to modify text annotations and fill the form fields. The default value is true for this property.
    getFillFormsPermission / setFillFormsPermission Gets or sets the permission to fill the form fields even if the ModifyAnnotationsPermission property returns false. The default value for this property is true. Note that if you want to prevent a user from filling interactive form fields, you need to set the ModifyAnnotationsPermission property to false.

    Using Code

    Refer to the following example to add security options while exporting Excel spreadsheets to PDF documents.

    Java
    Copy Code
    // Initialize workbook
    Workbook workbook = new Workbook();
    // Fetch default worksheet
    IWorksheet worksheet = workbook.getWorksheets().get(0);
    Object data = new Object[][] { { "Name", "City", "Sex", "Weight", "Height", "Age" },
            { "Bob", "NewYork", "male", 80, 180, 56 }, { "Betty", "NewYork", "female", 72, 168, 45 },
            { "Gary", "NewYork", "male", 71, 179, 50 }, { "Hunk", "Washington", "male", 80, 171, 59 },
            { "Cherry", "Washington", "female", 58, 161, 34 }, { "Coco", "Virginia", "female", 58, 181, 45 },
            { "Lance", "Chicago", "female", 49, 160, 57 }, { "Eva", "Washington", "female", 71, 180, 81 } };
    
    worksheet.setName("Listing");
    // Set data
    worksheet.getRange("A1:G9").setValue(data);
    
    // The security settings of pdf when converting excel to pdf
    PdfSecurityOptions securityOptions = new PdfSecurityOptions();
    // Sets the user password
    securityOptions.setUserPassword("user");
    // Sets the owner password
    securityOptions.setOwnerPassword("owner");
    // Printing the pdf document is not allowed
    securityOptions.setPrintPermission(false);
    // Filling the form fields of the pdf document is not allowed
    securityOptions.setFillFormsPermission(false);
    
    PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
    // Sets the security settings of the pdf
    pdfSaveOptions.setSecurityOptions(securityOptions);
    
    // Saving workbook to PDF
    workbook.save("4-SavePDFPdfSecurityOptions.pdf", pdfSaveOptions);

    Note: GcExcel uses RC4 encryption with key from 40 to 128 bit length and allows to define additional permission flags.