Skip to content

页面对象 (Page Object)

页面对象可以帮助对 PDF 对象 知识了解有限的开发人员能够处理 PDF 文档中的文本、路径、图像和画布等对象。福昕 PDF SDK 提供 APIs 用以在页面中添加和删除 PDF 对象并设置特定属性。使用页面对象,用户可以从对象内容创建 PDF 页面。页面对象的其他可能用法包括向 PDF 文档添加页眉和页脚,向每个页面添加图片 logo,或者根据需要生成 PDF 模板。

在 PDF 页面中创建一个文本对象

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("@foxitsoftware/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);
...
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("@foxitsoftware/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();
...