We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Sign PDF after generation

Hi,

Our application outputs a report as PDF from ASPOSE.WORD. We would like to allow the end user to digitally sign the report with any certificate…

I saw it’s possible to apply certificate by code in ASPOSE but we would like to let the user to apply it’s own certificate.

The PDF is password protected and we dont want the user to be able to modify the “content”.

Here’s the sample code:

Dim pdfSaveOptions As New PdfSaveOptions()
Dim encryptionDetails As New PdfEncryptionDetails(String.Empty, "test", PdfEncryptionAlgorithm.RC4_128)
' Start by disallowing all permissions.
encryptionDetails.Permissions = PdfPermissions.DisallowAll
' Extend permissions to allow editing or modifying annotations.
encryptionDetails.Permissions = PdfPermissions.Printing Or PdfPermissions.HighResolutionPrinting
pdfSaveOptions.EncryptionDetails = encryptionDetails
doc2.Save(ms2, pdfSaveOptions)

I also tried to use “ALLOWALL” enum value but the “Signin” is still not authorized

Is it something I can achieve or I absolutely need to apply a certificate by the code.

Thanks,

JSr

Hi JSr,

Thanks for your inquiry. Yes, you can specify your own certificate while creating digitally signed PDF. Please use the following code snippet for your requirements. You may specifies a digital hash algorithm used by a digital signature. I have used the Md5 (SHA-1 hash algorithm). Please read PdfDigitalSignatureHashAlgorithm enumeration from here:
https://reference.aspose.com/words/net/aspose.words.saving/pdfdigitalsignaturehashalgorithm/

Hope this helps you. Please let us know if you have any more queries.

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.Writeln("Test Signed PDF.");
PdfSaveOptions saveOptions = new PdfSaveOptions();
// Create encryption details and set owner password.
PdfEncryptionDetails encryptionDetails = new PdfEncryptionDetails(string.Empty, "password", PdfEncryptionAlgorithm.RC4_128);
// Start by disallowing all permissions.
encryptionDetails.Permissions = PdfPermissions.DisallowAll;
// Extend permissions to allow editing or modifying annotations.
encryptionDetails.Permissions = PdfPermissions.Printing | PdfPermissions.HighResolutionPrinting;
saveOptions.EncryptionDetails = encryptionDetails;
// Load the certificate from disk.
// The other constructor overloads can be used to load certificates from different locations.
X509Certificate2 cert = new X509Certificate2(MyDir + "TestCert.pfx", "1111");
// Pass the certificate and details to the save options class to sign with.
PdfDigitalSignatureDetails signature = new PdfDigitalSignatureDetails(
    cert,
    "Test Signing",
    "Aspose Office",
    DateTime.Now);
saveOptions.DigitalSignatureDetails = signature;
signature.HashAlgorithm = PdfDigitalSignatureHashAlgorithm.Md5;
// Render the document to PDF format with the specified permissions.
doc.Save(MyDir + "Out.pdf", saveOptions);

Hi,

Sorry for the late feedback!

Thanks for the code snippet but my initial question was, Is there any way I can protect my PDF with password to allow printing / saving but disallow end user to modify the generated PDF, BUT, allow the user to apply a certificate with, for example, Adobe Acrobat or another program.

So is there a way I could achieve this.

-----------------------

Another question is, in the code snippet you’ve provided, you are using a pfx file for the signature. The client’s certificate is a .EPF file. Do you think it will work?

Thanks!

Best regards,

JSr

Hi JSr,

Thanks for your inquiry. The PdfEncryptionDetails class contains details for encrypting and access permissions for a PDF document. You can specify the owner password for the encrypted PDF document by using PdfEncryptionDetails.OwnerPassword property. The owner password allows the user to open an encrypted PDF document without any access restrictions specified in Permissions. The owner password cannot be the same as the user password. Please read the Pdf Permissions from here:

https://reference.aspose.com/words/net/aspose.words.saving/pdfpermissions/

