Back to Blog

    Developer Blog

    How to Render using Foxit PDF SDK (C++)

    Foxit PDF SDK for Windows

    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 Renderer::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 Renderer::RenderAnnot.
    • To render on a bitmap, use function Renderer::StartRenderBitmap.
    • To render a reflowed page, use function Renderer::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 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

    #include "include/common/fs_common.h"
    #include "include/pdf/fs_pdfdoc.h"
    #include "include/pdf/fs_pdfpage.h"
    #include "include/common/fs_render.h"
    
    using namespace foxit;
    using namespace common;
    using namespace pdf;
    using namespace foxit::common;
    
    // Assuming PDFPage page has been loaded and parsed.
    int width = static_cast(page.GetWidth());
    int height = static_cast(page.GetHeight());
    Matrix matrix = page.GetDisplayMatrix(0, 0, width, height, page.GetRotation());
    
    // Prepare a bitmap for rendering.
    Bitmap bitmap(width, height, Bitmap::e_DIBArgb, NULL, 0);
    bitmap.FillRect(0xFFFFFFFF, NULL);
    
    // Render page.
    Renderer render(bitmap, false);
    render.StartRender(page, matrix, NULL);
    ...
    

    How to render page and annotation

    #include "include/common/fs_common.h"
    #include "include/pdf/fs_pdfdoc.h"
    #include "include/pdf/fs_pdfpage.h"
    #include "include/common/fs_render.h"
    
    using namespace foxit;
    using namespace common;
    using namespace pdf;
    using namespace foxit::common;
    
    // Assuming PDFPage page has been loaded and parsed.
    int width = static_cast(page.GetWidth());
    int height = static_cast(page.GetHeight());
    Matrix matrix = page.GetDisplayMatrix(0, 0, width, height, page.GetRotation());
    
    // Prepare a bitmap for rendering.
    Bitmap bitmap(width, height, Bitmap::e_DIBArgb, NULL, 0);
    bitmap.FillRect(0xFFFFFFFF, NULL);
    
    Renderer render(bitmap, false);
    uint32 dwRenderFlag = Renderer::e_RenderAnnot | Renderer::e_RenderPage;
    render.SetRenderContentFlags(dwRenderFlag);
    render.StartRender(page, matrix, NULL);
    ...