BCL easyPDF SDK
easyPDF SDK Usermanual
PDF Creator Programming API  |  Download Free Trial  |  Contact Us to Purchase

CloseMemEncrypt Method

Encrypts the PDF and closes the handle to it. All write operations to the PDF are finalized and all internal memory structures are released. Use this method if you want your output PDF to have encryption-based security.

Function CloseMemEncrypt(UserPassword As String,
                         OwnerPassword As String,
                         Encryption As prcSecEncryption,
                         AnnotationPerm As prcSecAnnotationPerm,
                         ExtractionPerm As prcSecExtractionPerm,
                         ModificationPerm As prcSecModificationPerm,
                         PrintingPerm As prcSecPrintingPerm) As Variant

byte[] CloseMemEncrypt(string UserPassword,
                       string OwnerPassword,
                       prcSecEncryption Encryption,
                       prcSecAnnotationPerm AnnotationPerm,
                       prcSecExtractionPerm ExtractionPerm,
                       prcSecModificationPerm ModificationPerm,
                       prcSecPrintingPerm PrintingPerm)

byte[] CloseMemEncrypt(String UserPassword,
                       String OwnerPassword,
                       prcSecEncryption Encryption,
                       prcSecAnnotationPerm AnnotationPerm,
                       prcSecExtractionPerm ExtractionPerm,
                       prcSecModificationPerm ModificationPerm,
                       prcSecPrintingPerm PrintingPerm) throws PDFProcessorException

Parameters

Return Values

If the file handle is open and was obtained by calling OpenMem, the encrypted output PDF is returned.

Otherwise, if the file handle was obtained by calling OpenFile, or has already been previously closed, an empty array of size zero is returned.

While CloseMemEncrypt will always properly close the file handle regardless of how the handle was originally obtained, suggested practice is to call CloseMemEncrypt when the file handle was retrieved using OpenMem and modifications have been made to the PDF. This way you get the encrypted modified PDF stream back.

In all other cases, such as when the file handle was retrieved using OpenFile or the PDF was only queried but not modified, calling CloseEncrypt will suffice.

Remarks

Note 1:

If you want an encrypted output PDF, be sure to call CloseEncrypt or CloseMemEncrypt when you are finished modifying your PDF. If you do not close the file handle, the PDF will not be fully written out and is considered invalid. Additionally, memory used internally by the file handle will not be deallocated; in some languages and frameworks this will manifest as a memory leak, and in others it could result in unpredictable garbage collection since EasyPDF SDK is unmanaged code. Always be sure to call CloseEncrypt or CloseMemEncrypt when you want to write out an encrypted PDF.

Once the file handle is closed, no further calls on any of the handle's methods are allowed, except that Close, CloseMem, CloseEncrypt, CloseMemEncrypt, CloseDecrypt, or CloseMemDecrypt may be called repeatedly. Thus, if you are using the try-catch-finally idiom, you may safely call Close or its variants in the finally block regardless of earlier handle closes.

Note, however, that only the first call to Close, CloseMem, CloseEncrypt, CloseMemEncrypt, CloseDecrypt, or CloseMemDecrypt actually closes the file handle, and it does so regardless of success or failure. If the first call to Close or its variants fails for some reason, then the file handle is closed, its internal memory is freed, and an exception is thrown. Any call to Close or its variants other than the first call will be ignored (it will not do anything but will not produce an error either).

Note 2:

If your original input PDF was already encrypted, then call CloseEncrypt or CloseMemEncrypt if you want to change the security settings (for example password and permissions) for your output PDF. If you do not want to change the security settings for your output PDF, then call Close or CloseMem instead to retain the original security settings.

Note 3:

If you enable any security feature, be sure to set the owner password or the user password. If you set both passwords, they should be different.

Also bear in mind that if the user password is set but no owner password is specified, the permission flags are ignored and full permissions are granted for all operations. In other words, the permission flags are only respected when there is an owner password.

The security features can be combined in any way, but be aware that out of the possible combinations, only five are offered by Adobe Acrobat. The five configurations available in Acrobat (when using 128-bit encryption) can be achieved as follows:

Acrobat "Changes Allowed" easyPDF SDK Flag Combination
None

PRN_SEC_MODIFY_PERM_NONE | PRN_SEC_ANNOT_PERM_NONE

Inserting, deleting, and rotating pages

