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

    DsExcel Java provides you with the facility to export workbook to a PDF file. You can set pagination for each worksheet in the workbook and export it to required pages in a PDF file. You can also apply styles, customize fonts, add security options, configure document properties and adjust row height or column width while performing the export operation.

    Morover, you can export Excel sheets with slicers and sheet background image to PDF document.

    DsExcel Java allows you to save all visible spreadsheets in a workbook to a Portable Document File (PDF) using the save() method of the IWorkbook interface. Each worksheet in a workbook is saved to a new page in the PDF file. However, if you want to export only the current sheet (active sheet) to PDF format, you can use the save() method of the IWorksheet interface.

    The handling of images in the case of PDF export is also very efficient. If a picture is used multiple times in a spreadsheet, DsExcel maintains a single copy of the picture which reduces the size of exported PDF file.

    In order to export a spreadsheet to a PDF file, refer to the following example code.

    Java
    Copy Code
    // Create a new workbook and add worksheets
    Workbook workbook = new Workbook();
    IWorksheet worksheet = workbook.getWorksheets().get(0);
    IWorksheet worksheet1 = workbook.getWorksheets().add();
            
    // Set value and apply styles to the worksheet
    worksheet1.getRange("A1").setValue("Sheet1");
    worksheet1.getRange("A1").getFont().setName("Wide Latin");
    worksheet1.getRange("A1").getFont().setColor(Color.GetRed());
    worksheet1.getRange("A1").getInterior().setColor(Color.GetGreen());
            
    // Export Workbook to pdf file, the exported file has two pages.
    workbook.save("ConvertWorkbookToPDF.pdf", SaveFileFormat.Pdf);
           
    // Just export a particular worksheet to pdf file
    worksheet1.save("ConvertWorksheetToPDF.pdf", SaveFileFormat.Pdf)

    In addition, DsExcel provides PdfSaveOptions class to customize the export of a PDF file. These options are listed below:

    Class Option Description
    Export Options PdfSaveOptions BorderOptions Stores border options when exporting PDF.
    DocumentProperties Represents the document properties of the PDF.
    FileFormat Represents the format in which the workbook is saved.
    FormFields Indicates whether to replace Excel form controls with PDF form fields. Not all controls and properties are supported.
    ImageQuality Sets the image quality in percent. This value must be between 0 (lowest quality, maximum compression) and 100 (highest quality, no compression). The default value is 75.
    OpenActionScript Sets the JavaScript to be executed when the saved PDF file is opened.
    PrintBackgroundPicture Indicates whether to print the sheet's background image on the page.
    PrintTransparentCell Indicates whether to print the transparency of the cell's background color on the page.
    SecurityOptions Represents the security settings of PDF.
    ShrinkToFitSettings The settings about performing shrink to fit on the wrapped text.
    ViewerPreferences The settings that contain information specifying how the current document should be displayed.
    IncludeAutoMergedCells Indicates whether to include the automatically merged cells. The default value is false.

    DsExcel also supports setting JavaScript in PDF documents by using setOpenActionScript method of PdfSaveOptions class. The JavaScript is executed when the saved PDF document is opened.

    Refer to the following example code to set JavaScript in an Excel template which is processed to create a PDF form.

    Java
    Copy Code
    Workbook workbook = new Workbook();
    workbook.open("D:\\SampleTemplate.xlsx");
      
    workbook.processTemplate();
      
    PdfSaveOptions options = new PdfSaveOptions();
    options.setOpenActionScript("var fld1 = this.getField(\"num\");" +
    "fld1.value = fld1.value;" +
    "this.dirty = false;");
      
    workbook.save("SampleTemplate_java.pdf", options);

    While executing the export operation, you can configure fonts, set style and specify the page setup options in order to customize the PDF as per your preferences. Refer to the following topics for more details:

    Limitations