
// Copyright (C) 2003-2026, Foxit Software Inc..
// All Rights Reserved.
//
// http://www.foxitsoftware.com
//
// The following code is copyrighted and contains proprietary information and trade secrets of Foxit Software Inc..
// You cannot distribute any part of Foxit PDF SDK to any third party or general public,
// unless there is a separate license agreement with Foxit Software Inc. which explicitly grants you such rights.
//
// This file contains an example to demonstrate how to use Foxit PDF SDK to add table to PDF document.

// Include Foxit SDK header files.
#include <time.h>
#include <iostream>
#include <map>

#if defined(_WIN32) || defined(_WIN64)
#include<direct.h>
#else
#include <sys/stat.h>
#endif

#include "../../../include/common/fs_common.h"
#include "../../../include/pdf/annots/fs_annot.h"
#include "../../../include/pdf/fs_pdfdoc.h"
#include "../../../include/pdf/fs_pdfpage.h"
#include "../../../include/addon/tablegenerator/fs_tablegenerator.h"

using namespace std;
using namespace foxit;
using namespace foxit::common;
using foxit::common::Library;
using namespace pdf;
using namespace annots;
using foxit::pdf::annots::Line;

static const char* sn = "Rew1vB0TqoMIxX444UcpScARWdhgJpsJfzmyMLR32a0+EgrCjp2NaQ==";
static const char* key = "8f3g1sFttW8NAgfSNv+BvaJWy4Pp8flcm7ncNDkdvCCO4JwUFbGLyZdujReMaiplbYJwwxam3lr7uHtQaEf0xTSu8j2ZBdrZ8FS4OQkEUjOI9UEGb3Ic0GqmlP9T9OnSItOYqoZZA/OnYg5EaVP+c/ha32nUquOk+5riEdhYgINAZDdgJkPc+YSWsrfixOIROf6t34j3eRcyFnOY0MS7tSXi1AEPE0VZdyu+5bfmBSGvLI+WR5lQ+oOQR5hRGwqf990WEdBelNO7dxhZELVhEZ+/L60PpDf5y9A8qmrY6PiEU8nhqIyLpks0BxeIsYyZ3Cr82jjfbItPGyEARsIVzluBSgVMD4oGjojz8T6wGDEXUbmZH/pra94XAs8wZ28mzbmIEppWnhBqX0c6Ov8iXCfFfUZr4KNuPxMEgk4VOYTMOd70GEjGnylivw18hbn7ZpkWh2Zql+uU2HejvOgn+WHvT0HF9NhK0Xb5vH8aFkgBP92PJx8obIbxBVeeExczxvsqWufuPdXS2sPNIgMYZ2TtvqVVsiUToeGB2FQo09fzZOSH5Vi18u/siyvC3FmjeMf4+OYWPuDPNCcrb21NgcHam7XI8AAoGLTGtLLWR+fxynbBBcg2d2ZPcZ5CMFIfOBHRWCkcA0sDT3cl9nsBppHNJze/jVxHy+lREeQEA/etSIgohTGAWW3ZlpibRXqMxlmVWEPifCubbWTzQ6U54mRMpW2UghH3gvCNFNgdday3Av7EtlIJOdUuiMq3zmmd5J4Jt4C82CKnVewA0DtZ4EEtwRG9IYuLrfpuZlnjBAtoeXRV1lqUZ6BcCe0E3m9jO28sGkALuIeJQ74XG7X8lMC7lMWWQYiYD7D6G9QT6X3qV4vXJwNKRKpYc/rI+amEawTSnDYAMEaYPwtZ0rb/7BL+atsZZaI+ncebdfVfjDJZbCZAc+JnkuNlUn2xrNRuObw8h/G0lm2pNOEJ10S8R4+yplo5B1QmC2dCDhaTaW67QJPqnJPKbVmjw7i6KGBi+CDP3JnAKlOX/YUu8uk56JfeQUuGcQg/KaBMGctnchH2n1i3+oGbibhurQj22+OO66Q6mijKoCRP/QJm5WTGcO4c0iVX0B8dIlai/uv/pGU8Wps9MxysH5ViVIj56LWKuNfy1FNO9+wx4iHYnUxfW0Iccq3YJYAlOBkMv8VZIJl9aTEEFJ626LJEw/UbEBdR9CdlwnylPf4RuNW/lcGYMgq3xNVpp/bzYKwZivvQOFgMdX1tv8D33SHvAm3zrRCU+v94BJXnBi9k25fnKsc705IMwv+I6bueXA+mDnagj1oTqsWXZ0WnPAYI3WUO7sBf7XUH230wfYXnTSVhujJxiEP/2oWqi2813gtQkKwz9N2p6yT8Vsh3+BaAwwDvtOZMJKUFcabTBSL6NoouzmzwItJ4mIboT6KXaef66rs8jCU=";

