view xdwaddatn.cpp @ 9:cc5262d43399

small changes.
author pyon@macmini
date Thu, 05 Apr 2018 21:06:13 +0900 (2018-04-05)
parents c1ebc8b218f2
children
line wrap: on
line source
/*--------------------------------------------------------------------------------
 * �@�\:
 *  DocuWorks�����̂��ׂẴy�[�W�Ƀe�L�X�g�A�m�e�[�V������\��t����
 *-------------------------------------------------------------------------------*/

#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���C���X�g�[������Ă��܂���B");
		break;
	case XDW_E_FILE_NOT_FOUND:
		fprintf(stderr, "�w�肳�ꂽ�t�@�C�������‚���܂���B");
		break;
	case XDW_E_ACCESSDENIED:
	case XDW_E_INVALID_NAME:
	case XDW_E_BAD_NETPATH:
		fprintf(stderr, "�w�肳�ꂽ�t�@�C�����J�����Ƃ��ł��܂���B");
		break;
	case XDW_E_BAD_FORMAT:
		fprintf(stderr, "�w�肳�ꂽ�t�@�C���͐������t�H�[�}�b�g�ł͂���܂���B");
		break;
	case XDW_E_INVALID_ACCESS:
		fprintf(stderr, "�w�肳�ꂽ��������錠��������܂���B");
		break;
	default:
		fprintf(stderr, "�G���[�R�[�h:%x\n", code);
		fprintf(stderr, "�G���[���������܂����B");
		break;
	}
}

int add_annotation(XDW_DOCUMENT_HANDLE h, int page, int x, int y, char* string, int* sz, int tr)
{
	// �e�L�X�g�A�m�e�[�V�������쐬����
	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;

	// �쐬�����A�m�e�[�V�����ɕ������ݒ肷��
	api_result = XDW_SetAnnotationAttribute(
		h, annoation, XDW_ATN_Text, XDW_ATYPE_STRING, string, 0, NULL); // �����e�L�X�g
	api_result = XDW_SetAnnotationAttribute(
		h, annoation, XDW_ATN_FontSize, XDW_ATYPE_INT, reinterpret_cast<char*>(sz), 0, NULL); // �t�H���g�T�C�Y
    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); // �w�i����
			h, annoation, XDW_ATN_BackColor, XDW_ATYPE_INT, reinterpret_cast<char*>(&color), 0, NULL); // �w�i����
    }

	return api_result;
}

int main(int argc, char** argv)
{
	int api_result = 0;

	if (argc != 8) {
		fprintf(stderr, "�g�p���@:\n");
        fprintf(stderr, "xdwaddatn �����t�@�C���� ���������� X���W Y���W �}�������� �T�C�Y �g�����X�y�A�����g\n");
		fprintf(stderr, "    ex. : xdwaddatn hoge.xdw searchtext 500 1000 Boooo! 120 1    # �w�i����\n");
		fprintf(stderr, "    ex. : xdwaddatn hoge.xdw searchtext 500 1000 Boooo! 120 0    # �w�i��\n");
		fprintf(stderr, "    ex. : xdwaddatn hoge.xdw *          500 1000 Boooo! 120 0    # ���ׂẴy�[�W��\n");
		return 0;
	}

	char in_path[_MAX_PATH];
	_fullpath(in_path, argv[1], _MAX_PATH);

	// �����n���h�����J��
	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��p���đ��y�[�W���𓾂�
	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] );   // �����t���O

	// �A�m�e�[�V������\��t����
	if ( !strcmp( "*", argv[2] ) ) {	// ���ׂẴy�[�W
		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 ) {   // �����q�b�g
				api_result = add_annotation(h, i, x, y, argv[5], &sz, tr);
				if (api_result < 0) {
					print_error(api_result);
					break;
				}
			}
		}
	}

	// �ύX���t�@�C���ɔ��f����
	if (api_result >= 0) {
		api_result = XDW_SaveDocument(h, NULL);
	}

	// �����n���h����‚���
	XDW_CloseDocumentHandle(h, NULL);

	if (api_result < 0) return 0;

	return 1;
}