/*
    Antenna House PDF Tool API V8.0
    Java Interface sample program

    概要：文書情報の取得（バージョン・文書情報・PDFの開き方・セキュリティ情報）

    Copyright 2015-2026 Antenna House, Inc.
*/

package Sample;

import jp.co.antenna.ptl.*;

public class GetDocInfo_2 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        if (args.length < 1)
        {
            System.out.println("usage: java GetDocInfo_2 in-pdf-file");
            return;
        }

        try (PtlParamInput inputFile = new PtlParamInput(args[0]);
             PtlPDFDocument doc = new PtlPDFDocument())
        {
            // PDFファイルをロード
            doc.load(inputFile);

    		//PDFの文書プロパティ
            try (PtlDocProperty docproperty = doc.getDocProperty();)	 //PDFの文書プロパティ
            {
	    		System.out.println("PDF Version");
	    		switch (docproperty.getVersion())
	    		{
	    		case PDFV1_0:	// PDF-1.0
	    			System.out.println(" major : 1");
	    			System.out.println(" minor : 0");
	    			break;
	    		case PDFV1_1:	// PDF-1.1
	    			System.out.println(" major : 1");
	    			System.out.println(" minor : 1");
	    			break;
	    		case PDFV1_2:	// PDF-1.2
	    			System.out.println(" major : 1");
	    			System.out.println(" minor : 2");
	    			break;
	    		case PDFV1_3:	// PDF-1.3
	    			System.out.println(" major : 1");
	    			System.out.println(" minor : 3");
	    			break;
	    		case PDFV1_4:	// PDF-1.4
	    			System.out.println(" major : 1");
	    			System.out.println(" minor : 4");
	    			break;
	    		case PDFV1_5:	// PDF-1.5
	    			System.out.println(" major : 1");
	    			System.out.println(" minor : 5");
	    			break;
	    		case PDFV1_6:	// PDF-1.6
	    			System.out.println(" major : 1");
	    			System.out.println(" minor : 6");
	    			break;
	    		case PDFV1_7:	// PDF-1.7
	    			System.out.println(" major : 1");
	    			System.out.println(" minor : 7");
	    			break;
	    		case PDFV2_0:	// PDF-2.0
	    			System.out.println(" major : 2");
	    			System.out.println(" minor : 0");
	    			break;
	    		case PDFV_UNSUPPORTED:	// 未サポートのバージョン
	    			System.out.println(" 未サポートのバージョン");
	    			break;
	    		}
            
	            System.out.print("\n");
	            System.out.println("Document Information");
	            try (PtlDocInfo docinf = docproperty.getDocInfo()) {    //PDFの文書情報
	
	                // タイトル取得
	                System.out.println(" Title : " + docinf.getTitle());
	
	                // サブジェクト取得
	                System.out.println(" Subject : " + docinf.getSubject());
	
	                // 著者取得
	                System.out.println(" Author : " + docinf.getAuthor());
	
	                // キーワード取得
	                System.out.println(" Keywords : " + docinf.getKeywords());
	
	                // クリエータ取得
	                System.out.println(" Creator : " + docinf.getCreator());
	
	                // プロデューサ取得
	                System.out.println(" Producer : " + docinf.getProducer());
	
	                // 作成日付を取得
	                try (PtlDate dateCreate = docinf.getCreationDate())
	                {
	                    System.out.println(" CreationDate : "
	                        + dateCreate.getYear()
	                        + "/" + dateCreate.getMonth()
	                        + "/" + dateCreate.getDay()
	                        + " " + dateCreate.getHour()
	                        + ":" + dateCreate.getMin()
	                        + ":" + dateCreate.getSec());
	                }
	
	                // 更新日付を取得
	                try (PtlDate dateMod = docinf.getModDate())
	                {
	                    System.out.println(" ModifyDate : "
	                        + dateMod.getYear()
	                        + "/" + dateMod.getMonth()
	                        + "/" + dateMod.getDay()
	                        + " " + dateMod.getHour()
	                        + ":" + dateMod.getMin()
	                        + ":" + dateMod.getSec());
	                }
	
	                // ページ数
	                System.out.println(" Max Page : " + doc.getPageCount());
	            }
	
	    		// 開き方情報を取得
	            System.out.println("\n");
	            System.out.println("View Information");
	    		try(PtlOpenMode openmode = docproperty.getOpenMode();)
	    		{
		    		//読み上げオプション取得
		    		switch (docproperty.getDirection()) {
		    		case DIRECTION_L2R:
		    			System.out.println(" Direction  : Left to Right");
		    			break;
		    		case DIRECTION_R2L:
		    			System.out.println(" Direction  : Right to Left )");
		    			break;
		    		}
		
		    		// ページレイアウトを取得
		    		switch (openmode.getPageLayout())
		    		{
		    		case PAGE_LAYOUT_SINGLE_PAGE:	// 単一ページ
		    			System.out.println(" PageLayout : SinglePage");
		    			break;
		    		case PAGE_LAYOUT_ONE_COLUMN:	// 連続ページ
		    			System.out.println(" PageLayout : OneColumn");
		    			break;
		    		case PAGE_LAYOUT_TWO_COLUMN_LEFT:	// 奇数ページを左側にして連続見開きページ
		    			System.out.println(" PageLayout : TwoColumnLeft");
		    			break;
		    		case PAGE_LAYOUT_TWO_COLUMN_RIGHT:	// 奇数ページを右側にして連続見開きページ
		    			System.out.println(" PageLayout : TwoColumnRight");
		    			break;
		    		case PAGE_LAYOUT_TWO_PAGE_LEFT:	// 奇数ページを左側にして見開きページ
		    			System.out.println(" PageLayout : TwoPageLeft");
		    			break;
		    		case PAGE_LAYOUT_TWO_PAGE_RIGHT:	// 奇数ページを右側にして見開きページ
		    			System.out.println(" PageLayout : TwoPageRight");
		    			break;
		    		}
		
		    		//  PageMode   : Use Outlines
		    		switch (openmode.getPageMode())
		    		{
		    			case PAGE_MODE_USE_NONE:	// ページのみ表示
		    				System.out.println(" PageMode   : Use None");
		    				break;
		    			case PAGE_MODE_USE_OUTLINES:	// アウトラインパネルとページ
		    				System.out.println(" PageMode   : Use Outlines");
		    				break;
		    			case PAGE_MODE_USE_THUMBS:	// サムネイルパネルとページ
		    				System.out.println(" PageMode   : Use Thumbs");
		    				break;
		    			case PAGE_MODE_USE_OC:	// レイヤーパネルとページ
		    				System.out.println(" PageMode   : Use OC");
		    				break;
		    			case PAGE_MODE_USE_ATTACHMENTS:	// 添付ファイルパネルとページ
		    				System.out.println(" PageMode   : Use Atttachments");
		    				break;
		    		}
		    		
		    		// ウィンドウオプションを取得
		    		boolean fitwindow
		    			= (openmode.getWindowOption() & PtlOpenMode.WINDOWS_OPTION_FIT_WINDOW)
		    			== PtlOpenMode.WINDOWS_OPTION_FIT_WINDOW;
		    		boolean centerwindow
		    			= (openmode.getWindowOption() & PtlOpenMode.WINDOWS_OPTION_CENTER_WINDOW)
		    			== PtlOpenMode.WINDOWS_OPTION_CENTER_WINDOW;
		    		boolean fullscreen
		    			= (openmode.getWindowOption() & PtlOpenMode.WINDOWS_OPTION_FULL_SCREEN)
		    			== PtlOpenMode.WINDOWS_OPTION_FULL_SCREEN;
		    		if (fitwindow) {
		    			System.out.println(" FitWindow  : true");
		    		}
		    		else{
		    			System.out.println(" FitWindow  : false");
		    		}
		    		if (centerwindow){
		    			System.out.println(" CenterWindow : true");
		    		}
		    		else{
		    			System.out.println(" CenterWindow : false");
		    		}
		    		if (fullscreen)	{
		    			System.out.println(" Full Screen  : true");
		    		}
		    		else {
		    			System.out.println(" Full Screen  : false");
		    		}
		
		    		// ウィンドウのタイトルバーに文書タイトルを表示するかどうかを取得
		    		if (openmode.getDisplayDocTitle())
		    		{
		    			System.out.println(" Display Doc Title  : true");
		    		}
		    		else
		    		{
		    			System.out.println(" Display Doc Title  : false");
		    		}
		
		    		// ユーザーインターフェイスオプションを取得
//		    		int uiopt = openmode.getUIOption();
		    		boolean hidemenu
		    			= (openmode.getUIOption() & PtlOpenMode.UI_OPTION_HIDE_MENUBAR)
		    			== PtlOpenMode.UI_OPTION_HIDE_MENUBAR;
		    		boolean hidetool
		    			= (openmode.getUIOption() & PtlOpenMode.UI_OPTION_HIDE_TOOLBAR)
		    			== PtlOpenMode.UI_OPTION_HIDE_TOOLBAR;
		    		boolean hidewinui
		    			= (openmode.getUIOption() & PtlOpenMode.UI_OPTION_HIDE_WINDOWUI)
		    			== PtlOpenMode.UI_OPTION_HIDE_WINDOWUI;
		    		if (hidemenu) {
		    			System.out.println(" Hide Menubar  : true");
		    		}
		    		else {
		    			System.out.println(" Hide Menubar : false");
		    		}
		    		if (hidetool) {
		    			System.out.println(" Hide Toolbar : true");
		    		}
		    		else {
		    			System.out.println(" Hide Toolbar : false");
		    		}
		    		if (hidewinui) {
		    			System.out.println(" Hide Window UI  : true");
		    		}
		    		else {
		    			System.out.println(" Hide Window UI : false");
		    		}
		
		    		// オープンアクションを取得
		    		showAction(openmode.getOpenAction());
		
		    		// カスタムプロパティを取得
		    		System.out.println("\n");
		    		System.out.println("Custom Property");
		    		PtlCustomProperties customProperties = docproperty.getCustomProperties();
		    		int numCustomProperties = customProperties.getCount();
		    		for (int i = 0; i < numCustomProperties; ++i) {
		    			PtlCustomProperty prop = customProperties.get(i);
		    			System.out.println(" Name  : " + prop.getName());
		    			System.out.println(" Value : " + prop.getValue());
		    		}
	    		}
	
	    		System.out.println("\n");
	    		System.out.println("Security Information");
	    		boolean isEncrypted = doc.isEncrypted();
	    		if (!isEncrypted) {
	    			System.out.println(" Not Encrypted.");
	    			return;
	    		}
	    		PtlEncrypt encrypt = doc.getEncrypt();
	    		PtlEncrypt.FILTER_TYPE filterType = encrypt.getFilterType();
	    		if (filterType != PtlEncrypt.FILTER_TYPE.FILTER_STANDARD) {
	    			return;
	    		}
	    		PtlEncryptStandard encryptStandard = (PtlEncryptStandard)encrypt;
	    		System.out.println(" Security Handler : Standard Security");
	    		int keyLength = encrypt.getKeyLength();
	    		System.out.println(" Key Length : " + keyLength);
	    		PtlEncryptStandard.STANDARD_METHOD_TYPE method = encryptStandard.getMethod();
	    		if (method == PtlEncryptStandard.STANDARD_METHOD_TYPE.METHOD_RC4) {
	    			System.out.println(" Method = RC4");
	    		}
	    		else
	    			if (method == PtlEncryptStandard.STANDARD_METHOD_TYPE.METHOD_RC4) {
	    				System.out.println(" Method = AES");
	    			}
	    		switch (encryptStandard.getPermission().getType()) {
	    		case PERM_TYPE1:
	    		{
	    			System.out.println(" Permission Type1");
	    			PtlEncryptPermissionType1 perms1 = (PtlEncryptPermissionType1)encryptStandard.getPermission();
	    			boolean print = perms1.getPrint();
	    			if (print) {
	    				System.out.println("  Print  : allowed.");
	    			}
	    			else {
	    				System.out.println("  Print  : not allowed.");
	    			}
	    			PtlEncryptPermissionType1.PERMISSION_MODIFY modify = perms1.getModify();
	    			switch (modify) {
	    			case PERM_MODIFY_NOT_ALLOWED:
	    				System.out.println("  Modify : not allowed.");
	    				break;
	    			case PERM_MODIFY_ANNOTANDFORM:
	    				System.out.println("  Modify : Commenting, filling in form fields, and signing existing signature fields.");
	    				break;
	    			case PERM_MODIFY_ASSEMBLEANDFORM:
	    				System.out.println("  Modify : Page layout, filling in form fields, and signing existing signature fields.");
	    				break;
	    			case PERM_MODIFY_MODYFYDOC:
	    				System.out.println("  Modify : Any except extracting pages.");
	    				break;
	    			}
	    			boolean copy = perms1.getCopy();
	    			if (copy) {
	    				System.out.println("  Copy   : Enable copying of text, images, and other content and access for the visually impaired.");
	    			}
	    			else {
	    				System.out.println("  Copy   : Disable copying of text, images, and other content and access for the visually impaired.");
	    			}
	    		}
	    		break;
	    		case PERM_TYPE2:
	    		{
	    			System.out.println(" Permission Type2");
	    			PtlEncryptPermissionType2 perms2 = (PtlEncryptPermissionType2)encryptStandard.getPermission();
	    			PtlEncryptPermissionType2.PERMISSION_PRINT print = perms2.getPrint();
	    			switch (print) {
	    			case PERM_PRINT_NOT_ALLOWED:
	    				System.out.println("  Print  : not allowed.");
	    				break;
	    			case PERM_PRINT_LOW:
	    				System.out.println("  Print  : Low resolution.");
	    				break;
	    			case PERM_PRINT_HIGH:
	    				System.out.println("  Print  : High resolution.");
	    				break;
	    			}
	    			PtlEncryptPermissionType2.PERMISSION_MODIFY modify = perms2.getModify();
	    			switch (modify) {
	    			case PERM_MODIFY_NOT_ALLOWED:
	    				System.out.println("  Modify : not allowed.");
	    				break;
	    			case PERM_MODIFY_ASSEMBLEDOC:
	    				System.out.println("  Modify : Inserting, deleting, and rotating pages.");
	    				break;
	    			case PERM_MODIFY_FILLFORM:
	    				System.out.println("  Modify : Filling in form fields, and signing existing signature fields.");
	    				break;
	    			case PERM_MODIFY_ANNOTANDFORM:
	    				System.out.println("  Modify : Commenting, filling in form fields, and signing existing signature fields.");
	    				break;
	    			case PERM_MODIFY_MODYFYDOC:
	    				System.out.println("  Modify : Any except extracting pages.");
	    				break;
	    			}
	    			boolean copy = perms2.getCopy();
	    			if (copy) {
	    				System.out.println("  Copy   : Enable copying of text, images, and other content.");
	    			}
	    			else {
	    				System.out.println("  Copy   : Disable copying of text, images, and other content.");
	    			}
	    			boolean acc = perms2.getAccessibility();
	    			if (acc) {
	    				System.out.println("  Accessibility : Enable text access for screen render devices for the visually impaired.");
	    			}
	    			else {
	    				System.out.println("  Accessibility : Disable text access for screen render devices for the visually impaired.");
	    			}
	    		}
	    		break;
	    		}
	
	    		System.out.println("\n");
	    		System.out.println("Standard Information");
	    		if (doc.isSignatured()) {
	    			System.out.println(" Signature  : true");
	    		}
	    		else {
	    			System.out.println(" Signature  : false");
	    		}
	
	    		if (doc.isPDFA()) {
	    			System.out.println(" PDF/A      : true");
	    		}
	    		else {
	    			System.out.println(" PDF/A      : false");
	    		}
	    		if (doc.isPDFX()) {
	    			System.out.println(" PDF/X      : true");
	    		}
	    		else {
	    			System.out.println(" PDF/X      : false");
	    		}
            }
        }
        catch(PtlException pex) {
            System.out.println("PtlException : ErrorCode = " + pex.getErrorCode() + "\n  " + pex.getErrorMessage());
        }
        catch(Exception ex) {
            System.out.println(ex.getMessage());
            ex.printStackTrace();
        }
        catch (Error ex) {
            System.out.println(ex.getMessage());
            ex.printStackTrace();
        }
        finally {
            System.out.print("\n");
            System.out.println("-- 完了 --");
        }
    }

	private static int showAction(PtlAction action) {
		try {
			switch (action.getType()) {
			case TYPE_NONE:
				System.out.println(" Action : none");
				break;
			case TYPE_UNKNOWN:
				System.out.println(" Action : unknown");
				break;
			case TYPE_GOTO:
			{
				System.out.println(" Action : GoTo");
				PtlActionGoTo actGoTo = (PtlActionGoTo)action;
				PtlDest dest = actGoTo.getDest();
				int ret = showDest(dest);
				if (ret != 0) {
					return ret;
				}
			}
			break;
			case TYPE_GOTO_R:
			{
				System.out.println(" Action : GoToR");
				PtlActionGoToR actGoToR = (PtlActionGoToR)action;
				PtlDest dest = actGoToR.getDest();
				int ret = showDest(dest);
				if (ret != 0) {
					return ret;
				}
			}
			break;
			case TYPE_LAUNCH:
			{
				System.out.println(" Action : Launch");
				PtlActionLaunch actLaunch = (PtlActionLaunch)action;
				System.out.println("   file name : " + actLaunch.getFileName());
				boolean flag = actLaunch.getNewWindowFlag();
				if (flag) {
					System.out.println("  new window : true");
				}
				else {
					System.out.println("  new window : false");
				}
			}
			break;
			case TYPE_URI:
			{
				System.out.println(" Action : URI");
				PtlActionURI actURI = (PtlActionURI)action;
				System.out.println("  uri : " + actURI.getURI());
			}
			break;
			case TYPE_JAVASCRIPT:
				break;
			default:
				break;
			}
		}
		catch (PtlException e) {
			System.out.println(" Error Message: " + e.getErrorMessage());
			System.out.println("\n");
			return e.getErrorCode();
		}
		catch (Exception e) {
			System.out.println("Unknown error has occured.");
			System.out.println("\n");
			return -2;
		}
		return 0;
	}


	private static int showDest(PtlDest dest)
	{
		try {
			PtlDest.DEST_TYPE destType = dest.getType();
			int pageNumber = dest.getPageNumber();
			switch (destType) {
			case TYPE_NONE:
				System.out.println("  Dest : none");
				break;
			case TYPE_XYZ:
				{
				System.out.println("  Dest page : " + pageNumber);
				System.out.println("  Dest type : XYZ");
					float left = 0.0f;
					if (!dest.isLeftNull())
						left = dest.getLeft();
					System.out.println("   left    : " + left);
					float top = 0.0f;
					if (!dest.isTopNull())
						top = dest.getTop();
					System.out.println("   top     : " + top);
					float zoom = 0.0f;
					if (!dest.isZoomNull())
						zoom = dest.getZoom();
					System.out.println("   zoom    : " + zoom);
				}
				break;
			case TYPE_FIT:
				System.out.println("  Dest page : " + pageNumber);
				System.out.println("  Dest type : Fit");
				break;
			case TYPE_FIT_H:
				{
					System.out.println("  Dest page : " + pageNumber);
					System.out.println("  Dest type : FitH");
					float top = 0.0f;
					if (!dest.isTopNull())
						top = dest.getTop();
					System.out.println("   top     : " + top);
				}
				break;
			case TYPE_FIT_V:
				{
				System.out.println("  Dest page : " + pageNumber);
				System.out.println("  Dest type : FitV");
					float left = 0.0f;
					if (!dest.isLeftNull())
						left = dest.getLeft();
					System.out.println("   left    : " + left);
				}
				break;
			case TYPE_FIT_R:
				{
				System.out.println("  Dest page : " + pageNumber);
				System.out.println("  Dest type : FitR");
					float left = 0.0f;
					if (!dest.isLeftNull())
						left = dest.getLeft();
					System.out.println("   left    : " + left);
					float bottom = 0.0f;
					if (!dest.isBottomNull())
						bottom = dest.getBottom();
					System.out.println("   bottom  : " + bottom);
					float right = 0.0f;
					if (!dest.isRightNull())
						right = dest.getRight();
					System.out.println("   right   : " + right);
					float top = 0.0f;
					if (!dest.isTopNull())
						top = dest.getTop();
					System.out.println("   top     : " + top);
					float zoom = 0.0f;
					if (!dest.isZoomNull())
						zoom = dest.getZoom();
					System.out.println("   zoom    : " + zoom);
				}
				break;
			case TYPE_FIT_B:
				System.out.println("  Dest page : " + pageNumber);
			System.out.println("  Dest type : FitB");
				break;
			case TYPE_FIT_BH:
				System.out.println("  Dest page : " + pageNumber);
			System.out.println("  Dest type : FitBH");
				{
					float top = 0.0f;
					if (!dest.isTopNull())
						top = dest.getTop();
					System.out.println("   top     : " + top);
				}
				break;
			case TYPE_FIT_BV:
				{
				System.out.println("  Dest page : " + pageNumber);
				System.out.println("  Dest type : Fit");
					float left = 0.0f;
					if (!dest.isLeftNull())
						left = dest.getLeft();
					System.out.println("   left    : " + left);
				}
				break;
			}
		}
		catch (PtlException e) {
			System.out.println(" Error Message: " + e.getErrorMessage());
			System.out.println("\n");
			return e.getErrorCode();
		}
		catch (Exception e) {
			System.out.println("Unknown error has occured.");
			System.out.println("\n");
			return -2;
		}
		return 0;
	}
}
