Skip to content

Page Object

Page objects help developers with limited PDF object knowledge work with text, path, image, and form objects in PDF documents. Foxit PDF SDK provides APIs to add and remove PDF objects on pages and set their properties. With page objects, users can create PDF pages from object content. Other common uses include adding headers and footers, placing image logos on each page, or generating PDF templates as needed.

Create a Text Object on a PDF Page

c++
#include "include/common/fs_common.h"
#include "include/pdf/graphics/fs_pdfgraphicsobject.h"
#include "include/pdf/fs_pdfdoc.h"
#include "include/pdf/fs_pdfpage.h"

using namespace foxit;
using namespace foxit::common;
using foxit::common::Library;
using namespace pdf;
using namespace graphics;
...

// Assuming PDFPage page has been loaded and parsed.

POSITION position = page.GetLastGraphicsObjectPosition(GraphicsObject::e_TypeText);
TextObject* text_object = TextObject::Create();

text_object->SetFillColor(0xFFFF7F00);

// Prepare text state.
TextState state;
state.font_size = 80.0f;
state.font =  Font(L"Simsun", Font::e_StylesSmallCap, Font::e_CharsetGB2312, 0);
state.textmode = TextState::e_ModeFill;
text_object->SetTextState(page, state, false, 750);

// Set text.
text_object->SetText(L"Foxit Software");
POSITION last_position = page.InsertGraphicsObject(position, text_object);
...
C
#include "include/fs_basictypes_c.h"
#include "include/fs_common_c.h"
#include "include/fs_pdfgraphicsobject_c.h"
#include "include/fs_pdfdoc_c.h"
#include "include/fs_pdfpage_c.h"
...

// Assuming FS_PDFPAGE_HANDLE page has been loaded and parsed.

FS_POSITION position;
FSDK_GraphicsObjects_GetLastGraphicsObjectPosition(page, e_FSTypeText, &position);
FS_TEXTOBJECT_HANDLE text_object;
FSDK_TextObject_Create(&text_object);

FSDK_GraphicsObject_SetFillColor(text_object, 0xFFFF7F00);

// Prepare text state.
FSTextState state;
state.font_size = 80.0f
FSDK_Font_Create(L"Simsun", e_FSStylesSmallCap, e_FSCharsetGB2312, 0, &state.font);
state.textmode = e_FSModeFill;
state.origin_position.x = 0;
state.origin_position.y = 0;
state.wordspace = 0.0f;
state.charspace = 0.0f;
state.textmatrix[0] = 1;
state.textmatrix[1] = 0;
state.textmatrix[2] = 0;
state.textmatrix[3] = 1;
FSDK_TextObject_SetTextState(text_object, page, state, false, 750);

// Set text.
FSDK_TextObject_SetText(text_object, L"Foxit Software");
FS_POSITION last_position;
FSDK_GraphicsObjects_InsertGraphicsObject(page, position, text_object, &last_position);
...
java
import com.foxit.sdk.common.fxcrt.Matrix2D;
import com.foxit.sdk.common.fxcrt.PointF;
import com.foxit.sdk.common.fxcrt.RectF;
import com.foxit.sdk.pdf.PDFDoc;
import com.foxit.sdk.pdf.PDFPage;
import com.foxit.sdk.pdf.graphics.*;
import com.foxit.sdk.common.Font;
import static com.foxit.sdk.common.Font.*;
...

long position = page.getLastGraphicsObjectPosition(e_TypeText);
TextObject text_object = TextObject.create();

text_object.setFillColor(0xFFFF7F00);

// Prepare text state
TextState state = new TextState();
state.setFont_size(80.0f);
state.setFont(new Font("Simsun", e_StylesSmallCap, e_CharsetGB2312, 0));
state.setTextmode(e_ModeFill);
text_object.setTextState(page, state, false, 750);

// Set text.
text_object.setText("Foxit Software");
long last_position = page.insertGraphicsObject(position, text_object);

