Document Solutions for Excel, Java Edition | Document Solutions
File Operations / Export to PDF / Configure Fonts and Set Style
In This Topic
    Configure Fonts and Set Style
    In This Topic

    DsExcel Java enables users to configure custom fonts and set styles while saving worksheets in PDF format.

    Before executing the export operation, users need to make sure they specify the font path that should be used while saving the PDF. If the folder path to the font is not specified and the user is working on Windows OS, the path "C:\Windows\Fonts" will be used by default. However, if the folder path to the font is not specified and the user is working on any other operating system, it is necessary that the user sets the font folder path and copies the used font files to it from the folder "C:\Windows\Fonts".

    The getUsedFonts() method of the IWorkbook interface can be used to get the collection of all the fonts used in the workbook.

    While exporting to a PDF file, DsExcel Java uses the fonts specified in the Workbook.FontsFolderPath in order to render the PDF. However, if the used font doesn't exist, it will make use of some fallback fonts. In case, fallback fonts don't exist in the file, DsExcel Java will throw the exception :"There are no available fonts. Please set a valid path to the FontsFolderPath method of the Workbook!"

    In order to configure fonts and set style while saving to a PDF, refer to the following example code.

    Java
    Copy Code
    // Create a new workbook and add worksheets
    Workbook workbook = new Workbook();
    IWorksheet sheet1 = workbook.getWorksheets().get(0);
    IWorksheet sheet2 = workbook.getWorksheets().add();
    
    // Set style.
    sheet1.getRange("A1").setValue("Sheet1");
    sheet1.getRange("A1").getFont().setName("Wide Latin");
    sheet1.getRange("A1").getFont().setColor(Color.GetRed());
    sheet1.getRange("A1").getInterior().setColor(Color.GetGreen());
    
    // Add Table
    ITable table = sheet1.getTables().add(sheet1.getRange("C1:E5"), true);
    sheet2.getRange("A1").setValue("Sheet2");
    
    // Specify font path
    Workbook.FontsFolderPath = "C:\\Users\\GPCTAdmin\\Documents\\Fonts";
    
    // Get the used fonts list in workbook, the list are:"Wide Latin", "Calibri"
    List<FontInfo> fonts = workbook.getUsedFonts();
    
    // Save to a pdf file
    workbook.save("configureFontsAndSetStyle.pdf", SaveFileFormat.Pdf);
    
    // Just export sheet1 to pdf file.
    sheet1.save("configureFontsAndSetStyle_sheet.pdf",SaveFileFormat.Pdf);

    DsExcel Java can also use font streams for PDF export if the user cannot store fonts directly on the disk. DsExcel provides FontProvider field in Workbook class that enables the user to provide font streams for PDF export. The font streams are implemented using IFontProvider interface and its methods getFontFilePaths and getFont.

    getFontFilePaths method returns all the font file paths for Auto Fit, PDF export, and image export, whereas getFont returns the font stream by the font file path. DsExcel will search the font path only in the font streams if the user implements FontProvider; otherwise, DsExcel will search in FontsFolderPath.

    Refer to the following example code to provide fonts using font streams:

    Java
    Copy Code
    // Create a new workbook.
    Workbook workbook = new Workbook();
    IWorksheet sheet = workbook.getWorksheets().get(0);
    
    // Set style of the font.
    sheet.getRange("A1").setValue("Sheet1");
    sheet.getRange("A1").getFont().setName("Arial");
    sheet.getRange("A1").getFont().setColor(Color.GetRed());
    sheet.getRange("A1").getInterior().setColor(Color.GetGreen());
    
    // Implement FontProvider.
    Workbook.FontProvider = new IFontProvider() {
        @Override
        public List<String> getFontFilePaths() {
            return new ArrayList<>(Arrays.asList(
                    "fonts\\arial.ttf",
                    "fonts\\arialbd.ttf",
                    "fonts\\ariali.ttf"
            ));
        }
    
        @Override
        public InputStream getFont(String fontFilePath) {
            return getClass().getClassLoader().getResourceAsStream(fontFilePath);
        }
    };
    
    // Save the workbook.
    workbook.save("FontStreaming.pdf", SaveFileFormat.Pdf);
    Note: DsExcel also supports custom fonts through font streams for image export using the similar code above.

    Note: The Export to PDF feature in DsExcel Java doesn't support saving the following worksheet styles to PDF format:

    a) Usage of double underline, single accounting underline, double accounting underline, superscript font effect, subscript font effect.

    b) Alignment Preferences like center across selection, fill alignment, justify alignment, distributed alignment, orientation and text reading order etc.