Please use the following code snippet to protect document with password , allow print option and disallow content modification.

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.Writeln("Test Signed PDF.");
PdfSaveOptions saveOptions = new PdfSaveOptions();
// Create encryption details and set owner password.
PdfEncryptionDetails encryptionDetails = new PdfEncryptionDetails("", "ownerpassword", PdfEncryptionAlgorithm.RC4_128);
// Start by disallowing all permissions.
encryptionDetails.Permissions = PdfPermissions.DisallowAll;
// Extend permissions to allow editing or modifying annotations.
encryptionDetails.Permissions = PdfPermissions.Printing;
saveOptions.EncryptionDetails = encryptionDetails;
// Render the document to PDF format with the specified permissions.
doc.Save(MyDir + "SpecifyPermissions Out.pdf", saveOptions);
//Use Aspose.Pdf to set PDF security. 
Aspose.Pdf.Facades.DocumentPrivilege privilege = Aspose.Pdf.Facades.DocumentPrivilege.ModifyContents;
privilege.ChangeAllowLevel = 1;
privilege.AllowPrint = true;
privilege.AllowCopy = true;
//open PDF document
Aspose.Pdf.Facades.PdfFileSecurity fileSecurity = new Aspose.Pdf.Facades.PdfFileSecurity();
fileSecurity.BindPdf(MyDir + "SpecifyPermissions Out.pdf");
//decrypt PDF document
fileSecurity.DecryptFile("ownerpassword");
//set document privileges
fileSecurity.SetPrivilege(privilege);
fileSecurity.Save(MyDir + "Out.pdf");

PdfDigitalSignatureDetails.Certificate property gets/sets X509Certificate2 certificate to sign with.

Moreover, you may use our product Aspose.Pdf to work with Pdf documents and may post your queries related to PDF document atAspose.Pdf forum. Please read following documentation links for your kind reference.

Decrypt PDF File using Owner Password (Facades)
Encrypt PDF File using Different Encryption Types and Algorithms (Facades)

Hi!

Thanks for the code… We use Aspose Words because we allow certain users to generate their report in MS-WORD format. Other users are limited to PDF and we want to make sure the user cannot modify it’s content…

We’ve noticed another thing: I can open the generated PDF file with Adobe Reader, click on “print”, select “Microsoft XPS Document Writer” and generate a .xps file. After that, open the .xps and select print and then select “Pdf Creator” to generate a PDF.

The resulting PDF is totally unsecured.

I can also open a password secured PDF with FoxitReader, click on print, directly select “CutePDF Writer” or “Pdf Creator” to generate a completely unsecured PDF document…

Is there a way to avoid this (use another security than password)?

Note that we must allow users to “print”.

Thanks,

JSR

Hi JSr,

Thanks for your inquiry. In case you are using an older version of Aspose.Words, I would suggest you please upgrade to the latest version (v13.1.0) from here and let us know how it goes on your side.

jeezypop:

We’ve noticed another thing: I can open the generated PDF file with Adobe Reader, click on “print”, select “Microsoft XPS Document Writer” and generate a .xps file. After that, open the .xps and select print and then select “Pdf Creator” to generate a PDF.

click on print, directly select “CutePDF Writer” or “Pdf Creator” to generate a completely unsecured PDF document…

In this case, please use only Aspose.Words to generate Pdf file as shown in following code snippet. The text of output Pdf file generated with Cute Pdf Write is not editable. However, the Pdf file is split into multiple images. I have attached the sample output Pdf file with this post for your kind reference.

jeezypop:

I can also open a password secured PDF with FoxitReader

I am checking this scenario and will update you asap.

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.Writeln("Test Signed PDF.");
PdfSaveOptions saveOptions = new PdfSaveOptions();
// Create encryption details and set owner password.
PdfEncryptionDetails encryptionDetails = new PdfEncryptionDetails("userpassword", "ownerpassword", PdfEncryptionAlgorithm.RC4_128);
// Start by disallowing all permissions.
encryptionDetails.Permissions = PdfPermissions.DisallowAll;
// Extend permissions to allow editing or modifying annotations.
encryptionDetails.Permissions = PdfPermissions.Printing;
saveOptions.EncryptionDetails = encryptionDetails;
// Render the document to PDF format with the specified permissions.
doc.Save(MyDir + "SpecifyPermissions Out.pdf", saveOptions);

Hi JSr,

Thanks for your patience.

jeezypop:

I can also open a password secured PDF with FoxitReader

I have downloaded the Foxit Reader from here:
http://www.foxitsoftware.com/downloads/

I have tested the scenario and have not found the shared issue while using latest version of Aspose.Words for .NET. I would suggest you please upgrade to the latest version (v13.2.0) from here:
https://downloads.aspose.com/words/net