RectF rect = text_object.getRect();
float offset_x = (page.getWidth() - (rect.getRight() - rect.getLeft())) / 2;
float offset_y = page.getHeight() * 0.8f - (rect.getTop() - rect.getBottom()) / 2;
text_object.transform(new Matrix2D(1, 0, 0, 1, offset_x, offset_y), false);

// Generate content
page.generateContent();
...
py
import sys
import site

if sys.version_info.major == 2:
    _PYTHON2_ = True
else:
    _PYTHON2_ = False

if _PYTHON2_:
    #replace with the python2 lib path
    site.addsitedir('../../../')
    from FoxitPDFSDKPython2 import *
else:
    from FoxitPDFSDKPython3 import *
...

# Assuming PDFPage page has been loaded and parsed.

position = page.GetLastGraphicsObjectPosition(GraphicsObject.e_TypeText)
text_object = TextObject.Create()

text_object.SetFillColor(0xFFFF7F00)

# Prepare text state.
state = TextState()
state.font_size = 80.0
state.font =  Font("Simsun", Font.e_StylesSmallCap, Font.e_CharsetGB2312, 0)
state.textmode = TextState.e_ModeFill
text_object.SetTextState(page, state, False, 750)

# Set text.
text_object.SetText("Foxit Software")
last_position = page.InsertGraphicsObject(position, text_object)
...
objc
#include "FSPDFObjC.h"
...

long position = [page getLastGraphicsObjectPosition:FSGraphicsObjectTypeText];
FSTextObject *text_object = [FSTextObject create];

text_object.fillColor = 0xFFFF7F00;

// Prepare text state
FSTextState *state = [[FSTextState alloc] init];
state.font_size = 80.0f;
FSFont *font = [[FSFont alloc] initWithName:@"Simsun" styles:FSFontStylesSmallCap charset:FSFontCharsetGB2312 weight:0];
state.font = font;
state.textmode = FSTextStateModeFill;
[text_object setTextState:page text_state:state is_italic:false weight:750];

// Set text.
text_object.text = @"Foxit Software";
long last_position = [page insertGraphicsObject:position graphics_object:text_object];
...
js
const FSDK = require("@fuxinsoft/foxit-pdf-sdk-node");
...

// Assuming PDFPage page has been loaded and parsed.

let position = page.GetLastGraphicsObjectPosition(FSDK.GraphicsObject.e_TypeText);
let text_object = FSDK.TextObject.Create();

text_object.SetFillColor(0xFFFF7F00);

// Prepare text state
let state = new FSDK.TextState();
state.font_size = 80.0;
state.font =  new FSDK.Font("Simsun", FSDK.Font.e_StylesSmallCap, FSDK.Font.e_CharsetGB2312, 0);
state.textmode = FSDK.TextState.e_ModeFill;
text_object.SetTextState(page, state, false, 750);

// Set text.
text_object.SetText("Foxit Software");
let last_position = page.InsertGraphicsObject(position, text_object);
...
csharp
using foxit.common;
using foxit.pdf;
using foxit.pdf.graphics;	
using foxit.common.fxcrt;
...

// Assuming PDFPage page has been loaded and parsed.

long position = page.GetLastGraphicsObjectPosition(GraphicsObject.Type.e_TypeText);
TextObject text_object = TextObject.Create();
text_object.SetFillColor(0xFFFF7F00);

// Prepare text state.
TextState state = new TextState();
Font font = new Font("Simsun", (int)Font.Styles.e_StylesSmallCap, Font.Charset.e_CharsetGB2312, 0);
state.font_size = 80.0f;
state.font = font;
state.textmode = TextState.Mode.e_ModeFill;
text_object.SetTextState(page, state, false, 750);

// Set text.
text_object.SetText("Foxit Software");
long last_position = page.InsertGraphicsObject(position, text_object);
...
go
import (
. “foxit.com/fsdk”
)
...

# Assuming PDFPage page has been loaded and parsed.

position := page.GetLastGraphicsObjectPosition(GraphicsObjectE_TypeText)
text_object := TextObjectCreate()

text_object.SetFillColor(0xFFFF7F00)

