How to manage your PDF Pages using Foxit PDF SDK (Java)
In PDF development, a PDF page means much more than in the final user’s PDF editor or viewer. Page objects help group and identify multiple objects in the PDF to perform other operations. A PDFPage object is retrieved from a PDF document by function PDFDoc.getPage. Page level APIs provide functions to parse, render, edit (includes creating, deleting and flattening) a page, retrieve PDF annotations, read and set the properties of a page, and more. For most cases, a PDF page needs to be parsed before it is rendered or processed.
In this article, we will provide sample code examples for handling your pages using Foxit PDF SDK:
Example:
Contents
How to get page size
import static com.foxit.sdk.pdf.PDFPage.*; ... // Assuming PDFPage page has been loaded and parsed. ... int width = (int) page.getWidth(); int height = (int) page.getHeight(); ...
How to calculate bounding box of page contents
import static com.foxit.sdk.pdf.PDFPage.*; ... // Assuming PDFPage page has been loaded and parsed. ... RectF calcRc = page.calcContentBBox(e_CalcContentsBox); float fcalcRc = calcRc.width(); float fcalcRcHeight = calcRc.height();
How to create a PDF page and set the size
import static com.foxit.sdk.pdf.PDFPage.*; ... // Assuming PDFDoc doc has been loaded. // Insert a new blank PDF page to document, which will be inserted to the first page. PDFPage newBlankPage = doc.insertPage(-1, 500, 800); // Insert a new blank PDF page to document, which will be inserted at index 1. PDFPage newBlankPage = doc.insertPage(1, 500, 800); // Insert a new blank PDF page to document, which will be inserted to the end. PDFPage newBlankPage = doc.insertPage(doc.getPageCount(), e_SizeLetter);
How to delete a PDF page
import static com.foxit.sdk.pdf.PDFDoc.*; ... // Assuming PDFDoc doc has been loaded. // Remove a PDF page by page index. doc.removePage(index); // Remove a specified PDF page. doc.removePage(page); ...
How to flatten a PDF page
import static com.foxit.sdk.pdf.PDFPage.*; ... // Assuming PDFPage page has been loaded and parsed. // Flatten all contents of a PDF page. page.flatten(true, e_FlattenAll); // Flatten a PDF page without annotations. page.flatten(true, e_FlattenNoAnnot); // Flatten a PDF page without form controls. page.flatten(true, e_FlattenNoFormControl); // Flatten a PDF page without annotations and form controls (Equals to nothing to be flattened). page.flatten(true, e_FlattenNoAnnot | e_FlattenNoFormControl); ...
How to get and set page thumbnails in a PDF document
import static com.foxit.sdk.pdf.PDFPage.*; ... // Assuming PDFPage page has been loaded and parsed. ... // Load the thumbnail bitmap. If the return value of function db.isEmpty() is true means no thumbnail can be found. Bitmap db = page.loadThumbnail(); if (!db.isEmpty()) { int dbWidth = db.getWidth(); int dbHeight = db.getHeight(); } // Set page thumbnail, db should be a valid bitmap. page.setThumbnail(db); ...
Updated on April 3, 2019