/*
	Antenna House PDF Driver API V7.5
	Java Interface sample program

	概要：入出力ファイルを指定してプログラムで編集した設定でPDF出力を行う
	      編集した設定を引数で指定したファイル名で保存する

	Copyright 2020 Antenna House,Inc.
*/
import jp.co.antennahouse.pdfdriver.pdfdrv.PtlDrvConvert;
import jp.co.antennahouse.pdfdriver.pdfdrv.PtlDrvException;
import jp.co.antennahouse.pdfdriver.pdfdrv.PtlParamColorSettings;
import jp.co.antennahouse.pdfdriver.pdfdrv.PtlParamCommonSettings;
import jp.co.antennahouse.pdfdriver.pdfdrv.PtlParamDriverSettings;
import jp.co.antennahouse.pdfdriver.pdfdrv.PtlParamInformationSettings;
import jp.co.antennahouse.pdfdriver.pdfdrv.PtlParamPDFVersionSettings;
import jp.co.antennahouse.pdfdriver.pdfdrv.PtlParamSecuritySettings;

public class Sampleapi01Java {

	public static void main(String[] args) {

		System.out.println("PDF Driver API V7.5 Java サンプル");

		//入出力ファイル名と設定ファイル名。テスト用の初期値を設定する
		String infile = "C:\\test\\test.docx";
		String outfile = "C:\\sav\\out.pdf";
		String setfile = "standard.ps4";
		String savefile = "C:\\sav\\sample-set.ps4";

		//第１引数は入力ファイル名とする
		if (args.length > 0)
		{
			infile = args[0];
		}

		//第２引数は出力ファイル名とする
		if (args.length > 1)
		{
			outfile = args[1];
		}

		//第３引数は設定ファイル名とする
		if (args.length > 2)
		{
			setfile = args[2];
		}

		//第４引数は保存する設定ファイル名とする
		if (args.length >= 3)
		{
			savefile = args[3];
		}

		try (PtlDrvConvert ptlConv = new PtlDrvConvert();
				PtlParamDriverSettings ptlParam = new PtlParamDriverSettings();)
		{
			ptlParam.load(setfile); //設定ファイルを読み込む

			//一般設定
			try (PtlParamCommonSettings ptlCommon = ptlParam.getCommon())
			{
				ptlCommon.setViewAuto(false);	// false -> PDF作成後表示しない
				ptlParam.setCommon(ptlCommon);
			}

			//PDFバージョン設定
			try (PtlParamPDFVersionSettings ptlPdfVer = ptlParam.getPDFVersion())
			{
				ptlPdfVer.setPdfVersion(PtlParamPDFVersionSettings.PDFVERSION_PDF17);	//PDF1.7を設定
				ptlParam.setPDFVersion(ptlPdfVer);
			}

			//色設定
			try (PtlParamColorSettings ptlColor = ptlParam.getColor())
			{
				ptlColor.setColorMode(PtlParamColorSettings.CM_GRAY);	//グレースケールに変換
				ptlParam.setColor(ptlColor);
			}

			//セキュリティ設定
			try (PtlParamSecuritySettings ptlSec = ptlParam.getSecurity())
			{
				ptlSec.setSecurityLevel(PtlParamSecuritySettings.SecurityLevel_AES256);	//AES256bit暗号化
				ptlSec.setOwnerPassword("test");			//権限パスワード文字列を指定
				ptlSec.setPDF14EditAllow(PtlParamSecuritySettings.EDIT_ALLOW_COMMENTFILL_SIGN);	//編集制限
				ptlSec.setPDF14PrintAllow(PtlParamSecuritySettings.PRINT_ALLOW_LOW_RESOLUTION);	//印刷制限
				ptlParam.setSecurity(ptlSec);
			}

			//情報設定
			try (PtlParamInformationSettings ptlInfo = ptlParam.getInformation())
			{
				ptlInfo.setAuthor("アンテナハウス株式会社");
				ptlInfo.setTitle("PDF Driver API テスト");
				ptlParam.setInformation(ptlInfo);
			}

			//編集した設定をファイルに保存する場合
			ptlParam.setSettingName("sample-setting");	//設定名を指定
			int iret = ptlParam.saveAs(savefile);
			if (iret != 0)
			{
				System.out.println("設定ファイルの保存に失敗：error code [" + iret+ "]");
			}

			//PDF出力
			ptlConv.setParamSettings(ptlParam);
			ptlConv.setSelectSettings(PtlDrvConvert.SELECT_SETTINGS_CLASS);	//ParamSettingsを使用する場合は、「ByAPI」の指定が必須
			boolean bret = ptlConv.convertFile(infile, outfile);   //PDF Driverを使用してPDF出力する
			if (bret)
			{
				System.out.println("成功");
			}
			else
			{
				System.out.println("失敗");
			}
		}
        catch (PtlDrvException ex)
        {
        	System.out.println("error code [" + ex.getErrorCode() + "] : " + ex.getErrorMessage());
        }
        catch (Exception ex)
        {
        	System.out.println("Error : " + ex.getMessage());
        }
        System.out.println("終了");
	}
}
