import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import wkc.pdf.PdfException;
import wkc.pdf.tool.Field;
import wkc.pdf.tool.Report;
import wkc.pdf.tool.ReportException;

public class Tips2 {
  //WPRLのホームディレクトリを設定
  static {
    System.getProperties().setProperty("wkc.pdf.lib.home","C:\\Program Files\\wkc-pdflib");//適宜修正してください
  }
  
  private static File FORM_DIRECTORY_ROOT = new File("./form");
  private static File OUTPUT_DIRECTORY_ROOT = new File(".");
  private static final int PAGE1_DTL_MAXLINE = 27;
  private static final int PAGE2_DTL_MAXLINE = 41;
  
  private static String[] xPName = {"モカ100", "モカ200", "モカ500",
                                      "ケニア100", "ケニア200", "ケニア500",
                                      "キリマンジャロ100", "キリマンジャロ200", "キリマンジャロ500",
                                      "ガラパゴス100", "ガラパゴス200", "ガラパゴス500",
                                      "ハワイ・コナ100", "ハワイ・コナ200", "ハワイ・コナ500",
                                      "ブルーマウンテン100", "ブルーマウンテン200", "ブルーマウンテン500",
                                      "コロンビア100", "コロンビア200", "コロンビア500",
                                      "ブラジル・サントス100", "ブラジル・サントス200", "ブラジル・サントス500",
                                      "マンデリン100", "マンデリン200", "マンデリン500",
                                      "グァテマラ100", "グァテマラ200", "グァテマラ500",
                                      "モカ100", "モカ200", "モカ500",
                                      "ケニア100", "ケニア200", "ケニア500",
                                      "キリマンジャロ100", "キリマンジャロ200", "キリマンジャロ500",
                                      "ガラパゴス100", "ガラパゴス200", "ガラパゴス500",
                                      "ハワイ・コナ100", "ハワイ・コナ200", "ハワイ・コナ500",
                                      "ブルーマウンテン100", "ブルーマウンテン200", "ブルーマウンテン500",
                                      "コロンビア100", "コロンビア200", "コロンビア500",
                                      "ブラジル・サントス100", "ブラジル・サントス200", "ブラジル・サントス500",
                                      "マンデリン100", "マンデリン200", "マンデリン500",
                                      "グァテマラ100", "グァテマラ200", "グァテマラ500",
                                      "モカ100", "モカ200", "モカ500",
                                      "ケニア100", "ケニア200", "ケニア500",
                                      "キリマンジャロ100", "キリマンジャロ200", "キリマンジャロ500",
                                      "ガラパゴス100", "ガラパゴス200", "ガラパゴス500",
                                      "ハワイ・コナ100", "ハワイ・コナ200", "ハワイ・コナ500",
                                      "ブルーマウンテン100", "ブルーマウンテン200", "ブルーマウンテン500",
                                      "コロンビア100", "コロンビア200", "コロンビア500",
                                      "ブラジル・サントス100", "ブラジル・サントス200", "ブラジル・サントス500",
                                      "マンデリン100", "マンデリン200", "マンデリン500",
                                      "グァテマラ100", "グァテマラ200", "グァテマラ500",};

  private static String[] xPNumber = {"MO-100", "MO-200", "MO-500",
                                        "KE-100", "KE-200", "KE-500",
                                        "KI-100", "KI-200", "KI-500",
                                        "GA-100", "GA-200", "GA-500",
                                        "HA-100", "HA-200", "HA-500",
                                        "BU-100", "BU-200", "BU-500",
                                        "KO-100", "KO-200", "KO-500",
                                        "BR-100", "BR-200", "BR-500",
                                        "MA-100", "MA-200", "MA-500",
                                        "GT-100", "GT-200", "GT-500",
                                        "MO-100", "MO-200", "MO-500",
                                        "KE-100", "KE-200", "KE-500",
                                        "KI-100", "KI-200", "KI-500",
                                        "GA-100", "GA-200", "GA-500",
                                        "HA-100", "HA-200", "HA-500",
                                        "BU-100", "BU-200", "BU-500",
                                        "KO-100", "KO-200", "KO-500",
                                        "BR-100", "BR-200", "BR-500",
                                        "MA-100", "MA-200", "MA-500",
                                        "GT-100", "GT-200", "GT-500",
                                        "MO-100", "MO-200", "MO-500",
                                        "KE-100", "KE-200", "KE-500",
                                        "KI-100", "KI-200", "KI-500",
                                        "GA-100", "GA-200", "GA-500",
                                        "HA-100", "HA-200", "HA-500",
                                        "BU-100", "BU-200", "BU-500",
                                        "KO-100", "KO-200", "KO-500",
                                        "BR-100", "BR-200", "BR-500",
                                        "MA-100", "MA-200", "MA-500",
                                        "GT-100", "GT-200", "GT-500",};

