LFile::ExtractAttachment

#include "ltwrappr.h"

L_INT LFile::ExtractAttachment(pszSrcFileName, pSrcBuffer, pFileOptions, pFileInfo, uFlags, pszDstFileName, pDstBuffer)

Extracts an attachment from the owner file.

Parameters

L_TCHAR *pszSrcFileName

Owner file name.

If this value is not NULL, then it must point to the name of the file on disk to parse for the attachment.

If this value is NULL, then the owner file data must be set in pSrcBuffer.

Either pszSrcFileName or pSrcBuffer must not be NULL, but not both.

LBuffer *pSrcBuffer

Pointer to the memory buffer containing the data of the owner file.

If this value is not NULL, then it must contain the data of the owner file to parse for the attachment.

If this value is NULL, then pszSrcFileName must point to the name of the file on disk to parse for the attachment.

Either pszSrcFileName or pSrcBuffer must not be NULL, but not both.

pLOADFILEOPTION pFileOptions

Pointer to optional extended load options. This value cannot be NULL. The value of pLOADFILEOPTION->nAttachment must be set to the 1-based attachment number to extract.

pFILEINFO pFileInfo

Pointer to a FILEINFO structure. This structure may contain file information used in loading an image, or it may be updated with information about the file being loaded.

This value can be NULL.

Refer to LFile::LoadFile for more information.

L_UINT uFlags

Reserved for future use. Use 0.

L_TCHAR *pszDstFileName

Destination file name.

If this value is not NULL, then it must contain the name of the file on disk that will be created or overwritten with the attachment file data, if found.

If this value is NULL, then the attachment file data will be set in pDstBuffer.

Either pszDstFileName or ppDstBuffer must not be NULL, but not both.

LBuffer *pDstBuffer

Pointer to the class that will contain the attachment file data, if found.

If this value is not NULL, then it will contain the attachment file data. The file data will be freed when the pDstBuffer class is destroyed. The attachment buffer size will be stored in pDstBuffer->GetSize().

If this value is NULL, then the attachment file data is saved to the name of the file set in pszDstFileName.

Returns

Value Meaning
SUCCESS The function was successful.
ERROR_ATTACHMENT_NOT_FOUND The attachment number specified in pFileOptions->nAttachment was not found in the file.
ERROR_PDF_INVALID_PASSWORD The owner file is encrypted. Set the correct owner file password using LFileSettings::SetPDFOptions and try again.
< 1 An error occurred. Refer to Return Codes.

Comments

Use FILEINFO.nAttachmentCount to quickly determine the number of attachments found in a file.

Use LFile::ReadFileAttachments to obtain the number and properties of all attachments found in a file.

Required DLLs and Libraries

Platforms

Win32, x64.

See Also

Functions

Topics

Example

/ This sample shows how to load an image contained in an encrypted attachment.
The attachment is stored in an encrypted owner file.
The owner and the attachment use different passwords:
The PDF portfolio file (the owner) uses the password 'pass'
The attachment (the embedded file) uses a different password: 'pass1'
This means you cannot load the attachment in only one step, since you need to submit both passwords
Therefore, you will need to use two steps:
1) Extract the attachment using the owner password (pass)
2) Load the image from the attachment buffer using the attachment password (pass1)

L_INT ReadEncryptedFileAttachmentsExample(L_TCHAR* fileName, LBitmapBase &bitmap) 
{ 
   LFile file; 
   LOADFILEOPTION loadFileOption; 
   file.GetDefaultLoadFileOption(&loadFileOption, sizeof(LOADFILEOPTION)); 
   loadFileOption.nAttachment = 1; 
 
   FILEPDFOPTIONS pdfOptions = { sizeof(FILEPDFOPTIONS) }; 
   LFileSettings::GetPDFOptions(&pdfOptions, sizeof(FILEPDFOPTIONS)); 
   strcpy_s((char *)pdfOptions.szPassword, _countof(pdfOptions.szPassword), "pass1"); // try first the password for the attachment (pass1) 
   LFileSettings::SetPDFOptions(&pdfOptions); 
 
   // Try to load the image stored in attachment 1 using the password for the owner file - should fail 
   L_INT nRet = bitmap.Load(fileName, 0, ORDER_BGRORGRAY, &loadFileOption, NULL); 
   assert(nRet == ERROR_PDF_INVALID_PASSWORD); 
 
   if (nRet == ERROR_PDF_INVALID_PASSWORD) 
   { 
      strcpy_s((char *)pdfOptions.szPassword, _countof(pdfOptions.szPassword), "pass"); // use the password for the owner file 
      LFileSettings::SetPDFOptions(&pdfOptions); 
 
      LBuffer buffer; // declare a LBuffer class to store the attachment 
 
      // Extract attachment 1 (= loadFileOption.PageNumber) to a memory buffer. 
      // file.ExtractAttachment will store the output in 'buffer' 
      nRet = file.ExtractAttachment(fileName, NULL, &loadFileOption, NULL, 0, NULL, &buffer); 
      if (nRet != SUCCESS) 
         return nRet; 
 
      LMemoryFile fileMemory(&bitmap); 
 
      // Try loading the attachment without submitting the correct password. This operation should fail 
      nRet = fileMemory.LoadMemory(buffer, 0, ORDER_BGRORGRAY, LOADFILE_ALLOCATE | LOADFILE_STORE, NULL, NULL); 
      assert(nRet == ERROR_PDF_INVALID_PASSWORD); 
 
      if (nRet == ERROR_PDF_INVALID_PASSWORD) 
      { 
         // use the password for the attachment file. 
         strcpy_s((char *)pdfOptions.szPassword, _countof(pdfOptions.szPassword), "pass1"); 
         LFileSettings::SetPDFOptions(&pdfOptions); 
 
         // It is not necessary to pass a LOADFILEOPTION structure anymore, since the main image is being loaded from the attachment. 
         nRet = fileMemory.LoadMemory(buffer, 0, ORDER_BGRORGRAY, LOADFILE_ALLOCATE | LOADFILE_STORE, NULL, NULL); 
      } 
   } 
 
   return nRet; 
} 

Help Version 21.0.2021.7.2
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2021 LEAD Technologies, Inc. All Rights Reserved.

LEADTOOLS Raster Imaging C++ Class Library Help
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2021 LEAD Technologies, Inc. All Rights Reserved.