Foxit PDF DLL SDK
Tutorial – Ebase Bookmark
Demo
Foxit PDF DLL SDK
Ebase Bookmark Demo
Contents
Prerequisites ............................................................................................................... 3
Developer Audience ............................................................................................. 3
Supported Environments ...................................................................................... 3
Overview .................................................................................................................... 3
Purpose ............................................................................................................... 3
Setup ......................................................................................................................... 3
Demo Functionalities ................................................................................................... 4
Obtaining Bookmark ............................................................................................. 4
Bookmark fields ................................................................................................... 4
Add Bookmark ..................................................................................................... 5
Delete Bookmark.................................................................................................. 6
Set Bookmark Color.............................................................................................. 7
Set Bookmark Font Style ...................................................................................... 7
Set Bookmark Title ............................................................................................... 7
2
Foxit PDF DLL SDK
Ebase Bookmark Demo
Prerequisites
Developer Audience
This document is targeted towards C/C++ developers using the Foxit PDF DLL SDK. It
assumes the developer is familiar with C/C++ and Microsoft Foundation Classes (MFC).
Supported Environments
Platform Operating System Compiler
Windows (32-bit) Windows 2000/XP or later Microsoft Visual Studio 6.0.
Overview
Purpose
This document covers how to use the Foxit PDF DLL SDK’s bookmark editing module. It
uses the demo provided by Foxit Corporation as reference for explanation.
Setup
1) Download these two items from Foxit:
a. Evaluation version of the Foxit PDF DLL SDK
b. The Visual C++ 6.0 Demo
2) Extract both .zip files. Move fpdfsdk.dll and fpfsdk.lib to <yourDemoDirectory>/bin
3) Copy <yourSdkDirectory>/includes/*.h to <yourDemoDirectory>/includes,
overwriting all .h’s. You may encounter errors in building the demo if this is
not done
4) Open examples.dsw with Visual Studio 6.0
5) Compile “Ebase_Bookmark” project demo
3
Foxit PDF DLL SDK
Ebase Bookmark Demo
Demo Functionalities
The following sections contain references to the “Ebase_Bookmark” demos.
Obtaining Bookmark
When the Bookmark demo first opens a PDF file is opened with OpenPdfFile, the bookmark
content of the file should be displayed on the left panel. Inside
CEbase_BookmarkDlg::OpenPdfFile(), it first opens the document:
m_pDoc= FPDF_LoadDocument(m_PdfPath,NULL); // a FPDF_DOCUMENT object
if (m_pDoc)
m_TreeCtrl.SetTreeData(m_pDoc);
FPDF_LoadPdfDocument() opens the document and we save the document handle for
future use. We then call setTreeData(). Inside setTreeData() we call functions to get the
bookmarks:
FPDF_BOOKMARK bookmark1 = NULL;
//...
bookmark1 = FPDFBookmark_GetFirstChild(m_pDoc, NULL);
int num = FPDF_GetPageCount(m_pDoc);
if (bookmark1 == NULL)//insert the page index to tree
{
for (int i=0; i<num; i++)
{
//set bookmark label in MFC Tree view
}
}else{
while(bookmark1 != NULL) {
this->InsertChildItem(bookmark1, m_hItemRoot, ptreeCtrl);
bookmark1 = FPDFBookmark_GetNextSibling(m_pDoc,bookmark1);
}
}
FPDFBookmark_GetFirstChild() is used to pick up the first FPDF_BOOKMARK object,
adds relevant information with insertChildItem(), then FPDFBookmark_GetNextSibling()
is called to get the next FPDF_BOOKMARK object.
Bookmark Fields
Bookmarks have several related fields: Title, Action, and Destination.
4
Foxit PDF DLL SDK
Ebase Bookmark Demo
CBookmarkTree::insertChilditem(), and CBoomarkTree::GetBookmarkTitle(), several
bookmark related getters are called.
CBookmarkTree:GetBookmarkTitle() :
FPDFBookmark_GetTitle() – called inside to get the bookmark title.
CBookmarkTree::insertChilditem() :
FPDFBookmark_GetAction() – called to get a FPDF_ACTION object (the bookmark’s
designated action)
FPDFAction_GetDest() – called to get a FPDF_DEST object (the action’s destination
inside the PDF).
FPDFDest_GetPageIndex() – called to get the page number of page for the
FPDF_DEST object.
FPDFDest_GetZoomMode() – called to get the zoom mode for the FPDF_DEST object.
FPDFDest_GetZoomParam() – called to get the zoom parameter for the FPDF_DEST
object.
Add Bookmark
Bookmarks can do several things: Go to a certain page in the file, launch another file, open
an URI in your application (i.e. an URL in browser), or go to a remote area (could be in the
same file or in another file).
CBookmarkTree::AddBookmark() finds two items necessary to designate the new
bookmark’s place in the bookmark tree: the parent bookmark, and the bookmark that
comes after the new bookmark. Then it calls FPDFBookmark_AddBookmark() to create the
new bookmark (a FPDF_BOOKMARK object).
CBookmarkTree::ActionGoToPage() shows how to set up the newly created bookmark to
take the action of going to a certain page:
//PDFZOOM_FITHORZ
FPDF_DEST dest = FPDF_CreateDest(PDFZOOM_FITHORZ);
FPDFDest_SetZoomMode(dest,PDFZOOM_FITHORZ);
FPDF_ACTION action = FPDF_CreateAction(m_pDoc,PDFACTION_GOTO);
FPDFDest_SetFITHORZParam(dest,10);
FPDFDest_SetPageIndex(m_pDoc,dest,1);
FPDFAction_SetDest(action,dest);
FPDFBookmark_SetAction(bookmark,action);
FPDF_DeleteDest(dest);
This sets the bookmark with going to page 1, and fit the PDF horizontally. Note that
FPDF_ACTION and FPDF_DEST objects need to be created in setting the bookmark. There
are other examples for different fit mode in the comments of the function.
5
Foxit PDF DLL SDK
Ebase Bookmark Demo
CBookmarkTree::ActionLaunch() shows how to set up the newly created bookmark to
launch another file:
FPDF_DEST dest = FPDF_CreateDest(PDFZOOM_FITBBOX);
FPDFDest_SetZoomMode(dest,PDFZOOM_FITBBOX);
FPDF_ACTION action = FPDF_CreateAction(m_pDoc,PDFACTION_LAUNCH);
FPDFAction_SetFilePath(action,"I:\\workplace\\project\\Dll SDK\\dllsdk
3.0\\handwrite.pdf",FALSE);
FPDFAction_SetDest(action,dest);
FPDFBookmark_SetAction(bookmark,action);
FPDF_DeleteDest(dest);
CBookmarkTree::ActionURI() shows how to set up the newly created bookmark to open
an URI:
FPDF_DEST dest = FPDF_CreateDest(PDFZOOM_FITBBOX);
FPDFDest_SetZoomMode(dest,PDFZOOM_FITBBOX);
FPDF_ACTION action = FPDF_CreateAction(m_pDoc,PDFACTION_URI);
//FPDFAction_SetFilePath(action,"I:\\workplace\\project\\Dll SDK\\dllsdk
3.0\\handwrite.pdf",FALSE);
FPDFAction_SetURI(action,_T("www.foxitsoftware.com"));
FPDFAction_SetDest(action,dest);
FPDFBookmark_SetAction(bookmark,action);
FPDF_DeleteDest(dest);
CBookmarkTree::ActionGoToR() shows how to set up the newly created bookmark to go
to a remote location (in this case another local file):
FPDF_DEST dest = FPDF_CreateDest(PDFZOOM_FITBBOX);
FPDFDest_SetZoomMode(dest,PDFZOOM_FITBBOX);
FPDF_ACTION action = FPDF_CreateAction(m_pDoc,PDFACTION_REMOTEGOTO);
FPDFAction_SetFilePath(action ,"I:\\workplace\\project\\Dll SDK\\dllsdk
3.0\\handwrite.pdf",FALSE);
FPDFAction_SetDest(action,dest);
FPDFBookmark_SetAction(bookmark,action);
FPDF_DeleteDest(dest);
Delete Bookmark
Delete a bookmark inside a PDF document with FPDFBookmark_DeleteBookmark(). In
CBookmarkTree::DeleteBookmark(), FPDF_Bookmark_DeleteBookmark() is called with the
FPDF_DOCUMENT object and the FPDF_BOOKMARK object.
6
Foxit PDF DLL SDK
Ebase Bookmark Demo
Set Bookmark Color
Set a bookmark’s color with FPDFBookmark_SetColorRef(). To get the color of a
bookmark, use FPDF_Bookmark_GetColorRef().
CBookmarkTree:SetItemColor():
ColorFont_BMDict cf;
this->GetItemDict(hItem,cf); //obtain the dict object that contains the
bookmark object
BYTE a,r,g,b;
FPDFBookmark_SetColorRef(cf.pDict,color);
DWORD dr = FPDFBookmark_GetColorRef(cf.pDict);
r = GetRValue(dr);
g = GetGValue(dr);
b = GetBValue(dr);
//.. MFC invalidate work
Set Bookmark Font Style
Set a bookmark’s font style with FPDFBookmark_SetFontStyle(). Integer flags defined in
fpdfeditbase.h denote fontstyle. The styles are normal, italic, and bold.
void CBookmarkTree::SetFontStyle(HTREEITEM hItem,unsigned long fontstyle)
{
ColorFont_BMDict cf;
this->GetItemDict(hItem,cf);//pick up dictionary
FPDFBookmark_SetFontStyle(cf.pDict,fontstyle);//set font style
this->SetItemDict(hItem,cf.pDict);
Invalidate(FALSE);
}
Set Bookmark Title
Set a bookmark’s title with FPDFBookmark_SetBookmarkTitle().
BOOL CBookmarkTree::SetBookMarkTtitle(HTREEITEM hItem,CString title)
{
ColorFont_BMDict cf;
7
Foxit PDF DLL SDK
Ebase Bookmark Demo
this->GetItemDict(hItem,cf);
if (cf.pDict)
{
BOOL bRet = FPDFBookmark_SetTitle(cf.pDict,title);
if (bRet)
{
this->SetItemDict(hItem,cf.pDict);
this->SetItemText(hItem,title);
}
return bRet;
}
return FALSE;
}
Get a bookmark’s title with FPDFBookmark_GetTitle(). Note the preparation of the buffer
for write.
CString CBookmarkTree::GetBookmarkTitle(FPDF_BOOKMARK bookmark)
{
WCHAR buffer[1024];
int strlenth = 0;
memset(buffer,0,1024*sizeof(WCHAR));
strlenth = FPDFBookmark_GetTitle(bookmark, buffer, 0);
int nlen = WideCharToMultiByte(CP_ACP,0,buffer,-1,NULL,NULL,NULL,NULL);
char *buffer1 = new char[nlen];
memset(buffer1,0,nlen);
WideCharToMultiByte(CP_ACP,0,buffer,strlenth,buffer1,nlen,NULL,NULL);
buffer1[nlen -1] = '\0';
CString title(buffer1);
delete buffer1;
return title;
}