Proteggi i documenti PDF con JavaScript via C++
Contents
[
Hide
]
A volte, gli sviluppatori devono lavorare con file PDF criptati. Ad esempio:
- Proteggere i documenti con password per proprietario e utente in modo che non possa aprirlo chiunque.
- Impostare restrizioni o autorizzazioni al documento dopo l’apertura del documento, ad esempio limitare se È possibile stampare o estrarre il contenuto del documento.
Questo articolo spiega come passare le opzioni di sicurezza PDF durante il salvataggio dei fogli di calcolo in PDF.
Aspose.Cells fornisce PdfSecurityOptions per lavorare sulla sicurezza. Puoi impostare password proprietarie e utente durante il salvataggio in PDF. La password proprietaria o la password utente sarà richiesta per aprire il documento PDF criptato per la visualizzazione.
- La password utente può essere nulla o una stringa vuota; in questo caso, non verrà richiesta password dall’utente all’apertura del documento PDF.
- Aprire il documento PDF con la corretta password proprietaria consente l’accesso completo (senza restrizioni di accesso specificate) al documento.
- L’apertura del documento PDF con la corretta password dell’utente (o l’apertura di un documento che non ha una password utente) consente l’accesso limitato come le autorizzazioni specificate.
Il codice di esempio qui sotto descrive come proteggere i PDF con Aspose.Cells.
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Secure PDF Example</title>
</head>
<body>
<h1>Secure PDF Example</h1>
<input type="file" id="fileInput" accept=".xls,.xlsx" />
<button id="runExample">Run Example</button>
<a id="downloadLink" style="display: none;">Download Result</a>
<div id="result"></div>
</body>
<script src="aspose.cells.js.min.js"></script>
<script type="text/javascript">
const { Workbook, SaveFormat, PdfSaveOptions, PdfSecurityOptions, Utils } = AsposeCells;
AsposeCells.onReady({
license: "/lic/aspose.cells.enc",
fontPath: "/fonts/",
fontList: [
"arial.ttf",
"NotoSansSC-Regular.ttf"
]
}).then(() => {
console.log("Aspose.Cells initialized");
});
document.getElementById('runExample').addEventListener('click', async () => {
const fileInput = document.getElementById('fileInput');
if (!fileInput.files.length) {
document.getElementById('result').innerHTML = '<p style="color: red;">Please select an Excel file.</p>';
return;
}
const file = fileInput.files[0];
const arrayBuffer = await file.arrayBuffer();
// Instantiating a Workbook object from the uploaded file
const workbook = new Workbook(new Uint8Array(arrayBuffer));
// Create PDF save options and security options
const saveOption = new PdfSaveOptions();
saveOption.securityOptions = new PdfSecurityOptions();
// Set security options (converted from getters/setters to properties)
saveOption.securityOptions.userPassword = "user";
saveOption.securityOptions.ownerPassword = "owner";
saveOption.securityOptions.extractContentPermission = false;
saveOption.securityOptions.printPermission = false;
// Save the workbook to PDF with security options
const outputData = workbook.save(SaveFormat.Pdf, saveOption);
const blob = new Blob([outputData], { type: "application/pdf" });
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'securepdf_test.out.pdf';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Secure PDF';
document.getElementById('result').innerHTML = '<p style="color: green;">PDF generated successfully! Click the download link to get the secured PDF file.</p>';
});
</script>
</html>
Se il foglio di calcolo contiene formule, è meglio chiamare Workbook.calculateFormula() proprio prima di renderlo in PDF. Ciò garantisce che i valori dipendenti dalle formule vengano ricalcolati e i valori corretti siano resi nel PDF.