changeset 5:55f10a3aacb0

add xdwaddpage2.
author pyon@macmini
date Sun, 29 Oct 2017 14:48:51 +0900
parents af8f454d20ed
children a4af60a0ec32
files xdwaddpage2.cpp
diffstat 1 files changed, 168 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/xdwaddpage2.cpp	Sun Oct 29 14:48:51 2017 +0900
@@ -0,0 +1,168 @@
+/* Makefile
+debug: xdwaddpage2.c
+	#cls
+	gcc -g -O0 -I. xdwaddpage2.c xdwapi.lib
+
+release: xdwaddpage2.c
+	gcc -I. xdwaddpage2.c xdwapi.lib -static -o xdwaddpage2.exe
+	strip xdwaddpage2.exe
+
+clean:
+	rm -rf tempXXXX
+*/
+
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <time.h>
+#include <io.h>
+#include <windows.h>
+#include <xdw_api.h>
+#include <xdwapian.h>
+
+
+void print_error( int code ) {
+	fprintf( stderr, "Error code : %d\n", code );
+	switch ( code ) {
+	case XDW_E_NOT_INSTALLED:
+		fprintf( stderr, "DocuWorksがインストールされていません。" );
+		break;
+	case XDW_E_FILE_NOT_FOUND:
+		fprintf( stderr, "指定されたファイルが見つかりません。" );
+		break;
+	case XDW_E_FILE_EXISTS:
+		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, "エラーが発生しました。" );
+		break;
+	}
+}
+
+void print_now( char const *msg ) {
+   time_t now = time( NULL );
+   struct tm *ts = localtime( &now );
+
+   char buf[80];
+   strftime( buf, sizeof( buf ), "%H:%M:%S %Z", ts );
+   printf( "%s\t%s\n", buf, msg );
+}
+
+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[] ) {
+
+	/* オプションの解析 */
+	char prog[128];
+	strcpy( prog, argv[0] );
+
+	int pnow = 0;
+	int pstart = 0;
+	int pleft = 0;
+	char c;
+	while ( --argc > 0 && ( *++argv )[0] == '-' ) {
+		while ( c = *++argv[0] ) {
+			switch ( c ) {
+				case 'v':
+					pnow = 1;
+					print_now( "start." );
+					break;
+				case 'l':
+					pleft = 1;
+					break;
+				case 's':
+					pstart = 1;
+					break;
+				default:
+					fprintf( stderr, "error: illegal option '%c'.\n", c );
+					exit( 1 );
+			}
+		}
+	}
+
+	int sp = 1;
+	char in_path[ _MAX_PATH ];
+	if ( argc == 1 ) {
+		_fullpath( in_path, argv[0], _MAX_PATH );
+	} else if ( argc == 2 ) {
+		if ( pstart == 1 ) {
+			sp = atoi( argv[0] );
+			_fullpath( in_path, argv[1], _MAX_PATH );
+		}
+	} else {
+		fprintf( stderr, "%s infile\n", prog );
+		fprintf( stderr, "%s -l infile\n", prog );
+		fprintf( stderr, "%s -s S infile\n", prog );
+		fprintf( stderr, "%s -v infile\n", prog );
+		exit( 1 );
+	}
+
+	/* 本処理 */
+	if ( pnow ) print_now( "adding pages." );
+
+	int api_result = 0;
+
+	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_DOCUMENT_INFO info = { sizeof( XDW_DOCUMENT_INFO ), 0 }; // XDW_GetDocumentInformationを用いて総ページ数を得る
+	XDW_GetDocumentInformation( h, &info );
+	int last_page = info.nPages;
+
+    int sz = 80;    // 文字サイズ
+    int tr = 1;     // 透明フラグ
+    char pagenum[18];
+
+	// アノテーションを貼り付ける
+    for ( int p = 0; p < last_page; p++ ) {
+        sprintf( pagenum, "%05d-%05d-%05d", p + sp, p + 1,last_page );
+		if ( pleft == 1 )
+			api_result = add_annotation( h, p + 1,  1700,  5600, pagenum, &sz, tr );    // 左上
+        api_result = add_annotation( h, p + 1, 17500, 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 ( pnow ) print_now( "done." );
+	return 0;
+}
+