comparison xdwaddpage.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*>(&color), 0, NULL);
57 }
58
59 return api_result;
60 }
61
62 int main(int argc, char** argv)
63 {
64 int api_result = 0;
65
66 if (argc != 2) {
67 fprintf(stderr, "使用方法:\n");
68 fprintf(stderr, "xdwaddpage 文書ファイル名\n");
69 return 0;
70 }
71
72 char in_path[_MAX_PATH];
73 _fullpath(in_path, argv[1], _MAX_PATH);
74
75 // 文書ハンドルを開く
76 XDW_DOCUMENT_HANDLE h = NULL;
77 XDW_OPEN_MODE_EX mode = {
78 sizeof(XDW_OPEN_MODE_EX), XDW_OPEN_UPDATE, XDW_AUTH_NODIALOGUE };
79 api_result = XDW_OpenDocumentHandle(in_path, &h, (XDW_OPEN_MODE*)&mode);
80 if (api_result < 0) {
81 print_error(api_result);
82 return 0;
83 }
84
85 // XDW_GetDocumentInformationを用いて総ページ数を得る
86 XDW_DOCUMENT_INFO info = { sizeof(XDW_DOCUMENT_INFO), 0 };
87 XDW_GetDocumentInformation(h, &info);
88 int last_page = info.nPages;
89
90 int sz = 80; // 文字サイズ
91 int tr = 1; // 透明フラグ
92 char pagenum[12];
93
94 // アノテーションを貼り付ける
95 for (int i = 1; i <= last_page; i++) {
96 sprintf( pagenum, "%05d-%05d", i, last_page );
97 //api_result = add_annotation( h, i, 1700, 5600, pagenum, &sz, tr ); // 左上
98 api_result = add_annotation( h, i, 18500, 28500, pagenum, &sz, tr ); // 右下
99 if (api_result < 0) {
100 print_error(api_result);
101 break;
102 }
103 }
104
105 // 変更をファイルに反映する
106 if (api_result >= 0) {
107 api_result = XDW_SaveDocument(h, NULL);
108 }
109
110 // 文書ハンドルを閉じる
111 XDW_CloseDocumentHandle(h, NULL);
112
113 if (api_result < 0) return 0;
114
115 return 1;
116 }