# Prepare text state.
state := NewTextState()
state.SetFont_size(80.0)
state.SetFont(NewFont("Simsun", FontE_StylesSmallCap, FontE_CharsetGB2312, 0))
state.SetTextmode(TextStateE_ModeFill)
text_object.SetTextState(page, state, false, 750)

# Set text.
text_object.SetText("Foxit Software")
last_position := page.InsertGraphicsObject(position, text_object)
...

Insert an Image Logo on a PDF Page

c++
#include "include/common/fs_common.h"
#include "include/pdf/graphics/fs_pdfgraphicsobject.h"
#include "include/pdf/fs_pdfdoc.h"
#include "include/pdf/fs_pdfpage.h"

using namespace foxit;
using namespace foxit::common;
using foxit::common::Library;
using namespace pdf;
using namespace graphics;
...
//Assuming PDFPage page has been loaded and parsed.

POSITION position = page.GetLastGraphicsObjectPosition(GraphicsObject::e_TypeImage);
Image image(image_file);
ImageObject* image_object = ImageObject::Create(page.GetDocument());
image_object->SetImage(image, 0);

float width = static_cast<float>(image.GetWidth());
float height = static_cast<float>(image.GetHeight());

float page_width = page.GetWidth();
float page_height = page.GetHeight();

// Please notice the matrix value.
image_object->SetMatrix(Matrix(width, 0, 0, height, (page_width - width) / 2.0f, (page_height - height) / 2.0f));

page.InsertGraphicsObject(position, image_object);
page.GenerateContent();
...
C
#include "include/fs_basictypes_c.h"
#include "include/fs_common_c.h"
#include "include/fs_pdfgraphicsobject_c.h"
#include "include/fs_pdfdoc_c.h"
#include "include/fs_pdfpage_c.h"
...

//Assuming FS_PDFPAGE_HANDLE page has been loaded and parsed.

FS_POSITION position;
FSDK_GraphicsObjects_GetLastGraphicsObjectPosition(page, e_FSTypeImage, &position);
FS_IMAGE_HANDLE image;
FSDK_Image_Create0(image_file, &image);
FS_IMAGEOBJECT_HANDLE image_object;
FS_PDFDOC_HANDLE doc;
FSDK_PDFPage_GetDocument(page, &doc);
FSDK_ImageObject_Create(doc, &image_object);
FSDK_ImageObject_SetImage(image_object, image, 0);

int iwidth;
FSDK_Image_GetWidth(image, &width);
int iheight;
FSDK_Image_GetHeight(image, &height);

float width = (float)iwidth;
float height = (float)iheight;

float page_width;
FSDK_PDFPage_GetWidth(page, &page_width);
float page_height;
FSDK_PDFPage_GetHeight(page, &page_height);

// Please notice the matrix value.
FSMatrix matrix;
matrix.a = width;
matrix.b = 0;
matrix.c = 0;
matrix.d = height;
matrix.e = (page_width - width) / 2.0f;
matrix.f = (page_height - height) / 2.0f;
FSDK_GraphicsObject_SetMatrix(image_object, &matrix)

FSDK_GraphicsObjects_InsertGraphicsObject(page, position, image_object, &position);
FS_BOOL return_value;
FSDK_GraphicsObjects_GenerateContent(page, &return_value);
...
java
import com.foxit.sdk.pdf.PDFDoc;
import com.foxit.sdk.pdf.PDFPage;
import com.foxit.sdk.pdf.graphics.*;
import com.foxit.sdk.common.Font;
import static com.foxit.sdk.common.Font.*;	
import com.foxit.sdk.common.Image;
...

long position = page.getLastGraphicsObjectPosition(e_TypeImage);
Image image = new Image(image_file);
ImageObject image_object = ImageObject.create(page.getDocument());
image_object.setImage(image, 0);

float width = image.getWidth();
float height = image.getHeight();

float page_width = page.getWidth();
float page_height = page.getHeight();

// Please notice the matrix value.
image_object.setMatrix(new Matrix2D(width, 0, 0, height, (page_width - width) / 2.0f, (page_height - height) / 2.0f));

page.insertGraphicsObject(position, image_object);
page.generateContent();
...
py
import sys
import site