  private static int[] xUnit = {1, 1, 1,
                                  1, 2, 1,
                                  1, 1, 2,
                                  1, 1, 2,
                                  1, 2, 2,
                                  1, 3, 1,
                                  1, 2, 3,
                                  1, 3, 2,
                                  1, 3, 1,
                                  1, 1, 1,
                                  1, 2, 3,
                                  1, 1, 1,
                                  1, 2, 1,
                                  1, 1, 2,
                                  1, 1, 2,
                                  1, 2, 2,
                                  1, 3, 1,
                                  1, 2, 3,
                                  1, 3, 2,
                                  1, 3, 1,
                                  1, 1, 1,
                                  1, 2, 3,
                                  1, 1, 1,
                                  1, 2, 1,
                                  1, 1, 2,
                                  1, 1, 2,
                                  1, 2, 2,
                                  1, 3, 1,
                                  1, 2, 3,
                                  1, 3, 2,
                                  1, 3, 1,
                                  1, 1, 1,
                                  1, 2, 3,};

  private static int[] xPrice = {520, 1020, 2500,
                                   600, 1180, 2900,
                                   490, 960, 2350,
                                   780, 1540, 3800,
                                  1350, 2680, 6650,
                                  1600, 3180, 7900,
                                   550, 1080, 2650,
                                   660, 1300, 3200,
                                   500, 980, 2400,
                                   490, 960, 2350,
                                   520, 1020, 2500,
                                   520, 1020, 2500,
                                   600, 1180, 2900,
                                   490, 960, 2350,
                                   780, 1540, 3800,
                                  1350, 2680, 6650,
                                  1600, 3180, 7900,
                                   550, 1080, 2650,
                                   660, 1300, 3200,
                                   500, 980, 2400,
                                   490, 960, 2350,
                                   520, 1020, 2500,
                                   520, 1020, 2500,
                                   600, 1180, 2900,
                                   490, 960, 2350,
                                   780, 1540, 3800,
                                  1350, 2680, 6650,
                                  1600, 3180, 7900,
                                   550, 1080, 2650,
                                   660, 1300, 3200,
                                   500, 980, 2400,
                                   490, 960, 2350,
                                   520, 1020, 2500,};
  
  
  public static void main(String[] args) throws IOException, ReportException, PdfException  {
    File outPdfFile = new File(OUTPUT_DIRECTORY_ROOT, "Tips2.pdf");
    File formFile = new File(FORM_DIRECTORY_ROOT, "Tips2.pdf");
    File datFile = new File(FORM_DIRECTORY_ROOT, "Tips2.dat");
    FileOutputStream fout = new FileOutputStream(outPdfFile);
    Report report = null;
    
    int page = 1;
    int xAmount = 0;
    // あらかじめ小計金額、合計金額を計算する。
    int xTotal[] = new int[xPName.length];
    for (int i = 0; i < xPName.length; i++){
      xTotal[i] = xUnit[i] * xPrice[i];
      xAmount += xTotal[i];
    }
    
    try {
      //reportファイルをつくる。
      report = new Report(formFile, datFile, fout);
      report.createPage(1);
      report.setDefaultFontSize(11.0f);
      
      //ヘッダ情報を入力
      Field field = report.getField("xPostCode");
      field.setFontSize(13.0f);
      report.putFieldData(field, "261-8501");
      field = report.getField("xAddres");
      field.setFontSize(14.0f);
      field.setOnMultiline(true);
      report.putFieldData(field, "千葉県千葉市美浜区中瀬X-X-XX");
      field = report.getField("xName");
      field.setFontSize(18.0f);
      report.putFieldData(field, "川鉄情報システム");
      report.putFieldData("xYear", "17");
      report.putFieldData("xMonth", "4");
      report.putFieldData("xDay", "1");
      report.putFieldData("xPage1", new Integer(page));
      report.putFieldData("xCCode", "CO2003-1111");
      field = report.getField("xAmount");
      field.setFontSize(24.0f);
      report.putFieldData(field, new Integer(xAmount));
      
      //ディテール情報を入力
      //1ページ目
      for (int i = 0; i < Math.min(PAGE1_DTL_MAXLINE, xPName.length); i++) {
        report.putFieldData("xPName."+ i, xPName[i]);
        report.putFieldData("xPNumber."+ i, xPNumber[i]);
        report.putFieldData("xUnit."+ i, new Integer(xUnit[i]));
        report.putFieldData("xPrice."+ i, new Integer(xPrice[i]));
        report.putFieldData("xTotal."+ i, new Integer(xTotal[i]));
      }
      
      //2ページ目以降
      for (int maxline = PAGE1_DTL_MAXLINE ,counter = PAGE1_DTL_MAXLINE; counter < xPName.length;counter++) {
        report.createPage(2);
        report.putFieldData("xPage2", new Integer(++page));
        maxline += PAGE2_DTL_MAXLINE;
        for (int i = PAGE1_DTL_MAXLINE; counter < Math.min(maxline,xPName.length); i++, counter++) {
          report.putFieldData("xPName." + i, xPName[counter]);
          report.putFieldData("xPNumber." + i, xPNumber[counter]);
          report.putFieldData("xUnit." + i, new Integer(xUnit[counter]));
          report.putFieldData("xPrice." + i, new Integer(xPrice[counter]));
          report.putFieldData("xTotal." + i, new Integer(xTotal[counter]));
        }
        counter--;
      }
      report.close();
      report = null;
      System.out.println("正常終了");
    } finally {
      fout.close();
      if (report != null) {
        try {
          //必ずclose()を呼びます。
          report.close();
        } catch (ReportException e) {
          //例外処理
        }
      }
    }
  }
}