How to render with Foxit PDF SDK for Android
PDF rendering is done with the Foxit renderer, a graphic engine that is used to render pages to a bitmap or a platform device. Foxit PDF SDK provides APIs to set rendering options/flags. For example, to set a flag to decide whether to render form fields and a signature, whether to draw anti-aliasing images and anti-aliasing paths. To render, you can use the following APIs:
- To render a page and its annotations, first use the function Renderer.setRenderContentFlags to decide whether to render both the page and annotations, and then use the function Renderer.startRender to render. The function Renderer.startQuickRender can also be used to render page but only for thumbnail purposes.
- To render a single annotation, use the function Renderer.renderAnnot.
- To render on a bitmap, use the function Renderer.startRenderBitmap.
- To render a reflowed page, use the function Renderer.startRenderReflowPage.
Widget annotation is always associated with a 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 the page (including widget annotations).
- Then, if you are using 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 specified page to a bitmap
import com.foxit.sdk.PDFException; import com.foxit.sdk.PDFViewCtrl; import com.foxit.sdk.common.Constants; import com.foxit.sdk.common.Progressive; import com.foxit.sdk.common.Renderer; import com.foxit.sdk.common.fxcrt.Matrix2D; import com.foxit.sdk.pdf.PDFDoc; import com.foxit.sdk.pdf.PDFPage; ... public Bitmap renderPageToBitmap(PDFPage pdfPage, int drawPageWidth, int drawPageHeight) { try { // If the page hasn't been parsed yet, throw an exception. if (pdfPage.isParsed()) { throw new PDFException(Constants.e_ErrNotParsed, "PDF Page should be parsed first"); } // Pepare matrix to render on the bitmap. Matrix2D matrix2D = pdfPage.getDisplayMatrix(0, 0, drawPageWidth, drawPageHeight, Constants.e_Rotation0); // Create a bitmap according to the required drawPageWidth and drawPageHeight. Bitmap bitmap = Bitmap.createBitmap(drawPageWidth, drawPageHeight, Bitmap.Config.RGB_565); // Fill the bitmap with white color. bitmap.eraseColor(Color.WHITE); Renderer renderer = new Renderer(bitmap, true); // Set the render flag, both page content and annotation will be rendered. renderer.setRenderContentFlags(Renderer.e_RenderPage | Renderer.e_RenderAnnot); // Start to render the page progressively. Progressive progressive = renderer.startRender(pdfPage, matrix2D, null); int state = Progressive.e_ToBeContinued; while (state == Progressive.e_ToBeContinued) { state = progressive.resume(); } if (state != Progressive.e_Finished) return null; return bitmap; } catch (PDFException e) { e.printStackTrace(); } return null; }
Updated on May 28, 2019