diff xdwaddpage.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/xdwaddpage.cpp	Sun Mar 29 18:11:58 2015 +0900
@@ -0,0 +1,116 @@
+/*--------------------------------------------------------------------------------
+ * 機能:
+ *  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*>(&color), 0, NULL);
+    }
+
+	return api_result;
+}
+
+int main(int argc, char** argv)
+{
+	int api_result = 0;
+
+	if (argc != 2) {
+		fprintf(stderr, "使用方法:\n");
+        fprintf(stderr, "xdwaddpage 文書ファイル名\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;
+
+    int sz = 80;    // 文字サイズ
+    int tr = 1;     // 透明フラグ
+    char pagenum[12];
+
+	// アノテーションを貼り付ける
+    for (int i = 1; i <= last_page; i++) {
+        sprintf( pagenum, "%05d-%05d", i, last_page );
+        //api_result = add_annotation( h, i,  1700,  5600, pagenum, &sz, tr );    // 左上
+        api_result = add_annotation( h, i, 18500, 28500, pagenum, &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;
+}