diff xdwaddatn.cpp @ 0:c1ebc8b218f2

xdwtools source code.
author pyon@macmini
date Sun, 29 Mar 2015 18:11:58 +0900
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/xdwaddatn.cpp	Sun Mar 29 18:11:58 2015 +0900
@@ -0,0 +1,136 @@
+/*--------------------------------------------------------------------------------
+ * 機能:
+ *  DocuWorks文書のすべてのページにテキストアノテーションを貼り付ける
+ *-------------------------------------------------------------------------------*/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <windows.h>
+
+#include <xdw_api.h>
+#include <xdwapian.h>
+
+void print_error(int code)
+{
+	switch (code) {
+	case XDW_E_NOT_INSTALLED:
+		fprintf(stderr, "DocuWorksがインストールされていません。");
+		break;
+	case XDW_E_FILE_NOT_FOUND:
+		fprintf(stderr, "指定されたファイルが見つかりません。");
+		break;
+	case XDW_E_ACCESSDENIED:
+	case XDW_E_INVALID_NAME:
+	case XDW_E_BAD_NETPATH:
+		fprintf(stderr, "指定されたファイルを開くことができません。");
+		break;
+	case XDW_E_BAD_FORMAT:
+		fprintf(stderr, "指定されたファイルは正しいフォーマットではありません。");
+		break;
+	case XDW_E_INVALID_ACCESS:
+		fprintf(stderr, "指定された操作をする権利がありません。");
+		break;
+	default:
+		fprintf(stderr, "エラーコード:%x\n", code);
+		fprintf(stderr, "エラーが発生しました。");
+		break;
+	}
+}
+
+int add_annotation(XDW_DOCUMENT_HANDLE h, int page, int x, int y, char* string, int* sz, int tr)
+{
+	// テキストアノテーションを作成する
+	XDW_ANNOTATION_HANDLE annoation;
+	int api_result = XDW_AddAnnotation(
+		h, XDW_AID_TEXT, page, x, y, NULL, &annoation, NULL);
+	if (api_result < 0) return api_result;
+
+	// 作成したアノテーションに文字列を設定する
+	api_result = XDW_SetAnnotationAttribute(
+		h, annoation, XDW_ATN_Text, XDW_ATYPE_STRING, string, 0, NULL); // 文字テキスト
+	api_result = XDW_SetAnnotationAttribute(
+		h, annoation, XDW_ATN_FontSize, XDW_ATYPE_INT, reinterpret_cast<char*>(sz), 0, NULL); // フォントサイズ
+    if ( tr ) {
+		int color = XDW_COLOR_NONE;
+		api_result = XDW_SetAnnotationAttribute(
+			//h, annoation, XDW_ATN_BackColor, XDW_ATYPE_INT, reinterpret_cast<char*>(XDW_COLOR_NONE), 0, NULL); // 背景透明
+			h, annoation, XDW_ATN_BackColor, XDW_ATYPE_INT, reinterpret_cast<char*>(&color), 0, NULL); // 背景透明
+    }
+
+	return api_result;
+}
+
+int main(int argc, char** argv)
+{
+	int api_result = 0;
+
+	if (argc != 8) {
+		fprintf(stderr, "使用方法:\n");
+        fprintf(stderr, "xdwaddatn 文書ファイル名 検索文字列 X座標 Y座標 挿入文字列 サイズ トランスペアレント\n");
+		fprintf(stderr, "    ex. : xdwaddatn hoge.xdw searchtext 500 1000 Boooo! 120 1    # 背景透明\n");
+		fprintf(stderr, "    ex. : xdwaddatn hoge.xdw searchtext 500 1000 Boooo! 120 0    # 背景白\n");
+		fprintf(stderr, "    ex. : xdwaddatn hoge.xdw *          500 1000 Boooo! 120 0    # すべてのページに\n");
+		return 0;
+	}
+
+	char in_path[_MAX_PATH];
+	_fullpath(in_path, argv[1], _MAX_PATH);
+
+	// 文書ハンドルを開く
+	XDW_DOCUMENT_HANDLE h = NULL;
+	XDW_OPEN_MODE_EX mode = {
+		sizeof(XDW_OPEN_MODE_EX), XDW_OPEN_UPDATE, XDW_AUTH_NODIALOGUE };
+	api_result = XDW_OpenDocumentHandle(in_path, &h, (XDW_OPEN_MODE*)&mode);
+	if (api_result < 0) {
+		print_error(api_result);
+		return 0;
+	}
+
+	// XDW_GetDocumentInformationを用いて総ページ数を得る
+	XDW_DOCUMENT_INFO info = { sizeof(XDW_DOCUMENT_INFO), 0 };
+	XDW_GetDocumentInformation(h, &info);
+	int last_page = info.nPages;
+
+    XDW_FOUND_HANDLE pFoundHandle = NULL;
+
+
+    int x = atoi( argv[3] );
+    int y = atoi( argv[4] );
+    int sz = atoi( argv[6] );
+    int tr = atoi( argv[7] );   // 透明フラグ
+
+	// アノテーションを貼り付ける
+	if ( !strcmp( "*", argv[2] ) ) {	// すべてのページ
+		for (int i = 1; i <= last_page; i++) {
+			api_result = add_annotation(h, i, x, y, argv[5], &sz, tr);
+			if (api_result < 0) {
+				print_error(api_result);
+				break;
+			}
+		}
+	} 
+	else {
+		for (int i = 1; i <= last_page; i++) {
+			api_result = XDW_FindTextInPage( h, i, argv[2], NULL, &pFoundHandle, NULL );
+			if ( pFoundHandle != NULL ) {   // 検索ヒット
+				api_result = add_annotation(h, i, x, y, argv[5], &sz, tr);
+				if (api_result < 0) {
+					print_error(api_result);
+					break;
+				}
+			}
+		}
+	}
+
+	// 変更をファイルに反映する
+	if (api_result >= 0) {
+		api_result = XDW_SaveDocument(h, NULL);
+	}
+
+	// 文書ハンドルを閉じる
+	XDW_CloseDocumentHandle(h, NULL);
+
+	if (api_result < 0) return 0;
+
+	return 1;
+}