Professional Documents
Culture Documents
Foxit PDF DLL SDK Tutorial - Ebase Object
Foxit PDF DLL SDK Tutorial - Ebase Object
Contents
Prerequisites ............................................................................................................... 3
Developer Audience ............................................................................................. 3
Supported Environments ...................................................................................... 3
Overview .................................................................................................................... 3
Purpose ............................................................................................................... 3
Setup ......................................................................................................................... 3
Demo Functionalities ................................................................................................... 4
Document ............................................................................................................ 4
Create PDF.................................................................................................... 4
Load PDF ...................................................................................................... 4
Save PDF ...................................................................................................... 4
Close PDF ..................................................................................................... 5
Page .................................................................................................................... 5
Obtain Page Objects ...................................................................................... 5
MediaBox Usage ............................................................................................ 6
CropBox Usage .............................................................................................. 7
Rotation ........................................................................................................ 7
Object ................................................................................................................. 8
Add Text Object............................................................................................. 8
Add Path Object ........................................................................................... 13
Add Image ................................................................................................... 16
Transform Objects ........................................................................................ 18
Delete Objects.............................................................................................. 19
Set Clip ........................................................................................................ 19
Remove Clip ................................................................................................. 20
Object Properties ................................................................................................ 20
2
Foxit PDF DLL SDK
Ebase Object 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
Overview
Purpose
This document covers how to use the Foxit PDF DLL SDK’s editing PDF using objects
mechanism. 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_Objects” project demo
3
Foxit PDF DLL SDK
Ebase Object Demo
Demo Functionalities
The following sections contain references to the “Ebase_Object” demo.
Document
Create PDF
Create PDF with FPDF_CreateNewDocument(), then create pages in the document with
FPDFPage_New().
void CEbase_ObjectDlg::OnBtnCreatePdf()
{
if(m_pPDFDoc == NULL)
{
m_pPDFDoc = FPDF_CreateNewDocument();
m_pCurPage = FPDFPage_New(m_pPDFDoc,0,500,600);
//...
}
}
Load PDF
Save PDF
4
Foxit PDF DLL SDK
Ebase Object Demo
if(m_pPDFDoc != NULL)
{
//...
if (dlg.DoModal() == IDOK)
{
FPDFPage_GenerateContent(m_pCurPage);
CString strPDFName = dlg.GetPathName();
int ret =
FPDF_SaveAsFile(m_pPDFDoc,strPDFName.GetBuffer(strPDFName.GetLength()),NULL,
NULL,0,NULL,0);
}
//...
}
}
Close PDF
Page
5
Foxit PDF DLL SDK
Ebase Object Demo
void CEbase_ObjectDlg::OnBtnPrev()
{
//...
m_pCurPage = NULL;
m_nCurPageNum -= 1;//go to previous page
m_pCurPage = FPDF_LoadPage(m_pPDFDoc, m_nCurPageNum);
//...
}
void CEbase_ObjectDlg::OnBtnNext()
{
//...
m_pCurPage = NULL;
m_nCurPageNum += 1;//go to next page
m_pCurPage = FPDF_LoadPage(m_pPDFDoc, m_nCurPageNum);
//...
}
void CEbase_ObjectDlg::OnBtnLast()
{
//...
m_pCurPage = NULL;
m_nCurPageNum = m_nTotalNum -1; // go to last page
m_pCurPage = FPDF_LoadPage(m_pPDFDoc, m_nCurPageNum);
//...
}
MediaBox Usage
double left,right,bottom,top;
FPDFPage_GenerateContent(m_pCurPage);
FPDFPage_GetRectangle(m_pCurPage, FPDF_RECT_MEDIABOX,
&left, &right, &bottom, &top);
CropBox Usage
FPDFPage_SetRectangle(m_pCurPage, FPDF_RECT_CROPBOX,
left-50,right-50, bottom-50, top-50);
//...
}
Rotation
Use FPDFPage_SetRotation() to change the rotation of the page. One of following values
will be set for the rotation flag: 0(0 degree), 1(90 degrees), 2(180 degrees), 3(270
degrees)
void CEbase_ObjectDlg::OnBtnRotation()
{
//...
FPDFPage_SetRotation(m_pCurPage,m_nRotation+1);
//...
}
7
Foxit PDF DLL SDK
Ebase Object Demo
Object
There are two ways to add text object with font: adding a TrueType font, and adding a
PDF-standard supported font. Adding TrueType font requires the use of FPDFLOGFONT
object, but adding PDF-Standard font does not. OnBtnAddText() shows the call order of
our functions to add these text objects:
void CEbase_ObjectDlg::OnBtnAddText()
{
//adding TrueType font
FPDF_PAGEOBJECT textobj = FPDFPageObj_NewTextObj();
//perform the insert. Note that for a form inside PDF and no form inside PDF
8
Foxit PDF DLL SDK
Ebase Object Demo
if (!pFont)
return;
Insert Text
void CEbase_ObjectDlg::OnBtnInsertText()
{
//...
FPDF_PAGEOBJECT page_obj;
//different functions used for obtaining object from a form page and a
//regular page
if(m_bFormPage == FALSE)
{
page_obj= FPDFPage_GetObject(m_pCurPage, i);
}
else
{
page_obj=FPDFFormObj_GetSubObject(m_pCurPage,i);
}
if (page_obj)
{
int type = FPDFPageObj_GetType(page_obj);
if(type == FPDF_PAGEOBJ_TEXT)
{
wchar_t text[] = L"MM";
int nCount = wcslen(text);
int nNewCharCount = FPDFTextObj_Insert(page_obj,text,nCount,0);
}
}
//...
}
10
Foxit PDF DLL SDK
Ebase Object Demo
{
page_obj=FPDFFormObj_GetSubObject(m_pCurPage,i);
}
if (page_obj)
{
int type = FPDFPageObj_GetType(page_obj);
if(type == FPDF_PAGEOBJ_TEXT)
{
BOOL bTrueOrFal =
FPDFTextObj_SetUnicode(page_obj,0,0X004B);//0X004B is unicode for char "K"
}
}
//...
}
Delete Text
11
Foxit PDF DLL SDK
Ebase Object Demo
}
//...
}
Replace Text
12
Foxit PDF DLL SDK
Ebase Object Demo
Compare Text
OnBtnAddPath() shows the function order to add path object to a PDF page.
void CEbase_ObjectDlg::OnBtnAddPath()
{
FPDF_PAGEOBJECT path_obj = FPDFPageObj_NewPathObj();
13
Foxit PDF DLL SDK
Ebase Object Demo
14
Foxit PDF DLL SDK
Ebase Object Demo
FPDFPage_InsertObject(m_pCurPage,path_obj);
}
else
{
FPDFFormObj_InsertSubObject(m_pCurPage,path_obj);
}
//...
}
Modify Path
15
Foxit PDF DLL SDK
Ebase Object Demo
if(step == 4){
FPDFPageObj_SetLineJoinStyle(path_obj, 1);//join round
//...);
}
if(step == 5){
FPDFPageObj_SetLineJoinStyle(path_obj, 0); //join miter
//...
}
if(step == 6){
FPDFPageObj_SetMiterLimit(path_obj, 1);//set miter limit to 1
//...
}
if(step == 7){
FPDFPathObj_RemovePoint(path_obj, 2);//remove specific point by index
//...
}
if(step == 8){
FPDFPathObj_ClearPoints(path_obj);//remove all points
//...
}
//...
}
Add Image
//...
}
Modify Image
{
double a,b,c,d,e,f;
//get the matrix from the image object
FPDFImageObj_GetMatrix(pPageObj,&a,&b,&c,&d,&e,&f);
a=a*1.2;
d=d*1.2;
c=c+100;
f = f+100;
//set the new matrix, replacing the old one
FPDFImageObj_SetMatrix(pPageObj,a,b,c,d,e,f);
Invalidate(FALSE);
}
}
Transform Objects
Delete Objects
Set Clip
Set clip region for an object by setting points on a path, then use FPDFPageObj_AddClip()
with the path to add the region. The path represents an enclosed region.
void CEbase_ObjectDlg::OnBtnSetClip()
{
//set up path
FPDF_PAGEOBJECT path_obj = FPDFPageObj_NewPathObj();
double dbPointX[4] = {150, 150, 400,400};
double dbPointY[4] = {150, 400, 400,150};
int flags[4] = {FPDF_PATH_MOVETO, FPDF_PATH_LINETO,
FPDF_PATH_LINETO,FPDF_PATH_LINETO|FPDF_PATH_CLOSEFIGURE};
FPDFPathObj_SetPoints(path_obj, 4, dbPointX, dbPointY, flags);
FPDF_PATH path = FPDFPathObj_GetPath(path_obj);
//...
FPDF_PAGEOBJECT page_obj;
//different functions used for obtaining object from a form page and a
//regular page
19
Foxit PDF DLL SDK
Ebase Object Demo
if(m_bFormPage == FALSE)
{
page_obj = FPDFPage_GetObject(m_pCurPage, i);
}
else
{
page_obj = FPDFFormObj_GetSubObject(m_pCurPage, i);
}
//add path as the clip region
FPDFPageObj_AddClip(page_obj, path, FPDF_FILL_NULL);
FPDFPageObj_Free(path_obj);
//...
}
Remove Clip
Object Properties
Individual objects have different properties that you can obtain. ShowPageObjProperty()
contains example on how to obtain many of these. These correspond to the bottom of the
20
Foxit PDF DLL SDK
Ebase Object Demo
//GetFillColor
FPDF_DWORD color = FPDFPageObj_GetFillColor(page_obj);
int r,g,b,a;
r = FPDF_GetRValue(color);
g = FPDF_GetGValue(color);
b = FPDF_GetBValue(color);
a = FPDF_GetAValue(color)*100/255;
//...
//GetStrokeColor
color = FPDFPageObj_GetStrokeColor(page_obj);
r = FPDF_GetRValue(color);
g = FPDF_GetGValue(color);
b = FPDF_GetBValue(color);
a = FPDF_GetAValue(color)*100/255;
//...
//GetLineWidth
int nLineWidth = FPDFPageObj_GetLineWidth(page_obj);
//...
//GetLineCap
int nLineCap = FPDFPageObj_GetLineCapStyle(page_obj);
CString strLineCap;
21
Foxit PDF DLL SDK
Ebase Object Demo
switch(nLineCap)
{
case FPDF_LINECAP_BUTT:
strLineCap = "Butt Cap";
break;
case FPDF_LINECAP_ROUND:
strLineCap = "Round Cap";
break;
case FPDF_LINECAP_PROJECT:
strLineCap = "Projecting square";
break;
default:
strLineCap = "";
break;
}
//...
//GetLineJoin
int nLineJoin = FPDFPageObj_GetLineJoinStyle(page_obj);
CString strLineJoin;
switch(nLineJoin)
{
case FPDF_LINEJOIN_MITER:
strLineJoin = "Miter Join";
break;
case FPDF_LINEJOIN_ROUND:
strLineJoin = "Round Join";
break;
case FPDF_LINEJOIN_BEVEL:
strLineJoin = "Bevel Join";
break;
default:
strLineJoin = "";
break;
}
//...
//GetMiterLimit
int nMiterLimit = FPDFPageObj_GetMiterLimit(page_obj);
//...
//GetDashCount
22
Foxit PDF DLL SDK
Ebase Object Demo
if(nDashCount >0)
{
//GetDashArray
str = "Dash Array : \n ";
for(int i = 0; i<nDashCount; i++)
{
double lfDashData = FPDFPageObj_GetDashArray(page_obj,i);
//...
}
//...
//GetDashPhase
double lfDashPhase = FPDFPageObj_GetDashPhase(page_obj);
//...
}
//...
//path only
if(type == FPDF_PAGEOBJ_PATH)
{
//GetFillMode
int nMode = FPDFPathObj_GetFillMode(page_obj);
CString strFillMode;
switch(nMode)
{
case FPDF_FILL_NULL:
strFillMode = "No fill color.";
break;
case FPDF_FILL_ALTERNATE:
strFillMode = "Alternate";
break;
case FPDF_FILL_WINDING:
strFillMode = "Winding";
break;
default:
break;
}
//...
/////GetStroke
23
Foxit PDF DLL SDK
Ebase Object Demo
/////GetPointNumber
int nPointCount = FPDFPathObj_CountPoints(page_obj);
//...
}
//...
//text only
if(type == FPDF_PAGEOBJ_TEXT)
{
//GetTextMode
int nMode = FPDFTextObj_GetTextMode(page_obj);
CString strMode;
switch(nMode)
{
case FPDF_TEXTMODE_FILL:
strMode = "Fill";
break;
case FPDF_TEXTMODE_STROKE:
strMode = "Stroke";
break;
case FPDF_TEXTMODE_FILLANDSTROKE:
strMode = "Fill And Stroke";
break;
case FPDF_TEXTMODE_INVISIBLE:
strMode = "No Fill No Stroke";
break;
default:
break;
}
//...
//GetFontName
FPDF_FONT font = FPDFTextObj_GetFont (page_obj);
const char* pName = FPDFFont_GetFontName(font);
//...
//GetFontSize
int nSize = FPDFTextObj_GetFontSize(page_obj);
//...
24
Foxit PDF DLL SDK
Ebase Object Demo
//GetFontFlag
int nFlag = FPDFFont_GetFlags(font);
CString strFlag;
if(nFlag == 0)
{
strFlag = " Reserve";
}
else
{
if(FPDF_FLAG_FIXEDPITCH & nFlag)
strFlag += " FixedPitch";
if(FPDF_FLAG_SERIF & nFlag)
strFlag += " Serif";
if(FPDF_FLAG_SYMBOLIC & nFlag)
strFlag += " Symbolic";
if(FPDF_FLAG_SCRIPT & nFlag)
strFlag += " Script";
if(FPDF_FLAG_NONSYMBOLIC & nFlag)
strFlag += " NonSymbolic";
if(FPDF_FLAG_ITALIC & nFlag)
strFlag += " Italic";
if(FPDF_FLAG_ALLCAP & nFlag)
strFlag += " AllCap";
if(FPDF_FLAG_SMALLCAP & nFlag)
strFlag += " SmallCap";
if(FPDF_FLAG_FORCEBOLD & nFlag)
strFlag += " ForceBold";
}
//...
25
Foxit PDF DLL SDK
Ebase Object Demo
}
//...
//GetBBox
double left,right,bottom,top;
FPDFPageObj_GetBBox(page_obj,&left,&right,&bottom,&top);
//...
//GetMatrix
double matrix_a,matrix_b,matrix_c,matrix_d,matrix_e,matrix_f;
switch(type)
{
case FPDF_PAGEOBJ_TEXT:
FPDFTextObj_GetMatrix(page_obj,&matrix_a,&matrix_b,
&matrix_c,&matrix_d,&matrix_e,&matrix_f);
//...
break;
case FPDF_PAGEOBJ_IMAGE:
FPDFImageObj_GetMatrix(page_obj,&matrix_a,&matrix_b,
&matrix_c,&matrix_d,&matrix_e,&matrix_f);
//...
break;
case FPDF_PAGEOBJ_FORM:
FPDFFormObj_GetMatrix(page_obj,&matrix_a,&matrix_b,
&matrix_c,&matrix_d,&matrix_e,&matrix_f);
//…
break;
case FPDF_PAGEOBJ_PATH:
FPDFPathObj_GetMatrix(page_obj,&matrix_a,&matrix_b,
&matrix_c,&matrix_d,&matrix_e,&matrix_f);
//…
break;
default:
str = "Matrix : NULL";
break;
}
//…
//GetClipCount
int nClipNum = FPDFPageObj_GetClipCount(page_obj);
str.Format("Clip Count : %d",nClipNum);
//…
26
Foxit PDF DLL SDK
Ebase Object Demo
27