comparison xdwaddatn.cpp @ 0:c1ebc8b218f2

xdwtools source code.
author pyon@macmini
date Sun, 29 Mar 2015 18:11:58 +0900
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:c1ebc8b218f2
1 /*--------------------------------------------------------------------------------
2 * 機能:
3 * DocuWorks文書のすべてのページにテキストアノテーションを貼り付ける
4 *-------------------------------------------------------------------------------*/
5
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <windows.h>
9
10 #include <xdw_api.h>
11 #include <xdwapian.h>
12
13 void print_error(int code)
14 {
15 switch (code) {
16 case XDW_E_NOT_INSTALLED:
17 fprintf(stderr, "DocuWorksがインストールされていません。");
18 break;
19 case XDW_E_FILE_NOT_FOUND:
20 fprintf(stderr, "指定されたファイルが見つかりません。");
21 break;
22 case XDW_E_ACCESSDENIED:
23 case XDW_E_INVALID_NAME:
24 case XDW_E_BAD_NETPATH:
25 fprintf(stderr, "指定されたファイルを開くことができません。");
26 break;
27 case XDW_E_BAD_FORMAT:
28 fprintf(stderr, "指定されたファイルは正しいフォーマットではありません。");
29 break;
30 case XDW_E_INVALID_ACCESS:
31 fprintf(stderr, "指定された操作をする権利がありません。");
32 break;
33 default:
34 fprintf(stderr, "エラーコード:%x\n", code);
35 fprintf(stderr, "エラーが発生しました。");
36 break;
37 }
38 }
39
40 int add_annotation(XDW_DOCUMENT_HANDLE h, int page, int x, int y, char* string, int* sz, int tr)
41 {
42 // テキストアノテーションを作成する
43 XDW_ANNOTATION_HANDLE annoation;
44 int api_result = XDW_AddAnnotation(
45 h, XDW_AID_TEXT, page, x, y, NULL, &annoation, NULL);
46 if (api_result < 0) return api_result;
47
48 // 作成したアノテーションに文字列を設定する
49 api_result = XDW_SetAnnotationAttribute(
50 h, annoation, XDW_ATN_Text, XDW_ATYPE_STRING, string, 0, NULL); // 文字テキスト
51 api_result = XDW_SetAnnotationAttribute(
52 h, annoation, XDW_ATN_FontSize, XDW_ATYPE_INT, reinterpret_cast<char*>(sz), 0, NULL); // フォントサイズ
53 if ( tr ) {
54 int color = XDW_COLOR_NONE;
55 api_result = XDW_SetAnnotationAttribute(
56 //h, annoation, XDW_ATN_BackColor, XDW_ATYPE_INT, reinterpret_cast<char*>(XDW_COLOR_NONE), 0, NULL); // 背景透明
57 h, annoation, XDW_ATN_BackColor, XDW_ATYPE_INT, reinterpret_cast<char*>(&color), 0, NULL); // 背景透明
58 }
59
60 return api_result;
61 }
62
63 int main(int argc, char** argv)
64 {
65 int api_result = 0;
66
67 if (argc != 8) {
68 fprintf(stderr, "使用方法:\n");
69 fprintf(stderr, "xdwaddatn 文書ファイル名 検索文字列 X座標 Y座標 挿入文字列 サイズ トランスペアレント\n");
70 fprintf(stderr, " ex. : xdwaddatn hoge.xdw searchtext 500 1000 Boooo! 120 1 # 背景透明\n");
71 fprintf(stderr, " ex. : xdwaddatn hoge.xdw searchtext 500 1000 Boooo! 120 0 # 背景白\n");
72 fprintf(stderr, " ex. : xdwaddatn hoge.xdw * 500 1000 Boooo! 120 0 # すべてのページに\n");
73 return 0;
74 }
75
76 char in_path[_MAX_PATH];
77 _fullpath(in_path, argv[1], _MAX_PATH);
78
79 // 文書ハンドルを開く
80 XDW_DOCUMENT_HANDLE h = NULL;
81 XDW_OPEN_MODE_EX mode = {
82 sizeof(XDW_OPEN_MODE_EX), XDW_OPEN_UPDATE, XDW_AUTH_NODIALOGUE };
83 api_result = XDW_OpenDocumentHandle(in_path, &h, (XDW_OPEN_MODE*)&mode);
84 if (api_result < 0) {
85 print_error(api_result);
86 return 0;
87 }
88
89 // XDW_GetDocumentInformationを用いて総ページ数を得る
90 XDW_DOCUMENT_INFO info = { sizeof(XDW_DOCUMENT_INFO), 0 };
91 XDW_GetDocumentInformation(h, &info);
92 int last_page = info.nPages;
93
94 XDW_FOUND_HANDLE pFoundHandle = NULL;
95
96
97 int x = atoi( argv[3] );
98 int y = atoi( argv[4] );
99 int sz = atoi( argv[6] );
100 int tr = atoi( argv[7] ); // 透明フラグ
101
102 // アノテーションを貼り付ける
103 if ( !strcmp( "*", argv[2] ) ) { // すべてのページ
104 for (int i = 1; i <= last_page; i++) {
105 api_result = add_annotation(h, i, x, y, argv[5], &sz, tr);
106 if (api_result < 0) {
107 print_error(api_result);
108 break;
109 }
110 }
111 }
112 else {
113 for (int i = 1; i <= last_page; i++) {
114 api_result = XDW_FindTextInPage( h, i, argv[2], NULL, &pFoundHandle, NULL );
115 if ( pFoundHandle != NULL ) { // 検索ヒット
116 api_result = add_annotation(h, i, x, y, argv[5], &sz, tr);
117 if (api_result < 0) {
118 print_error(api_result);
119 break;
120 }
121 }
122 }
123 }
124
125 // 変更をファイルに反映する
126 if (api_result >= 0) {
127 api_result = XDW_SaveDocument(h, NULL);
128 }
129
130 // 文書ハンドルを閉じる
131 XDW_CloseDocumentHandle(h, NULL);
132
133 if (api_result < 0) return 0;
134
135 return 1;
136 }