#if defined(_WIN32) || defined(_WIN64)
static WString output_path = WString::FromLocal("../output_files/");
static WString input_path = WString::FromLocal("../input_files/");
#else
static WString output_path = WString::FromLocal("./output_files/");
static WString input_path = WString::FromLocal("./input_files/");
#endif

class SdkLibMgr {
public:
  SdkLibMgr() : is_initialize_(false){};
  ErrorCode Initialize() {
    ErrorCode error_code = Library::Initialize(sn, key);
    if (error_code != foxit::e_ErrSuccess) {
      printf("Library Initialize Error: %d\n", error_code);
    } else {
      is_initialize_ = true;
    }
    return error_code;

  }
  ~SdkLibMgr(){
    if(is_initialize_)
      Library::Release();
  }
private:
  bool is_initialize_;
};

DateTime GetLocalDateTime() {
  time_t t = time(NULL);
#if (WINAPI_PARTITION_APP || WINAPI_PARTITION_PC_APP) || \
  (defined(_WIN32) || defined(_WIN64)) && _FX_COMPILER_ != _FX_VC6_
  struct tm _Tm;
  localtime_s(&_Tm, &t);
  struct tm* rime = &_Tm;
  _tzset();
  long time_zone = NULL;
  _get_timezone(&time_zone);
  int timezone_hour = time_zone / 3600 * -1;
  int timezone_minute = (abs(time_zone) % 3600) / 60;
#elif defined(__linux__)
  struct tm* rime = localtime(&t);
  tzset();
  int timezone_hour = __timezone / 3600 * -1;
  int timezone_minute = ((int)abs(__timezone) % 3600) / 60;
#elif defined(__APPLE__)
  struct tm* rime = localtime(&t);
  tzset();
  int timezone_hour = timezone / 3600 * -1;
  int timezone_minute = ((int)abs(timezone) % 3600) / 60;
#endif
  DateTime datetime;
  datetime.year = static_cast<uint16>(rime->tm_year + 1900);
  datetime.month = static_cast<uint16>(rime->tm_mon + 1);
  datetime.day = static_cast<uint16>(rime->tm_mday);
  datetime.hour = static_cast<uint16>(rime->tm_hour);
  datetime.minute = static_cast<uint16>(rime->tm_min);
  datetime.second = static_cast<uint16>(rime->tm_sec);
  datetime.utc_hour_offset = timezone_hour;
  datetime.utc_minute_offset = timezone_minute;

  return datetime;
}

WString DateTimeToString(const DateTime& datetime) {
  WString ws_datetime;
  ws_datetime.Format((FX_LPCWSTR)L"%d/%d/%d-%d:%d:%d %s%d:%d", datetime.year, datetime.month, datetime.day, datetime.hour,
    datetime.minute, datetime.second, datetime.utc_hour_offset > 0 ? L"+" : L"-", datetime.utc_hour_offset,
    datetime.utc_minute_offset);
  return ws_datetime;
}


