left-icon

Writing Native Mobile Apps in a Functional Language Succinctly®
by Vassili Kaplan

Previous
Chapter

of
A
A
A

CHAPTER 6

Adding PDF, Word, and Excel Functionality from Syncfusion to CSCS

Adding PDF, Word, and Excel Functionality from Syncfusion to CSCS


 “A guy who builds a nice chair doesn’t owe money to everyone who ever has built a chair.”

Mark Zuckerberg

In this chapter, we are going to see how to create different types of documents using the Syncfusion framework. Namely, we are going to see how to create PDF, Microsoft Word, and Excel files.

One of the differences with the controls that we developed in the previous chapter is that the Syncfusion implementation for iOS and Android is almost the same (including the class and the method names). Since the platform-specific code is minimal, this allows us to use the same code for both platforms, putting the Syncfusion wrappers in the shared project.

Adding PDF file creation to CSCS

To add a Syncfusion widget for PDF (Portable Document Format) file creation, we extend the iOSVariable class on iOS, and the DroidVariable class on Android. Check out the details in Code Listing 34.

Code Listing 34: A Fragment of the SfPdf Class

#if __ANDROID__
using scripting.Droid;
using BASE_VARIABLE = scripting.Droid.DroidVariable;
using UTILS = scripting.Droid.UtilsDroid;
#elif __IOS__
using scripting.iOS;
using BASE_VARIABLE = scripting.iOS.iOSVariable;
using UTILS = scripting.iOS.UtilsiOS;
#endif

namespace scripting
{
  public class SfPdf : BASE_VARIABLE
  {
    PdfDocument m_document;
    PdfLoadedDocument m_loadedDoc;
    PdfPage m_page;
    PdfGraphics m_grapics;
    PdfStandardFont m_font = new PdfStandardFont(

             PdfFontFamily.Helvetica, 12);

    public void Init()
    {
      if (m_document != null) {
        return;
      }
      m_document = new PdfDocument();
      m_page = m_document.Pages.Add();
      m_grapics = m_page.Graphics;
    }

    public void AddText(string text, int x, int y, PdfBrush color)
    {
      m_grapics.DrawString(text, m_font, color, x, y);
    }

    public void AddImage(string imagePath, int x, int y, int w, int h)
    {
      Stream pngImageStream = UTILS.ImageToStream(imagePath);
      PdfImage pngImage = new PdfBitmap(pngImageStream);
      m_grapics.DrawImage(pngImage, x, y, w, h);
    }
    

    public void AddLine(int x1, int y1, int x2, int y2,

                        PdfBrush color, float width)
    {
      PdfPen pen = new PdfPen(color, width);
      m_grapics.DrawLine(pen, x1, y1, x2, y2);
    }
    

    public void AddRectangle(int x, int y, int w, int h, PdfBrush color)
    {
      m_grapics.DrawRectangle(color, x, y, w, h);
    }
    

    public void AddPie(int x, int y, int w, int h, float startAngle,
                       float sweepAngle, PdfBrush color, float width)
    {
      PdfPen pen = new PdfPen(color, width);
      m_grapics.DrawPie(pen, x, y, w, h, startAngle, sweepAngle);
    }


    public void Save(string filename)
    {
      MemoryStream stream = new MemoryStream();
      if (m_loadedDoc != null) {
        m_loadedDoc.Save(stream);
      } else {
        Init();
        m_document.Save(stream);
        m_document.Close(true);
      }
      stream.Position = 0;
#if __ANDROID__
      SaveAndroid.Save(filename, "application/pdf", stream);
#elif __IOS__
      PreviewController.Save(filename, "application/pdf", stream);
#endif
      m_document = null;
      Utils.SaveFile(filename, stream);
    }
  }
}

To register all the Syncfusion PDF-related functions with the parser, we use the following statements:

ParserFunction.RegisterFunction("SfPdfNew"new CreatePdf());
ParserFunction.RegisterFunction("SfPdfOpen"new OpenPdf());
ParserFunction.RegisterFunction("SfSetPdfText"new SetPdfText());
ParserFunction.RegisterFunction("SfSetPdfImage"new SetPdfImage());
ParserFunction.RegisterFunction("SfSetPdfLine"new SetPdfLine());
ParserFunction.RegisterFunction("SfSetPdfRectangle"new SetPdfRectangle());
ParserFunction.RegisterFunction("SfSetPdfPie"new SetPdfPie());
ParserFunction.RegisterFunction("SfSetPdfFont"new SetPdfFont());
ParserFunction.RegisterFunction("SfSavePdf"new SavePdf());