if sys.version_info.major == 2:
    _PYTHON2_ = True
else:
    _PYTHON2_ = False

if _PYTHON2_:
    #replace with the python2 lib path
    site.addsitedir('../../../')
    from FoxitPDFSDKPython2 import *
else:
    from FoxitPDFSDKPython3 import *
...
# Assuming PDFPage page has been loaded and parsed.

position = page.GetLastGraphicsObjectPosition(GraphicsObject.e_TypeImage)
image = Image(image_file)
image_object = ImageObject.Create(page.GetDocument())
image_object.SetImage(image, 0)

width = float(image.GetWidth())
height = float(image.GetHeight())

page_width = float(page.GetWidth())
page_height = float(page.GetHeight())

# Please notice the matrix value.
image_object.SetMatrix(Matrix2D(width, 0, 0, height, (page_width - width) / 2.0, (page_height - height) / 2.0))

page.InsertGraphicsObject(position, image_object)
page.GenerateContent()
...
objc
#include "FSPDFObjC.h"
...

long position = [page getLastGraphicsObjectPosition:FSGraphicsObjectTypeImage];
FSImage *image = [[FSImage alloc] initWithPath:image_file];
FSImageObject *image_object = [FSImageObject create:[page getDocument]];
[image_object setImage:image frame_index:0];

float width = [image getWidth];
float height = [image getHeight];

float page_width = [page getWidth];
float page_height = [page getHeight];

// Please notice the matrix value.
image_object.matrix = [[FSMatrix2D alloc] initWithA1:width b1:0 c1:0 d1:height e1:(page_width - width) / 2.0f f1:(page_height - height) / 2.0f];

[page insertGraphicsObject:position graphics_object:image_object];
[page generateContent];
...
js
const FSDK = require("@fuxinsoft/foxit-pdf-sdk-node");
// Assuming PDFPage page has been loaded and parsed.

let position = page.GetLastGraphicsObjectPosition(FSDK.GraphicsObject.e_TypeImage);
let image = new FSDK.Image(image_file);
let image_object = FSDK.ImageObject.Create(page.GetDocument());
image_object.SetImage(image, 0);

let width = image.GetWidth();
let height = image.GetHeight();

let page_width = page.GetWidth();
let page_height = page.GetHeight();

// Please notice the matrix value.
image_object.SetMatrix(new FSDK.Matrix2D(width, 0, 0, height, (page_width - width) / 2.0, (page_height - height) / 2.0));

page.InsertGraphicsObject(position, image_object);
page.GenerateContent();
...
csharp
using foxit.common;
using foxit.pdf;
using foxit.pdf.graphics;	
using foxit.common.fxcrt;
...

// Assuming PDFPage page has been loaded and parsed.

long position = page.GetLastGraphicsObjectPosition(GraphicsObject.Type.e_TypeImage);
Image image = new Image(image_file);
ImageObject image_object = ImageObject.Create(page.GetDocument());
image_object.SetImage(image, 0);

float width = image.GetWidth();
float height = image.GetHeight();
float page_width = page.GetWidth();
float page_height = page.GetHeight();

image_object.SetMatrix(new Matrix2D(width, 0, 0, height, (page_width - width) / 2.0f, (page_height - height) / 2.0f));
page.InsertGraphicsObject(position, image_object);
page.GenerateContent();
...
go
import (
. “foxit.com/fsdk”
)
...
# Assuming PDFPage page has been loaded and parsed.

position := page.GetLastGraphicsObjectPosition(GraphicsObjectE_TypeImage)
image := NewImage(image_file)
image_object := ImageObjectCreate(page.GetDocument())
image_object.SetImage(image, 0)

width := float(image.GetWidth())
height := float(image.GetHeight())

page_width := float(page.GetWidth())
page_height := float(page.GetHeight())

# Please notice the matrix value.
image_object.SetMatrix(NewMatrix2D(width, 0, 0, height, (page_width - width) / 2.0, (page_height - height) / 2.0))

page.InsertGraphicsObject(position, image_object)
page.GenerateContent()
...