foxit::pdf::RichTextStyle GetTableTextStyle(int index) {
  foxit::pdf::RichTextStyle style = foxit::pdf::RichTextStyle();
  style.font = common::Font(common::Font::e_StdIDHelvetica);
  style.text_size = 10;
  style.text_alignment = common::Alignment::e_AlignmentLeft;
  style.text_color = 0x000000;
  style.is_bold = false;
  style.is_italic = false;
  style.is_underline = false;
  style.is_strikethrough = false;
  style.mark_style = foxit::pdf::RichTextStyle::CornerMarkStyle::e_CornerMarkNone;

  switch (index) {
  case 1:
    style.text_alignment = common::Alignment::e_AlignmentCenter;
    break;
  case 2: {
    style.text_alignment = common::Alignment::e_AlignmentRight;
    style.text_color = 0x00FF00;
    break;
  }
  case 3:
    style.text_size = 15;
    break;
  case 4: {
#if defined(__linux__)
    style.font = Font(L"FreeSerif", 0, Font::e_CharsetANSI, 0);
#else
    style.font = common::Font(L"Times New Roman", 0, Font::e_CharsetANSI, 0);
#endif
    style.text_color = 0xFF0000;
    style.text_alignment = common::Alignment::e_AlignmentRight;
    break;
  }
  case 5: {
#if defined(__linux__)
    style.font = Font(L"FreeSerif", 0, Font::e_CharsetANSI, 0);
#else
    style.font = common::Font(L"Times New Roman", 0, Font::e_CharsetANSI, 0);
#endif
    style.is_bold = true;
    style.text_alignment = common::Alignment::e_AlignmentRight;
    break;
  }
  case 6: {
    style.is_bold = true;
    style.is_italic = true;
    break;
  }
  case 7: {
    style.is_bold = true;
    style.is_italic = true;
    style.text_alignment = common::Alignment::e_AlignmentCenter;
    break;
  }
  case 8: {
    style.is_underline = true;
    style.text_alignment = common::Alignment::e_AlignmentRight;
    break;
  }
  case 9:
    style.is_strikethrough = true;
    break;
  case 10:
    style.mark_style = foxit::pdf::RichTextStyle::CornerMarkStyle::e_CornerMarkSubscript;
    break;
  case 11:
    style.mark_style = foxit::pdf::RichTextStyle::CornerMarkStyle::e_CornerMarkSuperscript;
    break;
  default:
    break;
  }
  return style;
}

WString GetTableCellText(int index) {
  WString cell_text = L"";
  switch (index) {
  case 0:
    cell_text = L"Reference style";
    break;
  case 1:
    cell_text = L"Alignment center";
    break;
  case 2:
    cell_text = L"Green text color and alignment right";
    break;
  case 3:
    cell_text = L"Text font size 15";
    break;
  case 4: {
#if defined(__linux__)
    cell_text = L"Red text color, FreeSerif font and alignment right";
#else
    cell_text = L"Red text color, Times New Roman font and alignment right";
#endif
    break;
  }
  case 5: {
#if defined(__linux__)
    cell_text = L"Bold, FreeSerif font and alignment right";
#else
    cell_text = L"Bold, Times New Roman font and alignment right";
#endif
    break;
  }
  case 6:
    cell_text = L"Bold and italic";
    break;
  case 7:
    cell_text = L"Bold, italic and alignment center";
    break;
  case 8:
    cell_text = L"Underline and alignment right";
    break;
  case 9:
    cell_text = L"Strikethrough";
    break;
  case 10:
    cell_text = L"CornerMarkSubscript";
    break;
  case 11:
    cell_text = L"CornerMarkSuperscript";
    break;
  default:
    cell_text = L" ";
    break;
  }

  return cell_text;
}