Tip: All the classes deriving from the ParserFunction class are the glue between the CSCS parser and our C# wrapper over the Syncfusion classes.

See fragments of some of them in Code Listing 35.

Code Listing 35: Fragments of the CreatePdf and SetPdfText Classes

public class CreatePdf : ParserFunction
{
  protected override Variable Evaluate(ParsingScript script)
  {
    List<Variable> args = script.GetFunctionArgs();

    Utils.CheckArgs(args.Count, 0, m_name);

    SfPdf pdf = new SfPdf(true);
    return pdf;
  }
}

public class SetPdfText : ParserFunction
{
  protected override Variable Evaluate(ParsingScript script)
  {
    List<Variable> args = script.GetFunctionArgs();

    Utils.CheckArgs(args.Count, 3, m_name);

    SfPdf pdf = args[0as SfPdf;
    Utils.CheckNotNull(pdf, m_name);

    string text = Utils.GetSafeString(args, 1);
    int x = Utils.GetSafeInt(args, 2);
    int y = Utils.GetSafeInt(args, 3);

    string colorStr = Utils.GetSafeString(args, 4"black");
    PdfBrush color = SfUtils.String2PdfColor(colorStr);

    pdf.Init();
    pdf.AddText(text, x, y, color);

    ParserFunction.UpdateFunction(pdf);
    return pdf;
  }
}

Now you can create PDF files with a few lines of CSCS code: check out Code Listing 36.

Code Listing 36: The CSCS Code to Create a PDF File

pdf = SfPdfNew();

SfSetPdfFont(pdf, "Helvetica"24true);
SfSetPdfText(pdf, "Take a look at this dog:"010"blue");

SfSetPdfLine(pdf, 05064050"green"4);
SfSetPdfImage(pdf, "funnyDog"070640420);
SfSetPdfLine(pdf, 0520640520"green"4);

SfSavePdf(pdf, "hello.pdf");

The result of running Code Listing 36 is shown in Figure 21.

A Sample PDF File Created Using Syncfusion

Figure 21: A Sample PDF File Created Using Syncfusion

Adding Microsoft Word file creation to CSCS

Adding the Syncfusion widget for Microsoft Word is very similar to the PDF widget. See Code Listing 37 for a fragment of the SfWord class. This class is a wrapper over the Syncfusion Word widget.

Code Listing 37: A Fragment of the SfWord Class

public class SfWord : BASE_VARIABLE
{
  WordDocument m_document;
  WSection m_section;
  IWParagraph m_paragraph;

  IWTable m_table;
  WTableRow m_row;
  float m_margins;


  void Init()
  {
    if (m_document != null) {
      return;
    }

    m_document = new WordDocument();
    m_section = m_document.AddSection() as WSection;
    m_section.PageSetup.Margins.All = m_margins;
    AddStyles();
  }
  void CheckParagraph()
  {
    if (m_paragraph == null) {
      m_paragraph = m_section.AddParagraph();
    }
  }