PRN_SEC_MODIFY_PERM_ASSEMBLY | PRN_SEC_ANNOT_PERM_NONE

Filling in form fields and signing existing signature fields
PRN_SEC_MODIFY_PERM_NONE | PRN_SEC_ANNOT_PERM_FORM
Commenting, filling in form fields, and signing existing signature fields PRN_SEC_MODIFY_PERM_NONE | PRN_SEC_ANNOT_PERM_FULL
Any except extracting pages
PRN_SEC_MODIFY_PERM_FULL | PRN_SEC_ANNOT_PERM_FULL

Example Usage in VB.NET

Dim inMem() As Byte = File.ReadAllBytes("C:\test\input.pdf")
Dim oProcessor As PDFProcessor = Nothing
Dim oProcessorHandle As PDFProcessorHandle = Nothing
Try
   oProcessor = New PDFProcessor
   oProcessorHandle = oProcessor.OpenMem(inMem, "ownerPassword")
   oProcessorHandle.AddHyperlink(0, _
                                 0, _
                                 50, _
                                 80, _
                                 370, _
                                 100, _
                                 "http://www.bcltechnologies.com", _
                                 prcAnnotBorderStyle.PRC_BORDERSTYLE_DASHLINE_MEDIUM, _
                                 0)
   oProcessorHandle.AddStamp("C:\test\image.jpg", _
                             0, _
                             prcStampHPosition.PRC_STAMP_HPOS_CENTER, _
                             prcStampVPosition.PRC_STAMP_VPOS_CENTER, _
                             prcStampZOrder.PRC_STAMP_ZORDER_TOP, _
                             0, _
                             0, _
                             0, _
                             100)
      Dim outMem() As Byte = oProcessorHandle.CloseMemEncrypt("new_user_password", _
"new_owner_password", _
prcSecEncryption.PRC_SEC_ENCRYPT_128BITS, _
prcSecAnnotationPerm.PRC_SEC_ANNOT_PERM_NONE, _
prcSecExtractionPerm.PRC_SEC_EXTR_PERM_NONE, _
prcSecModificationPerm.PRC_SEC_MODIFY_PERM_NONE, _
prcSecPrintingPerm.PRC_SEC_PRINT_PERM_NONE)
   File.WriteAllBytes("C:\test\output.pdf", outMem)
Catch ex As System.Runtime.InteropServices.COMException
   MessageBox.Show(ex.Message)
Finally
   oProcessorHandle.Close()
End Try

Example Usage in C#

byte[] inMem = File.ReadAllBytes(@"C:\test\input.pdf");
PDFProcessor oProcessor = null;
PDFProcessorHandle oProcessorHandle = null;
try
{
   oProcessor = new PDFProcessor();
   oProcessorHandle = oProcessor.OpenMem(inMem, "ownerPassword");
   oProcessorHandle.AddHyperlink(0,
                                 0, 
                                 50, 
                                 80, 
                                 370, 
                                 100, 
                                 "http://www.bcltechnologies.com", 
                                 prcAnnotBorderStyle.PRC_BORDERSTYLE_DASHLINE_MEDIUM, 
                                 0);
   oProcessorHandle.AddStamp(@"C:\test\image.jpg", 
                             0, 
                             prcStampHPosition.PRC_STAMP_HPOS_CENTER, 
                             prcStampVPosition.PRC_STAMP_VPOS_CENTER, 
                             prcStampZOrder.PRC_STAMP_ZORDER_TOP, 
                             0,
                             0, 
                             0, 
                             100);
      byte[] outMem = (byte[])oProcessorHandle.CloseMemEncrypt("new_user_password", 
"new_owner_password",
prcSecEncryption.PRC_SEC_ENCRYPT_128BITS,
prcSecAnnotationPerm.PRC_SEC_ANNOT_PERM_NONE,
prcSecExtractionPerm.PRC_SEC_EXTR_PERM_NONE,
prcSecModificationPerm.PRC_SEC_MODIFY_PERM_NONE,
prcSecPrintingPerm.PRC_SEC_PRINT_PERM_NONE);
   File.WriteAllBytes(@"C:\test\output.pdf", outMem);
}
catch(System.Runtime.InteropServices.COMException ex)
{
   MessageBox.Show(ex.Message);
}
finally
{
   oProcessorHandle.Close();
}