How to load & save documents using Foxit PDF SDK (C++)
Foxit PDF SDK provides a series of options on how to handle documents. A PDF document object can be constructed with an existing PDF file from a file path, memory buffer, a custom implemented ReaderCallback object and an input file stream. The methods PDFDoc.Load or PDFDoc.StartLoad are used to load document content. Document objects inside a PDF are used for document level operations such as opening and closing files, getting pages, annotation, metadata and more. This article will describe how to perform these and other operations using Foxit PDF SDK.
Example:
Contents
- How to create a PDF document from scratch
- How to load an existing PDF document from file path
- How to load an existing PDF document from a memory buffer
- How to load an existing PDF document from a file read callback object
- How to load PDF document and get the first page of the PDF document
- How to save a PDF to a file
How to create a PDF document from scratch
#include "include/pdf/fs_pdfdoc.h" using namespace foxit; using namespace common; using namespace pdf; ... PDFDoc doc();
Note: It creates a new PDF document without any pages.
How to load an existing PDF document from file path
#include "include/pdf/fs_pdfdoc.h" using namespace foxit; using namespace common; using namespace pdf; ... PDFDoc doc("Sample.pdf"); ErrorCode error_code = doc.Load(); if (error_code!= foxit::e_ErrSuccess) return 0;
How to load an existing PDF document from a memory buffer
#include "include/pdf/fs_pdfdoc.h" using namespace foxit; using namespace common; using namespace pdf; ... FILE* pFile = fopen(TEST_DOC_PATH"blank.pdf", "rb"); ASSERT_EQ(TRUE, NULL != pFile); fseek(pFile, 0, SEEK_END); long lFileSize = ftell(pFile); char* buffer = new char[lFileSize]; memset(buffer, 0, sizeof(char)*lFileSize); fseek(pFile, 0, SEEK_SET); fread(buffer, sizeof(char), lFileSize, pFile); fclose(pFile); PDFDoc doc = PDFDoc(buffer, lFileSize); ErrorCode error_code = doc.Load(); if (error_code!= foxit::e_ErrSuccess) return 0;
How to load an existing PDF document from a file read callback object
#include "include/pdf/fs_pdfdoc.h" using namespace foxit; using namespace common; using namespace pdf; ... class CFSFile_Read : public ReaderCallback { public: CFSFile_Read():m_fileFP(NULL) ,m_bLargeFile(FALSE) {} ~CFSFile_Read() {} bool LoadFile(const wchar_t* wFilePath, bool bLargeFile = FALSE) { std::wstring strTemp(wFilePath); string bstrFilepath = wchar2utf8(strTemp.c_str(), strTemp.size()); m_fileFP = fopen(bstrFilepath.c_str(), "rb"); if (!m_fileFP) return FALSE; m_bLargeFile = bLargeFile; return TRUE; } bool LoadFile(const char* filePath, bool bLargeFile = FALSE) { m_fileFP = fopen(filePath, "rb"); if (!m_fileFP) return FALSE; m_bLargeFile = bLargeFile; return TRUE; } FILESIZE GetSize() { if (m_bLargeFile) { #if defined(_WIN32) || defined(_WIN64) _fseeki64(m_fileFP, 0, SEEK_END); long long sizeL = _ftelli64(m_fileFP); #elif defined(__linux__) || defined(__APPLE__) fseeko(m_fileFP, 0, SEEK_END); long long sizeL = ftello(m_fileFP); #endif return sizeL; } else { fseek(m_fileFP, 0, SEEK_END); return (uint32)ftell(m_fileFP); } } int ReadBlock(void* buffer, FILESIZE offset, size_t size) { if (m_bLargeFile) { #if defined(_WIN32) || defined(_WIN64) _fseeki64(m_fileFP, offset, SEEK_SET); #elif defined(__linux__) || defined(__APPLE__) fseeko(m_fileFP, offset, SEEK_SET); #endif long long readSize = fread(buffer, 1, size, m_fileFP); return (readSize == size); } else { if (!m_fileFP) return false; if(0 != fseek(m_fileFP, offset, 0)) return false; if(0 == fread(buffer, size, 1, m_fileFP)) return false; return true; } } size_t ReadBlock(void* buffer, size_t size) { if (m_bLargeFile) { #if defined(_WIN32) || defined(_WIN64) _fseeki64(m_fileFP, 0, SEEK_SET); #elif defined(__linux__) || defined(__APPLE__) fseeko(m_fileFP, 0, SEEK_SET); #endif return fread(buffer, 1, size, m_fileFP); } else { if (!m_fileFP) return false; if(0 != fseek(m_fileFP, 0, 0)) return 0; return fread(buffer, size, 1, m_fileFP); } } void Release() { if(m_fileFP) fclose(m_fileFP); m_fileFP = NULL; delete this; } private: FILE* m_fileFP; bool m_bLargeFile; } ... string inputPDFPath = "Sample.pdf"; CFSFile_Read* pFileRead = new CFSFile_Read(); If(!pFileRead->LoadFile(inputPDFPath.c_str())) Return; PDFDoc doc = PDFDoc(pFileRead); ErrorCode error_code = doc.Load(); if (error_code!= foxit::e_ErrSuccess) return 0;
How to load PDF document and get the first page of the PDF document
#include "include/pdf/fs_pdfdoc.h" #include "include/pdf/fs_pdfpage.h" using namespace foxit; using namespace common; using namespace pdf; ... PDFDoc doc("Sample.pdf"); ErrorCode error_code = doc.Load(); if (error_code!= foxit::e_ErrSuccess) return 0; PDFPage page = doc.GetPage(0); page.StartParse(foxit::pdf::PDFPage::e_ParsePageNormal, NULL, false);
How to save a PDF to a file
#include "include/pdf/fs_pdfdoc.h" #include "include/pdf/fs_pdfpage.h" using namespace foxit; using namespace common; using namespace pdf; ... PDFDoc doc("Sample.pdf"); ErrorCode error_code = doc.Load(); if (error_code!= foxit::e_ErrSuccess) return 0; doc.SaveAs("new_Sample.pdf", PDFDoc::e_SaveFlagNoOriginal);
Updated on April 1, 2019