  public void AddTable(int rows, int cols, string styleStr)
  {
    m_table = m_section.AddTable();
    m_table.ResetCells(rows, cols);
    m_table.TableFormat.Borders.BorderType =

            SfUtils.String2BorderStyle(styleStr);
    m_table.TableFormat.IsAutoResized = true;
  }
  public void AddText(string text)
  {
    CheckParagraph();
    m_paragraph.AppendText(text);
  }
}

To register the Word-specific functions with the parser, we use the statements in Code Listing 38.

Code Listing 38: Registration of the Syncfusion Word Classes with the Parser

ParserFunction.RegisterFunction("SfWordNew"new CreateWord());
ParserFunction.RegisterFunction("SfWordOpen"new OpenWord());
ParserFunction.RegisterFunction("SfAddWordText"new AddWordText());
ParserFunction.RegisterFunction("SfAddWordTextRange",

                                new AddWordTextRange());
ParserFunction.RegisterFunction("SfAddWordImage"new AddWordImage());
ParserFunction.RegisterFunction("SfAddWordTable"new AddWordTable());
ParserFunction.RegisterFunction("SfAddWordParagraph",

                                new AddWordParagraph());
ParserFunction.RegisterFunction("SfApplyWordStyle"new ApplyWordStyle());
ParserFunction.RegisterFunction("SfSaveWord"new SaveWord());

We won’t show the implementation of the CreateWord(), OpenWord(), and other methods, since they are very similar to Code Listing 35 for PDF documents. Using the functions defined in Code Listing 38, it is easy to create non-trivial Word documents with just a few lines of CSCS code. Code Listing 39 contains some CSCS code to create a Word document containing images and a table using different colors, fonts, and styles.

Code Listing 39: CSCS Code to Create a Microsoft Word Document

word = SfWordNew();

SfAddWordParagraph(word, "header");
SfApplyWordStyle(word, "Normal2""left");
SfAddWordTextRange(word, "Adventure Cycles""Calibri"12"red");

SfAddWordImage(word, "AdventureCycle"243.5, -24"InFrontOfText",
                     "Column""Milesargin"2015);
SfAddWordParagraph(word);
SfAddWordParagraph(word);
SfApplyWordStyle(word, "Heading 1""center");
SfAddWordTextRange(word, "Adventure Works Cycles""Calibri"18);

SfAddWordParagraph(word, "normal"36);
SfAddWordTextRange(word, "In 2000, Adventure Works Cycles bought a small manufacturing plant, Importadores Neptuno...""Calibri"12);

SfAddWordParagraph(word);
SfAddWordParagraph(word);
SfApplyWordStyle(word, "Heading 1""left");
SfAddWordTextRange(word, "Product Overview""Calibri"16);
SfAddWordParagraph(word);

SfAddWordTable(word, 32"None");
SfAddWordParagraph(word, "table"000);
SfAddWordImage(word, "Mountain200", -50"TopAndBottom",
                     "Column""Paragraph"7575);
SfAddWordParagraph(word);
SfAddWordParagraph(word, "table"001);
SfApplyWordStyle(word, "Heading 1");
SfAddWordText(word, "Mountain-200");
SfAddWordParagraph(word, "table"001);
SfAddWordTextRange(word, "Product No: BK-M68B-38\n",    

                         "Times New Roman"12);
SfAddWordTextRange(word, "Size: 38\n""Times New Roman"12);
SfAddWordTextRange(word, "Weight: 25\n""Times New Roman"12);
SfAddWordTextRange(word, "Price: $2,294.99\n""Times New Roman"12);
SfAddWordParagraph(word, "table"001);

SfAddWordParagraph(word, "table"010);
SfAddWordParagraph(word, "table"010);
SfApplyWordStyle(word, "Heading 1");
SfAddWordText(word, "Mountain-300");

SfAddWordParagraph(word, "table"010);
SfAddWordTextRange(word, "Product No: BK-M47B-38\n"

                         "Times New Roman"12);
SfAddWordTextRange(word, "Size: 35\n""Times New Roman"12);
SfAddWordTextRange(word, "Weight: 22\n""Times New Roman"12);
SfAddWordTextRange(word, "Price: $1,089.99\n""Times New Roman"12);
SfAddWordParagraph(word, "table"010);

SfAddWordParagraph(word, "table"011);
SfAddWordParagraph(word, "table"011);
SfAddWordImage(word, "Mountain300", -158"TopAndBottom",
                     "Column""Paragraph"7575);
SfAddWordParagraph(word, "table"020);
SfAddWordParagraph(word, "table"020);
SfApplyWordStyle(word, "Heading 1");
SfAddWordImage(word, "Road550W", -50"TopAndBottom",
                     "Column""Paragraph"7575);
SfAddWordParagraph(word, "table"021);
SfApplyWordStyle(word, "Heading 1");
SfAddWordText(word, "Road-150");
SfAddWordParagraph(word, "table"021);
SfAddWordTextRange(word, "Product No: BK-R93R-44\n",

                         "Times New Roman"12);
SfAddWordTextRange(word, "Size: 44\n""Times New Roman"12);
SfAddWordTextRange(word, "Weight: 14\n""Times New Roman"12);
SfAddWordTextRange(word, "Price: $3,599.99\n""Times New Roman"12);

SfAddWordParagraph(word, "table"021);
SfApplyWordStyle(word, "Heading 1");

SfSaveWord(word, "hello.docx");

The result of running Code Listing 39 is shown in Figure 22.

A Word Document Created by CSCS Using Syncfusion

Figure 22: A Word Document Created by CSCS Using Syncfusion

Adding Microsoft Excel file creation to CSCS

Adding the Syncfusion widget for the Microsoft Excel file creation is very similar to the PDF and Word file creation as well. See Code Listing 40 for a fragment of the SfExcel class. This class is a wrapper over the Syncfusion Excel widget.

Code Listing 40: A Fragment of the SfExcel Class

public class SfExcel : BASE_VARIABLE
{
  ExcelEngine m_excelEngine;
  IApplication m_application;
  IWorkbook m_workbook;
  IWorksheet m_sheet;
  int m_numberSheets;

