Page Objects
Foxit PDF SDK for Android provides two levels of page content APIs through the Core SDK:
- PDFPage shortcuts — Add text or images via
PDFPageconvenience methods for common cases. - Graphics objects API — Operate on the page content stream directly: create, traverse, modify, and delete text, images, paths, and more for precise control.
PDFPage Shortcuts: Insert Text and Images
PDFPage provides shortcuts without managing graphics objects and content streams manually.
Insert Image
From file path:
PDFPage.addImageFromFilePath(String file_path, PointF position,
float width, float height, boolean auto_generate_content)From Image object (multi-frame):
PDFPage.addImage(Image image, int frame_index, PointF position,
float width, float height, boolean auto_generate_content)NOTE
Parse the target page before calling addImage / addImageFromFilePath.
Example:
PDFDoc doc = new PDFDoc(filePath);
doc.load(null);
PDFPage page = doc.getPage(0);
if (!page.isParsed()) {
Progressive parse = page.startParse(PDFPage.e_ParsePageNormal, null, false);
int state = Progressive.e_ToBeContinued;
while (state == Progressive.e_ToBeContinued) {
state = parse.resume();
}
}
page.addImageFromFilePath("/sdcard/FoxitSDK/logo.png",
new PointF(20, 30), 60, 50, true);
doc.saveAs(outputPath, PDFDoc.e_SaveFlagNormal);Insert Text
PDFPage.addText(String text, RectF rect, RichTextStyle style)
PDFPage.addText(String text, RectF rect, RichTextStyle style, int rotation)RichTextStyle sets font, size, color, bold, italic, underline, and alignment.
Example:
RichTextStyle style = new RichTextStyle();
style.setFont(new Font(Font.e_StdIDHelvetica));
style.setText_size(16.0f);
style.setText_color(0xFF000000);
style.setIs_bold(true);
style.setText_alignment(CommonDefines.e_AlignmentLeft);
page.addText("Hello, Foxit PDF SDK!", new RectF(50, 750, 400, 700), style);
doc.saveAs(outputPath, PDFDoc.e_SaveFlagNormal);Graphics Objects API
Direct content-stream APIs for precise control of object properties, position, and z-order.
Core Classes
| Class | Package | Description |
|---|---|---|
GraphicsObjects | com.foxit.sdk.pdf | Graphics object collection (PDFPage parent); insert, delete, traverse |
GraphicsObject | com.foxit.sdk.pdf.graphics | Base; type, color, matrix, opacity |
TextObject | com.foxit.sdk.pdf.graphics | Text |
ImageObject | com.foxit.sdk.pdf.graphics | Image |
PathObject | com.foxit.sdk.pdf.graphics | Vector paths |
FormXObject | com.foxit.sdk.pdf.graphics | Reusable content |
ShadingObject | com.foxit.sdk.pdf.graphics | Shading |
Graphics Object Types
| Constant | Value | Description |
|---|---|---|
GraphicsObject.e_TypeAll | 0 | All types |
GraphicsObject.e_TypeText | 1 | Text |
GraphicsObject.e_TypePath | 2 | Path |
GraphicsObject.e_TypeImage | 3 | Image |
GraphicsObject.e_TypeShading | 4 | Shading |
GraphicsObject.e_TypeFormXObject | 5 | Form XObject |
Traverse Page Graphics Objects
PDFPage extends GraphicsObjects:
PDFPage page = doc.getPage(0);
if (!page.isParsed()) {
Progressive parse = page.startParse(PDFPage.e_ParsePageNormal, null, false);
int state = Progressive.e_ToBeContinued;
while (state == Progressive.e_ToBeContinued) {
state = parse.resume();
}
}
long pos = page.getFirstGraphicsObjectPosition(GraphicsObject.e_TypeAll);
while (pos != 0) {
GraphicsObject obj = page.getGraphicsObject(pos);
int type = obj.getType();
switch (type) {
case GraphicsObject.e_TypeText:
TextObject textObj = obj.getTextObject();
String text = textObj.getText();
break;
case GraphicsObject.e_TypeImage:
ImageObject imgObj = obj.getImageObject();
break;
case GraphicsObject.e_TypePath:
PathObject pathObj = obj.getPathObject();
break;
}
pos = page.getNextGraphicsObjectPosition(pos, GraphicsObject.e_TypeAll);
}Create Text Object
TextObject textObj = TextObject.create();
textObj.setText("Hello, Foxit SDK!");
TextState textState = new TextState();
textState.font_size = 24.0f;
textState.font = new Font(Font.e_StdIDHelvetica);
textState.textmode = TextState.e_ModeFill;
textObj.setTextState(page, textState, false, 400);
textObj.setFillColor(0xFF000000);
Matrix2D matrix = new Matrix2D(1, 0, 0, 1, 100, 700);
textObj.setMatrix(matrix);
long lastPos = page.getLastGraphicsObjectPosition(GraphicsObject.e_TypeAll);
page.insertGraphicsObject(lastPos, textObj);
page.generateContent();Important
After insert, delete, or modify, call page.generateContent() to write changes to the content stream.
Create Image Object
ImageObject imgObj = ImageObject.create(doc);
Bitmap bitmap = BitmapFactory.decodeFile(imagePath);
imgObj.setBitmap(bitmap, null);
Matrix2D matrix = new Matrix2D(200, 0, 0, 150, 100, 500);
imgObj.setMatrix(matrix);
long lastPos = page.getLastGraphicsObjectPosition(GraphicsObject.e_TypeAll);
page.insertGraphicsObject(lastPos, imgObj);
page.generateContent();Create Path Object
PathObject pathObj = PathObject.create();
Path pathData = new Path();
pathData.moveTo(new PointF(100, 700));
pathData.lineTo(new PointF(300, 700));
pathData.lineTo(new PointF(300, 500));
pathData.closeFigure();
pathObj.setPathData(pathData);
pathObj.setFillMode(Path.e_FillModeAlternate);
pathObj.setStrokeState(true);
pathObj.setStrokeColor(0xFF0000FF);
pathObj.setFillColor(0x80FFFF00);
long lastPos = page.getLastGraphicsObjectPosition(GraphicsObject.e_TypeAll);
page.insertGraphicsObject(lastPos, pathObj);
page.generateContent();Create Form XObject
FormXObject formXObj = FormXObject.create(doc);
formXObj.importPageContent(sourcePage, false);
GraphicsObjects innerObjects = formXObj.getGraphicsObjects();Delete Graphics Objects
page.removeGraphicsObject(graphicsObject);
page.removeGraphicsObjectByPosition(position);
page.generateContent();Change Object Order
page.moveGraphicsObjectByPosition(currentPos, targetPos);
page.generateContent();API Reference
See the API Reference for full GraphicsObject, TextObject, ImageObject, PathObject, and FormXObject documentation.