How to Render using Foxit PDF SDK (Java)
PDF rendering is realized through the Foxit renderer, a graphics engine that is used to render a page to a bitmap or platform graphics device. Foxit PDF SDK provides APIs to set rendering options/flags. As an example, you can set up a flag to decide whether to render form fields and signature, or whether to draw image anti-aliasing and path anti-aliasing. To render, you can use the following APIs:
- To render page and annotations, first use function setRenderContentFlags to decide whether to render page and annotation both or not, and then use function Renderer.startRender to do the rendering. Function Renderer.startQuickRender can also be used to render page but only for thumbnail purpose.
- To render a single annotation, use function renderAnnot.
- To render on a bitmap, use function startRenderBitmap.
- To render a reflowed page, use function startRenderReflowPage.
Widget annotation is always associated with form field and form control in Foxit PDF SDK. For how to render widget annotations, here is a recommended flow:
- After loading a PDF page, first render the page and all annotations in this page (including widget annotations).
- Then, if use foxit.sdk.pdf.interform.Filler object to fill the form, the function pdf.interform.Filler.render should be used to render the focused form control instead of the function Renderer.renderAnnot.
Example:
How to render a page to a bitmap
import com.foxit.sdk.pdf.PDFDoc; import com.foxit.sdk.pdf.PDFPage; import com.foxit.sdk.common.Bitmap; import com.foxit.sdk.common.Renderer; import com.foxit.sdk.common.fxcrt.Matrix2D; import static com.foxit.sdk.common.Bitmap.e_DIBArgb; // Assuming PDFPage page has been loaded and parsed. int width = (int) page.getWidth(); int height = (int) page.getHeight(); Matrix2D matrix = page.getDisplayMatrix(0, 0, width, height, page.getRotation()); // Prepare a bitmap for rendering. Bitmap bitmap = new Bitmap(width, height, e_DIBArgb, null, 0); bitmap.fillRect(0xFFFFFFFF, null); // Render page. Renderer render = new Renderer(bitmap, false); render.startRender(page, matrix, null); ...
How to render page and annotation
import com.foxit.sdk.pdf.PDFDoc; import com.foxit.sdk.pdf.PDFPage; import com.foxit.sdk.common.Bitmap; import com.foxit.sdk.common.Renderer; import com.foxit.sdk.common.fxcrt.Matrix2D; import static com.foxit.sdk.common.Bitmap.e_DIBArgb; // Assuming PDFPage page has been loaded and parsed. int width = (int) page.getWidth(); int height = (int) page.getHeight(); Matrix2D matrix = page.getDisplayMatrix(0, 0, width, height, page.getRotation()); // Prepare a bitmap for rendering. Bitmap bitmap = new Bitmap(width, height, e_DIBArgb, null, 0); bitmap.fillRect(0xFFFFFFFF, null); Renderer render(bitmap, false); render.setRenderContentFlags(e_RenderAnnot | e_RenderPage); render.startRender(page, matrix, null); ...
Updated on April 9, 2019