Skip to main content Skip to footer

SpreadJS and Saving To PDF Files

PDF files are popular because they can be viewed in many different operating systems. You can save to PDF with SpreadJS. There are several ways to create a PDF file with SpreadJS. You can use the Excel Import and Export component to save to a PDF file using the following steps:

  1. Create a Visual Studio project using these steps: http://sphelp.grapecity.com/2015/12/16/spreadjs-excel-import-and-export/. For more information, refer to: http://sphelp.grapecity.com/webhelp/SpreadJSWeb/webframe.html#excelservice.html in the on-line help.
  2. After you create the Visual Studio project, add a button named SavePDF and this additional code:

    private void SavePDF_Click(object sender, EventArgs e)  
            {  
                PdfExportSettings pdfs = new PdfExportSettings();  
                pdfs.Title = "PDF Test";  
                pdfs.DisplayDocTitle = true;  
    
                saveFileDialog1.Filter = "PDF (*.pdf)|*.pdf";  
                saveFileDialog1.FilterIndex = 1;  
    
                DialogResult result = this.saveFileDialog1.ShowDialog();  
    
                if (result == DialogResult.OK)  
                {                  
                    using (FileStream fs = File.Create(this.saveFileDialog1.FileName))  
                    {                     
                        Exporter exporter = new Exporter(this.richTextBox1.Text);                      
                        exporter.SavePdf(fs, pdfs, 0);  
                    }  
                }  
            }  
    
    
  3. Run the project, load a file using the import button, and then use the SavePDF button to save to a PDF file.

Another way to print to PDF is to use the browser print options with an installed PDF print driver. SpreadJSBrowserPrint Browser Print Option You can also use the SpreadJS print method and select a PDF printer option. The following example uses the SpreadJS print method. SpreadJSPrintWithPDFdriver PDF File

var datasource = [  
{ Name: "Apple", Category: "Fruit" },  
{ Name: "Orange", Category: "Fruit" },  
{ Name: "Broccoli", Category: "Vegetable" },  
{ Name: "Kiwi", Category: "Fruit" },  
{ Name: "Rice", Category: "Cereal" },  
{ Name: "Strawberry", Category: "Fruit" },  
{ Name: "Yogurt", Category: "Dairy" },  
{ Name: "Plum", Category: "Fruit" },  
{ Name: "Celery", Category: "Vegetable" },  
{ Name: "Grape", Category: "Fruit" },  
{ Name: "Oats", Category: "Cereal" },  
{ Name: "Quinoa", Category: "Cereal" },  
{ Name: "Maize", Category: "Cereal" },  
{ Name: "Okra", Category: "Vegetable" },  
{ Name: "Corn", Category: "Vegetable" },  
{ Name: "Wheat", Category: "Cereal" },  
{ Name: "Barley", Category: "Cereal" },  
{ Name: "Cream", Category: "Dairy" },  
{ Name: "Millet", Category: "Cereal" },  
{ Name: "Rye", Category: "Cereal" },  
{ Name: "Artichoke", Category: "Vegetable" },  
{ Name: "Buckwheat", Category: "Cereal" },  
{ Name: "Gooseberry", Category: "Fruit" },  
{ Name: "Amaranth", Category: "Cereal" },  
{ Name: "Carrot", Category: "Vegetable" },  
{ Name: "Cheese", Category: "Dairy" },  
{ Name: "Fig", Category: "Fruit" },  
{ Name: "Milk", Category: "Dairy" },  
{ Name: "Butter", Category: "Dairy" },  
               ];  

              activeSheet.setDataSource(datasource);   

$("#button1").click(function () {  
spread.print(0);  
   });  

Earlier versions of SpreadJS used an Excel service to import and export Excel files. This service also exported to PDF files. You can use the following help information to use the older Excel service, http://sphelp.grapecity.com/webhelp/SpreadJSWeb/webframe.html#excelioprevious.html. The following example exports to PDF using the Excel Import and Export service.

var dataObj = {  
    "spread": spread.toJSON(),  
    "exportFileType": "pdf"  
};  
dataObj.pdf = {"setting":{}, "sheetIndexes":[0, 2]};  
dataObj.pdf.setting.author = "Jone";  
dataObj.pdf.setting.pageLayout = 2; // same as dataObj.pdf.setting.pageLayout = "OneColumn";  
dataObj.pdf.setting.printPreset = {};  
dataObj.pdf.setting.printPreset.duplexMode = "DuplexFlipShortEdge";  
dataObj.pdf.setting.printPreset.printRanges = [{ "index": 1, "count": 1 }, { "index": 2, "count": 3 }];  

You can also use the SpreadJS designer at design time to save to a PDF file. SpreadJSPrintDesigner SpreadJS Designer

MESCIUS inc.

comments powered by Disqus