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

    DsExcel .NET allows you to export workbook to 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.

    To save all visible spreadsheets in a workbook to a Portable Document File (PDF), use the Save() method of the IWorkbook interface. Each worksheet in a workbook is saved to a new page in the PDF file. You can also export only the current sheet (active sheet) to PDF format using 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.

    Refer to the following example code to export a spreadsheet to a PDF file. 

    C#
    Copy Code
    //create workbook and add two sheets.
    Workbook workbook = new Workbook();
    IWorksheet sheet1 = workbook.Worksheets[0];
    IWorksheet sheet2 = workbook.Worksheets.Add();
    
    //export workbook to pdf file, the exported file has two pages.
    workbook.Save(@"D:\workbook.pdf", SaveFileFormat.Pdf);
    
    //just export a particular sheet to pdf file
    sheet1.Save(@"D:\sheet1.pdf", SaveFileFormat.Pdf);
    

    DsExcel also supports setting JavaScript in PDF documents by using OpenActionScript property 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.

    C#
    Copy Code
    Workbook workbook = new Workbook();
    workbook.Open("SampleTemplate.xlsx");
    
    workbook.ProcessTemplate();
    PdfSaveOptions options = new PdfSaveOptions();
            
    //Set JavaScript
    options.OpenActionScript = "var fld1 = this.getField(\"num\");" +
    "fld1.value = fld1.value;" +
    "this.dirty = false;";
    
    workbook.Save("SampleTemplate.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:

    While printing the PDF document, you can also configure to choose the paper source automatically based on PDF page size. For more information, please refer to Configure Paper Source.

    Note: The Export to PDF feature doesn't support exporting picture settings (such as LineFormat, FillFormat, Brightness, Contrast, Watermark Color Type and black and white pictures in emf format) to PDF files.