  public void Init()
  {
    if (m_excelEngine != null) {
      return;
    }
    m_excelEngine = new ExcelEngine();
    m_application = m_excelEngine.Excel;
    m_application.DefaultVersion = ExcelVersion.Excel2013;

    m_workbook = m_application.Workbooks.Create(m_numberSheets);
    m_workbook.Version = ExcelVersion.Excel2013;

    m_sheet = m_workbook.Worksheets[0];
    m_sheet.EnableSheetCalculations();
  }
  public void AddChart(string range, string title, int top, int bottom,

                       int left, int right)
  {
    IChartShape chart = m_sheet.Charts.Add();
    chart.DataRange = m_sheet[range];
    chart.ChartTitle = title;
    chart.HasLegend = false;
    chart.TopRow = top;
    chart.LeftColumn = left;
    chart.RightColumn = right;
    chart.BottomRow = bottom;
  }
  public void AddWorksheet(string title)
  {
    m_sheet = m_workbook.Worksheets.Create(title);
  }
  public void SetWorksheetName(string title)
  {
    m_sheet.Name = title;
  }
  public void ActivateWorksheet(string title)
  {
    m_sheet = m_workbook.Worksheets[title];
  }

}

Code Listing 41 shows the statements to register the Excel-specific functions with the parser.

Code Listing 41: Registration of the Syncfusion Excel Classes with the Parser

ParserFunction.RegisterFunction("SfExcelNew"new CreateExcel());
ParserFunction.RegisterFunction("SfExcelOpen"new OpenExcel());
ParserFunction.RegisterFunction("SfExcelSet"new SetExcelOption());
ParserFunction.RegisterFunction("SfAddExcelWorksheet",

                                new AddExcelWorksheet());
ParserFunction.RegisterFunction("SfRenameExcelWorksheet",

                                new RenameExcelWorksheet());
ParserFunction.RegisterFunction("SfActivateExcelWorksheet",

                                new ActivateExcelWorksheet());
ParserFunction.RegisterFunction("SfAddExcelChart"new AddExcelChart());
ParserFunction.RegisterFunction("SfSaveExcel"new SaveExcel());

We won’t show the implementation of the CreateExcel(), OpenExcel(), and other methods, since they are very similar to Code Listing 35 for PDF.

Using the functions defined in Code Listing 41, it is easy to create non-trivial Excel documents with just a few lines of CSCS code.

Code Listing 42 contains some CSCS code to create an Excel document with charts and formulas using different colors, fonts, and styles.

Code Listing 42: CSCS Code to Create a Microsoft Word Document

excel = SfExcelNew();

SfRenameExcelWorksheet(excel, "Expenses");
SfAddExcelWorksheet(excel, "Extras");

SfExcelSet(excel, "A2""col_width"18);
SfExcelSet(excel, "B2""col_width"12);
SfExcelSet(excel, "C2""col_width"12);
SfExcelSet(excel, "D2""col_width"12);

SfExcelSet(excel, "A2:D2""merge",  true);
SfExcelSet(excel, "A2""text",       "Expense Report");
SfExcelSet(excel, "A2""font_name",  "Verdana");
SfExcelSet(excel, "A2""font_color""sky_blue");
SfExcelSet(excel, "A2""font_size",  28);
SfExcelSet(excel, "A2""bold"true);
SfExcelSet(excel, "A2""horizontal_alignment""center");
SfExcelSet(excel, "A2""row_height",     34);

SfExcelSet(excel, "A4""text",           "Employee");
SfExcelSet(excel, "B4""text",           "Roger Federer");
SfExcelSet(excel, "A4:B7""font_name",   "Verdana");
SfExcelSet(excel, "A4:B7""font_size",   11);
SfExcelSet(excel, "A4:B7""bold",        true);
SfExcelSet(excel, "A4:A7""horizontal_alignment""left");
SfExcelSet(excel, "B4:B7""horizontal_alignment""right");

SfExcelSet(excel, "B4:D4""merge",       true);

SfExcelSet(excel, "A9:D20""font_name",  "Verdana");
SfExcelSet(excel, "A9:D20""font_size",  11);

SfExcelSet(excel, "A5""text",           "Department");
SfExcelSet(excel, "B5""text",           "Administration");
SfExcelSet(excel, "B5:D5""merge",       true);

SfExcelSet(excel, "A6""text",           "Week Ending");
SfExcelSet(excel, "B6""number_format",  "m/d/yyyy");
SfExcelSet(excel, "B6""date_time",      "12/12/2012");
SfExcelSet(excel, "B6:D6""merge",       true);

SfExcelSet(excel, "A7""text",           "Mileage Rate");
SfExcelSet(excel, "B6""number_format",  "$#,##0.00");
SfExcelSet(excel, "B7""number",         0.7);
SfExcelSet(excel, "B7:D7""merge",       true);

SfExcelSet(excel, "A10""text",           "Miles Driven");
SfExcelSet(excel, "A11""text",           "Reimbursement");
SfExcelSet(excel, "A12""text",           "Parking/Tolls");
SfExcelSet(excel, "A13""text",           "Auto Rental");
SfExcelSet(excel, "A14""text",           "Lodging");
SfExcelSet(excel, "A15""text",           "Breakfast");
SfExcelSet(excel, "A16""text",           "Lunch");
SfExcelSet(excel, "A17""text",           "Dinner");
SfExcelSet(excel, "A18""text",           "Snacks");
SfExcelSet(excel, "A19""text",           "Others");
SfExcelSet(excel, "A20""text",           "Total");
SfExcelSet(excel, "A20:D20""color",      "yellow");
SfExcelSet(excel, "A20:D20""font_color""black");
SfExcelSet(excel, "A20:D20""bold",       true);

SfExcelSet(excel, "B9:D9""horizontal_alignment",     "right");
SfExcelSet(excel, "B9:D9""vertical_alignment",       "center");
SfExcelSet(excel, "B9:D9""color",                    "light_green");
SfExcelSet(excel, "B9:D9""bold",                     true);
SfExcelSet(excel, "B9:D9""font_color",               "white");

SfExcelSet(excel, "A9""text",           "Expenses");
SfExcelSet(excel, "A9""color",          "pink");
SfExcelSet(excel, "A9""font_color",     "white");
SfExcelSet(excel, "A9""bold",           true);

SfExcelSet(excel, "B9""text",           "Day 1");
SfExcelSet(excel, "B10""number",         80);
SfExcelSet(excel, "B11""number_format""$#,##0.00");
SfExcelSet(excel, "B11""formula",       "=(B7*B10)");
SfExcelSet(excel, "B12""number_format",  "$#,##0.00");
SfExcelSet(excel, "B12""number",         "11");
SfExcelSet(excel, "B13""number_format",  "$#,##0.00");
SfExcelSet(excel, "B13""number",         "9");
SfExcelSet(excel, "B14""number_format",  "$#,##0.00");
SfExcelSet(excel, "B14""number",         "13");
SfExcelSet(excel, "B15""number_format",  "$#,##0.00");
SfExcelSet(excel, "B15""number",         "7");
SfExcelSet(excel, "B16""number_format",  "$#,##0.00");
SfExcelSet(excel, "B16""number",         "21");
SfExcelSet(excel, "B17""number_format",  "$#,##0.00");
SfExcelSet(excel, "B17""number",         "22");
SfExcelSet(excel, "B18""number_format",  "$#,##0.00");
SfExcelSet(excel, "B18""number",         "11");
SfExcelSet(excel, "B19""number_format",  "$#,##0.00");
SfExcelSet(excel, "B19""number",         "5");
SfExcelSet(excel, "B20""number_format",  "$#,##0.00");
SfExcelSet(excel, "B20""formula",        "=SUM(B11:B19)");

SfExcelSet(excel, "C9""text",           "Day 2");
SfExcelSet(excel, "C10""number",         120);
SfExcelSet(excel, "C11""number_format""$#,##0.00");
SfExcelSet(excel, "C11""formula",       "=(B7*B10)");
SfExcelSet(excel, "C12""number_format",  "$#,##0.00");
SfExcelSet(excel, "C12""number",         "21");
SfExcelSet(excel, "C13""number_format",  "$#,##0.00");
SfExcelSet(excel, "C13""number",         "17");
SfExcelSet(excel, "C14""number_format",  "$#,##0.00");
SfExcelSet(excel, "C14""number",         "13");
SfExcelSet(excel, "C15""number_format",  "$#,##0.00");
SfExcelSet(excel, "C15""number",         "7");
SfExcelSet(excel, "C16""number_format",  "$#,##0.00");
SfExcelSet(excel, "C16""number",         "24");
SfExcelSet(excel, "C17""number_format",  "$#,##0.00");
SfExcelSet(excel, "C17""number",         "22");
SfExcelSet(excel, "C18""number_format",  "$#,##0.00");
SfExcelSet(excel, "C18""number",         "18");
SfExcelSet(excel, "C19""number_format",  "$#,##0.00");
SfExcelSet(excel, "C19""number",         "15");
SfExcelSet(excel, "C20""number_format",  "$#,##0.00");
SfExcelSet(excel, "C20""formula",        "=SUM(C11:C19)");

SfExcelSet(excel, "D9""text",            "Day 3");
SfExcelSet(excel, "D10""number",         80);
SfExcelSet(excel, "D11""number_format""$#,##0.00");
SfExcelSet(excel, "D11""formula",       "=(B7*B10)");
SfExcelSet(excel, "D12""number_format",  "$#,##0.00");
SfExcelSet(excel, "D12""number",         "13");
SfExcelSet(excel, "D13""number_format",  "$#,##0.00");
SfExcelSet(excel, "D13""number",         "9");
SfExcelSet(excel, "D14""number_format",  "$#,##0.00");
SfExcelSet(excel, "D14""number",         "13");
SfExcelSet(excel, "D15""number_format",  "$#,##0.00");
SfExcelSet(excel, "D15""number",         "17");
SfExcelSet(excel, "D16""number_format",  "$#,##0.00");
SfExcelSet(excel, "D16""number",         "21");
SfExcelSet(excel, "D17""number_format",  "$#,##0.00");
SfExcelSet(excel, "D17""number",         "22");
SfExcelSet(excel, "D18""number_format",  "$#,##0.00");
SfExcelSet(excel, "D18""number",         "14");
SfExcelSet(excel, "D19""number_format",  "$#,##0.00");
SfExcelSet(excel, "D19""number",         "15");
SfExcelSet(excel, "D20""number_format",  "$#,##0.00");
SfExcelSet(excel, "D20""formula",        "=SUM(D11:D19)");

SfExcelSet(excel, "A10:D10""font_color""dark_blue");

SfAddExcelChart(excel, "A9:D20"""234018);
SfActivateExcelWorksheet(excel, "Expenses");

SfSaveExcel(excel, "hello.xlsx");

The result of running Code Listing 42 is shown in Figure 23.

An Excel Document Created by CSCS using Syncfusion

Figure 23: An Excel Document Created by CSCS using Syncfusion

Summary

In this chapter, we saw how to create PDF, Microsoft Word, and Excel documents using the Syncfusion framework. You can create Microsoft PowerPoint presentations with it as well. If you want to add PowerPoint to CSCS, just mimic what we did in this chapter.

In the next chapter, we are going to see how to add non-GUI related functionality to CSCS, in particular, text-to-speech and speech recognition.

Scroll To Top
Disclaimer
DISCLAIMER: Web reader is currently in beta. Please report any issues through our support system. PDF and Kindle format files are also available for download.

Previous

Next



You are one step away from downloading ebooks from the Succinctly® series premier collection!
A confirmation has been sent to your email address. Please check and confirm your email subscription to complete the download.