/**
 * Copyright (c) 2003-2005 System Integrator Corporation.
 *                 All Rights Reserved.
 */
package jp.co.sint.tools;

import java.io.FileOutputStream;
import java.io.IOException;

import com.lowagie.text.DocumentException;
import com.lowagie.text.PageSize;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfStamper;

/**
 * @version $Id : SIBGPdfTool.java,v 1.0 Exp $
 * @author      : Tsuyoshi Yagi
 * <br>Description :PDFファイル処理の基底クラス
 * <p>History</p>
 * <p>Author&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Date&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Reason</p>
 * ============&nbsp;&nbsp;&nbsp;==========&nbsp;&nbsp;===========================<br>
 * Tsuyoshi Yagi   2005/11/28  Original
 */
public class SIBGPdfTool {
  private PdfReader reader = null;
  
  private BaseFont bf = null;
  
  private PdfContentByte over;
  
  private PdfStamper stamp = null;
  
  private int pageNum = 0;
  
  private int lastPage = 0;
  
  private int fontSize = 9;
  
  /**
   * initialize <br>
   * 入力PDFファイル、出力PDFファイルを指定し初期化を当クラスの初期化を行います。
   * 
   * @param inputFile
   * @param outputFile
   */
  public void initialize(String inputFile, String outputFile) throws IOException, DocumentException {
    reader = new PdfReader(inputFile);
    bf = BaseFont.createFont("HeiseiMin-W3", "UniJIS-UCS2-HW-H", false);
    stamp = new PdfStamper(reader, new FileOutputStream(outputFile));
    pageNum = 1;
    lastPage = 1;
    over = stamp.getOverContent(pageNum);
  }
  
  public void fontSelect(String fontName) throws IOException, DocumentException {
    if (fontName.equals("Min")) {
      bf = BaseFont.createFont("HeiseiMin-W3", "UniJIS-UCS2-HW-H", false);
    } else if (fontName.equals("Goth")) {
      bf = BaseFont.createFont("HeiseiKakuGo-W5", "UniJIS-UCS2-H", false);
    }
  }
  
  /**
   * nextPage <br>
   * 書き込みを行うページ（カレントページ）を次のページに移動します
   */
  public void nextPage() {
    pageNum++;
    over = stamp.getOverContent(pageNum);
  }
  
  /**
   * previousPage <br>
   * 書き込みを行うページ（カレントページ）を前のページに移動します
   */
  public void previousPage() {
    pageNum--;
    over = stamp.getOverContent(pageNum);
  }
  
  /**
   * gotoPage <br>
   * 書き込みを行うページ（カレントページ）を任意のページに移動します
   * 
   * @param num ページ番号
   */
  public void gotoPage(int num) {
    over = stamp.getOverContent(num);
  }
  
  /**
   * addPage <br>
   * ページを追加します。
   */
  public void addPage() {
    lastPage++;
    stamp.insertPage(lastPage, PageSize.A4);
    over = stamp.getOverContent(lastPage);
    over.addTemplate(stamp.getImportedPage(reader, 1), 1, 0, 0, 1, 0, 0);
    pageNum = lastPage;
  }
  
  /**
   * addPage <br>
   * ページを追加します。
   * 
   * @param inputFile 入力PDFファイル
   */
  public void addPage(String inputFile) throws IOException {
    reader = new PdfReader(inputFile);
    lastPage++;
    stamp.insertPage(lastPage, PageSize.A4);
    over = stamp.getOverContent(lastPage);
    over.addTemplate(stamp.getImportedPage(reader, 1), 1, 0, 0, 1, 0, 0);
    pageNum = lastPage;
  }
  
  /**
   * close <br>
   * 編集しているファイルを閉じます
   */
  public void close() {
    try {
      //reader.close();
      stamp.close();
    } catch (IOException IE) {
      
    } catch (DocumentException DE) {
      
    }
  }
  
  /**
   * write <br>
   * 指定した座標に文字を入力します。
   * 
   * @param text 入力する文字列
   * @param x X座標
   * @param y Y座標
   */
  public void write(String text, float x, float y) {
    over.beginText();
    over.setFontAndSize(bf, fontSize);
    over.setTextMatrix(x, y);
    over.showText(text);
    over.endText();
  }
  
  /**
   * write <br>
   * 指定した座標に文字を入力します。
   * 
   * @param text 入力する文字列
   * @param x X座標
   * @param y Y座標
   */
  public void write(String text, int x, int y) {
    over.beginText();
    over.setFontAndSize(bf, fontSize);
    over.setTextMatrix(x, y);
    over.showText(text);
    over.endText();
  }
  
  /**
   * setFontSize <br>
   * フォントのサイズを指定します。
   * 
   * @param size フォントサイズ
   */
  public void setFontSize(int size) {
    fontSize = size;
  }
  /**
   * write <br>
   * 指定した座標に文字を入力します。
   * 
   * @param text 入力する文字列
   * @param x X座標
   * @param y Y座標
   */
  public void writeRtoL(String text, int x, int y) {
    over.beginText();
    over.setFontAndSize(bf, fontSize);
    int size = text.length();
    x = x - size*(fontSize/2);
    over.setTextMatrix(x, y);
    over.showText(text);
    over.endText();
  }
  
  /**
   * write <br>
   * 指定した座標に文字を入力します。
   * 
   * @param text 入力する文字列
   * @param x X座標
   * @param y Y座標
   */
  public void writeRtoL(String text, float x, float y) {
    over.beginText();
    over.setFontAndSize(bf, fontSize);
    int size = text.length();
    x = x - size*(fontSize/2);
    over.setTextMatrix(x, y);
    over.showText(text);
    over.endText();
  }
}
