Loading and Saving PDFs To Memory

The PDFCreator control can load data from memory and save back PDF data to memory or sources other than files.  It implements a standard IStream interface, more precisely an IPersistStreamInit interface. For details about these two interfaces, please consult the Windows documentation.

 

Example

IStream *inputStream = NULL;

IPersistStreamInit *pdfStream = NULL;

 

// m_pdfControl is the object associated with the PDF control

m_pdf.GetControlUnknown()->QueryInterface( IID_IPersistStreamInit, (void **)&pdfStream );

 

// for testing, load a PDF file into memory, then send it to Creator

CFile inFile( " LongDocument.pdf", CFile::modeRead );

 

// hg is the handle to the memory where the PDF data resides

HGLOBAL hg = GlobalAlloc( GHND, inFile.GetLength() );

inFile.Read( GlobalLock( hg ), inFile.GetLength() );

GlobalUnlock( hg );

CreateStreamOnHGlobal( hg, TRUE, &inputStream );

 

// load the PDF control from the memory object

pdfStream->Load( inputStream );

inputStream->Release();

pdfStream->Release();

 

// save the PDF to another memory buffer

ULARGE_INTEGER size;

IStream *outputStream = NULL;

IPersistStreamInit *pdfStream2 = NULL;

 

// m_pdfControl is the object associated with the PDF control

m_pdf.GetControlUnknown()->QueryInterface( IID_IPersistStreamInit, (void **)&pdfStream2 );

pdfStream2->GetSizeMax( &size ); // get the size

 

// hg1 is the handle to the memory where the PDF data will reside

HGLOBAL hg1 = GlobalAlloc( GHND, size.LowPart );

CreateStreamOnHGlobal( hg1, FALSE, &outputStream );

pdfStream2->Save( outputStream, TRUE );

outputStream->Release();

LPBYTE pdfData = (LPBYTE)GlobalLock( hg1 );

CFile file;

file.Open (_T ("amyuni_new.pdf"), CFile::modeCreate | CFile::modeWrite | CFile::shareDenyWrite );

file.Write (pdfData, (unsigned int)size.QuadPart );

file.Close ();

GlobalUnlock( hg1 );