void AddElectronicTable(PDFPage &page) {

  // Add a spreadsheet with 4 rows and 3 columns
  {
    int index = 0;
    foxit::addon::tablegenerator::TableCellDataArray cell_array;
    
    for (int row = 0; row < 4; row++) {
      foxit::addon::tablegenerator::TableCellDataColArray col_array;
      for (int col = 0; col < 3; col++) {
        foxit::pdf::RichTextStyle style = GetTableTextStyle(index);
        WString cell_text = GetTableCellText(index++);
        foxit::addon::tablegenerator::TableCellData cell_data(style, 0xFFFFFFFF, cell_text, foxit::common::Image((FS_HANDLE)NULL), RectF());
        col_array.Add(cell_data);
      }
      cell_array.Add(col_array);
    }
    FX_FLOAT page_width = page.GetWidth();
    FX_FLOAT page_height = page.GetHeight();
 
    foxit::addon::tablegenerator::TableBorderInfo outside_border_left;
    outside_border_left.line_width = 1;
    foxit::addon::tablegenerator::TableBorderInfo outside_border_right;
    outside_border_right.line_width = 1;
    foxit::addon::tablegenerator::TableBorderInfo outside_border_top;
    outside_border_top.line_width = 1;
    foxit::addon::tablegenerator::TableBorderInfo outside_border_bottom;
    outside_border_bottom.line_width = 1;
    foxit::addon::tablegenerator::TableBorderInfo inside_border_row;
    inside_border_row.line_width = 1;
    foxit::addon::tablegenerator::TableBorderInfo inside_border_col;
    inside_border_col.line_width = 1;
    foxit::addon::tablegenerator::TableData data(RectF(100, 550, page_width - 100, page_height - 100), 4, 3, outside_border_left, outside_border_right, outside_border_top, outside_border_bottom, inside_border_row, inside_border_col, foxit::addon::tablegenerator::TableCellIndexArray(), FloatArray(), FloatArray());
    foxit::addon::tablegenerator::TableGenerator::AddTableToPage(page, data, cell_array);
  }

  //Add a spreadsheet with 5 rows and 6 columns
  {
    foxit::addon::tablegenerator::TableCellDataArray cell_array;
    WString cell_text = L" ";
    for (int row = 0; row < 5; row++) {
      foxit::addon::tablegenerator::TableCellDataColArray col_array;
      for (int col = 0; col < 6; col++) {
        foxit::pdf::RichTextStyle style = GetTableTextStyle(row);
        switch (col) {
        case 0:
          cell_text.Format(L"First column");
          break;
        case 1:
          cell_text.Format(L"Second column");
          break;
        case 2:
          cell_text.Format(L"Third column");
          break;
        case 3:
          cell_text.Format(L"Fourth column");
          break;
        case 4:
          cell_text.Format(L"Fifth column");
          break;
        case 5:
          cell_text = DateTimeToString(GetLocalDateTime());
          break;
        default:
          cell_text = L" ";
          break;
        }
        foxit::addon::tablegenerator::TableCellData cell_data(style, 0xFFFFFFFF, cell_text, foxit::common::Image((FS_HANDLE)NULL), RectF());
        col_array.Add(cell_data);
      }
      cell_array.Add(col_array);
    }

    FX_FLOAT page_width = page.GetWidth();
    FX_FLOAT page_height = page.GetHeight();
    foxit::addon::tablegenerator::TableBorderInfo outside_border_left;
    outside_border_left.line_width = 2;
    foxit::addon::tablegenerator::TableBorderInfo outside_border_right;
    outside_border_right.line_width = 2;
    foxit::addon::tablegenerator::TableBorderInfo outside_border_top;
    outside_border_top.line_width = 2;
    foxit::addon::tablegenerator::TableBorderInfo outside_border_bottom;
    outside_border_bottom.line_width = 2;

    foxit::addon::tablegenerator::TableBorderInfo inside_border_row;
    inside_border_row.line_width = 2;
    foxit::addon::tablegenerator::TableBorderInfo inside_border_col;
    inside_border_col.line_width = 2;
    foxit::addon::tablegenerator::TableData data(RectF(10, 200, page_width - 10, page_height - 350), 5, 6, outside_border_left, outside_border_right, outside_border_top, outside_border_bottom, inside_border_row, inside_border_col, foxit::addon::tablegenerator::TableCellIndexArray(), FloatArray(), FloatArray());
    foxit::addon::tablegenerator::TableGenerator::AddTableToPage(page, data, cell_array);
  }
}


int main(int argc, char *argv[]) {
  int err_ret = 0;
  SdkLibMgr sdk_lib_mgr;
  // Initialize library
  ErrorCode error_code = sdk_lib_mgr.Initialize();
  if (error_code != foxit::e_ErrSuccess) {
    return 1;
  }

  try {
    WString output_directory = output_path + L"electronictable/";
#if defined(_WIN32) || defined(_WIN64)
    _mkdir(String::FromUnicode(output_directory));
#else
    mkdir(String::FromUnicode(output_directory), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
#endif

    PDFDoc doc = PDFDoc();
    PDFPage page = doc.InsertPage(0);
    AddElectronicTable(page);
    // Save PDF file
    WString new_pdf = output_directory + L"electronictable_result.pdf";
    doc.SaveAs(new_pdf, PDFDoc::e_SaveFlagNoOriginal);
    std::cout << "electronictable demo." << std::endl;
  }
  catch (const Exception& e) {
    cout << e.GetMessage() << endl;
    err_ret = 1;
  }

  return err_ret;
}
