Document Solutions for Excel, Java Edition | Document Solutions
File Operations / Export to PDF / Export Text
In This Topic
    Export Text
    In This Topic

    DsExcel Java allows users to save worksheets with different text formats and font effects while exporting to a PDF file.

    You can save an excel file with different text formats and font effects in the following ways:

    1. Export Number Formats
    2. Export Overflow Text
    3. Export Font Effects

    Export Number Formats

    In order to export an excel file to PDF having text with excel number formatter, refer to the following example code.

    Java
    Copy Code
    // Create a new workbook and access the default worksheet
    Workbook workbook = new Workbook();
    IWorksheet sheet = workbook.getWorksheets().get(0);
            
    // Assign values to the range
    sheet.getRange("B3:B7").setValue(123456.789);
    sheet.getRange("B9:B13").setValue(-123456.789);
            
    // Set number format
    sheet.getRange("B4, B10").setNumberFormat("0.00;[Red]0.00");
    sheet.getRange("B5, B11").setNumberFormat("$#,##0.00;[Red]$#,##0.00");
    sheet.getRange("B6, B12").setNumberFormat("0.00E+00");
    sheet.getRange("B7, B13")
    .setNumberFormat("_($* #,##0.00_);_($* (#,##0.00);_($* \" - \"??_);_(@_)");
    
    // Customize other settings
    sheet.getColumns().get(1).setColumnWidthInPixel(100);
    
    // Save to a pdf file
    workbook.save("ExportNumberFormats.pdf", SaveFileFormat.Pdf);

    Export Overflow Text

    In order to export an excel file to PDF having overflow text, refer to the following example code.

    Java
    Copy Code
    // Settings for overflow text
    sheet.getRange("F2, F4").setValue("This is a test string of overflow");
    sheet.getRange("F6, F8").setValue("This is a test string of overflow with right alignment");
    sheet.getRange("F6, F8").setHorizontalAlignment(HorizontalAlignment.Right);
    sheet.getRange("D8, H4").setValue(123);
    
    // Apply style
    sheet.getRange("A1:J10").getBorders().setLineStyle(BorderLineStyle.Thin);
            
    // Save to a pdf file
    workbook.save("ExportOverflowText.pdf", SaveFileFormat.Pdf);

    Export Font Effects

    In order to export an excel file to PDF having font effects such as text alignment, wordwrap, text indent, shrink to fit, underline and strikethrough, refer to the following example code.

    Java
    Copy Code
    // Set Aligment
    sheet.getRange("A1").setValue("Alignment");
    sheet.getRange("B2").setValue("Left Alignment");
    sheet.getRange("B2").setHorizontalAlignment(HorizontalAlignment.Left);
    sheet.getRange("C2").setValue("Center Alignment");
    sheet.getRange("C2").setHorizontalAlignment(HorizontalAlignment.Center);
    sheet.getRange("D2").setValue("Right Alignment");
    sheet.getRange("D2").setHorizontalAlignment(HorizontalAlignment.Right);
    sheet.getRange("B3").setValue("Top Alignment");
    sheet.getRange("B3").setVerticalAlignment(VerticalAlignment.Top);
    sheet.getRange("C3").setValue("Middle Alignment");
    sheet.getRange("C3").setVerticalAlignment(VerticalAlignment.Center);
    sheet.getRange("D3").setValue("Bottom Alignment");
    sheet.getRange("D3").setVerticalAlignment(VerticalAlignment.Bottom);
    sheet.getRange("B4").setValue(
    "Test String. \nThis is a test string for Justify Alignment. ");
    sheet.getRange("B4").setHorizontalAlignment(HorizontalAlignment.Justify);
    sheet.getRange("B4").setVerticalAlignment(VerticalAlignment.Justify);
    sheet.getRange("C4").setValue(
    "Test String. \nThis is a test string for Distributed Alignment. ");
    sheet.getRange("C4").setHorizontalAlignment(HorizontalAlignment.Distributed);
    sheet.getRange("C4").setVerticalAlignment(VerticalAlignment.Distributed);
            
    // Set wordwrap
    sheet.getRange("A6").setValue("Wordwrap");
    sheet.getRange("B7").setValue("This is a test string for Wordwrap");
    sheet.getRange("C7").setValue("This is a test string \n for Wordwrap");
    sheet.getRange("B7:C7").setWrapText(true);
    
    // Set text indent
    sheet.getRange("A9").setValue("Indent");
    sheet.getRange("B10").setValue("Left Indent");;
    sheet.getRange("B10").setIndentLevel(3);
    sheet.getRange("C10").setValue("Right Indent");
    sheet.getRange("C10").setIndentLevel(3);
    sheet.getRange("C10").setHorizontalAlignment(HorizontalAlignment.Right);
    
    // Apply Shrink to fit
    sheet.getRange("A12").setValue("Shrink to fit");
    sheet.getRange("B13").setValue("This is a test string for \"Shrink to fit\"");
    sheet.getRange("B13").setShrinkToFit(true);
    
    // Set Underline
    sheet.getRange("A15").setValue("Underline");
    sheet.getRange("B16").setValue("Single Underline");
    sheet.getRange("B16").getFont().setUnderline(UnderlineType.Single);
    
    // Use Strikethrough
    sheet.getRange("A18").setValue("Strikthrough");
    sheet.getRange("B19").setValue("Strikthrough");
    sheet.getRange("B19").getFont().setStrikethrough(true);
    
    // Customize other settings
    sheet.getColumns().get(0).getFont().setBold(true);
    sheet.getColumns().get(0).setColumnWidthInPixel(100);
    sheet.getColumns().get(1).setColumnWidthInPixel(200);
    sheet.getColumns().get(2).setColumnWidthInPixel(245);
    sheet.getColumns().get(3).setColumnWidthInPixel(234);
    sheet.getRows().get(2).setRowHeightInPixel(72);
    sheet.getRows().get(3).setRowHeightInPixel(123);
    sheet.getRows().get(6).setRowHeightInPixel(48);
    sheet.getRange("A1:D19").getBorders().setLineStyle(BorderLineStyle.Thin);
    sheet.getPageSetup().setPaperSize(PaperSize.A3);
            
    // Save to a pdf file
    workbook.save("ExportFontEffects.pdf", SaveFileFormat.Pdf);