[]
        
(Showing Draft Content)

IPDFRenderEngine

Interface IPDFRenderEngine


public interface IPDFRenderEngine
Provides a custom rendering engine for saving a workbook as PDF.

Implement this interface when you want to render the PDF with your own engine. Set the implementation through PdfSaveOptions.setPDFRenderEngin(IPDFRenderEngine) before saving the workbook as PDF.


 Workbook workbook = new Workbook();
 workbook.getActiveSheet().getRange("A1").setValue("Quarterly Report");
 class CustomPdfRenderEngine implements IPDFRenderEngine {
     private final Map<FontInfo, FontInfo> fontMapping = new HashMap<>();
     private boolean renderedWorkbook;

     public void addFontMapping(String workbookFontName, String replacementFontName) {
         FontInfo workbookFont = new FontInfo();
         workbookFont.name = workbookFontName;
         FontInfo replacementFont = new FontInfo();
         replacementFont.name = replacementFontName;
         fontMapping.put(workbookFont, replacementFont);
     }

     public boolean hasRenderedWorkbook() {
         return renderedWorkbook;
     }

     @Override
     public void render(Object... parameters) {
         renderedWorkbook = parameters[0] instanceof IWorkbook;
     }

     @Override
     public Object getEngineFontMapping() {
         return fontMapping;
     }
 }
 CustomPdfRenderEngine renderEngine = new CustomPdfRenderEngine();
 renderEngine.addFontMapping("Calibri", "Arial");
 PdfSaveOptions options = new PdfSaveOptions();
 options.setPDFRenderEngin(renderEngine);
 workbook.save(new ByteArrayOutputStream(), options);
 boolean rendered = renderEngine.hasRenderedWorkbook();
 
  • Method Summary

    Modifier and Type
    Method
    Description
    Gets the font mapping used by the custom PDF render engine.
    void
    render(Object... parameters)
    Renders the workbook to the target PDF stream.
  • Method Details

    • render

      void render(Object... parameters)
      Renders the workbook to the target PDF stream.

      When the workbook is saved as PDF, this method receives the workbook, font information, and destination output stream through the parameters array.

      
       Workbook workbook = new Workbook();
       workbook.getActiveSheet().getRange("A1").setValue("Quarterly Report");
       class CustomPdfRenderEngine implements IPDFRenderEngine {
           private IWorkbook renderedWorkbook;
      
           public IWorkbook getRenderedWorkbook() {
               return renderedWorkbook;
           }
      
           @Override
           public void render(Object... parameters) {
               renderedWorkbook = (IWorkbook) parameters[0];
           }
      
           @Override
           public Object getEngineFontMapping() {
               return null;
           }
       }
       CustomPdfRenderEngine renderEngine = new CustomPdfRenderEngine();
       PdfSaveOptions options = new PdfSaveOptions();
       options.setPDFRenderEngin(renderEngine);
       workbook.save(new ByteArrayOutputStream(), options);
       IWorkbook exportedWorkbook = renderEngine.getRenderedWorkbook();
       
      Parameters:
      parameters - The workbook, font information, and destination output stream passed when saving as PDF.
    • getEngineFontMapping

      Object getEngineFontMapping()
      Gets the font mapping used by the custom PDF render engine.

      Return a Map<FontInfo, FontInfo> to replace workbook fonts with fonts used by the custom render engine.

      
       class CustomPdfRenderEngine implements IPDFRenderEngine {
           private final Map<FontInfo, FontInfo> fontMapping = new HashMap<>();
      
           public void addFontMapping(String workbookFontName, String replacementFontName) {
               FontInfo workbookFont = new FontInfo();
               workbookFont.name = workbookFontName;
               FontInfo replacementFont = new FontInfo();
               replacementFont.name = replacementFontName;
               fontMapping.put(workbookFont, replacementFont);
           }
      
           @Override
           public void render(Object... parameters) {
           }
      
           @Override
           public Object getEngineFontMapping() {
               return fontMapping;
           }
       }
       CustomPdfRenderEngine renderEngine = new CustomPdfRenderEngine();
       renderEngine.addFontMapping("Calibri", "Arial");
       Map<FontInfo, FontInfo> fontMapping =
           (Map<FontInfo, FontInfo>) renderEngine.getEngineFontMapping();
       
      Returns:
      The font mapping used by the custom PDF render engine.