OSDN Git Service

binding with libharu.
[putex/putex.git] / src / texsourc / lib / libhpdf / demo / permission.c
1 /*
2  * << Haru Free PDF Library 2.0.0 >> -- permission.c
3  *
4  * Copyright (c) 1999-2006 Takeshi Kanno <takeshi_kanno@est.hi-ho.ne.jp>
5  *
6  * Permission to use, copy, modify, distribute and sell this software
7  * and its documentation for any purpose is hereby granted without fee,
8  * provided that the above copyright notice appear in all copies and
9  * that both that copyright notice and this permission notice appear
10  * in supporting documentation.
11  * It is provided "as is" without express or implied warranty.
12  *
13  */
14
15 #include <stdlib.h>
16 #include <stdio.h>
17 #include <string.h>
18 #include <setjmp.h>
19 #include "hpdf.h"
20 #include "grid_sheet.h"
21
22
23 static const char* text = "User cannot print and copy this document.";
24 static const char* owner_passwd = "owner";
25 static const char* user_passwd = "";
26
27 jmp_buf env;
28
29 #ifdef HPDF_DLL
30 void  __stdcall
31 #else
32 void
33 #endif
34 error_handler  (HPDF_STATUS   error_no,
35                 HPDF_STATUS   detail_no,
36                 void         *user_data)
37 {
38     printf ("ERROR: error_no=%04X, detail_no=%u\n", (HPDF_UINT)error_no,
39                 (HPDF_UINT)detail_no);
40     longjmp(env, 1);
41 }
42
43 int
44 main (int argc, char **argv)
45 {
46     HPDF_Doc  pdf;
47     HPDF_Font font;
48     HPDF_Page page;
49     char fname[256];
50     HPDF_REAL tw;
51
52     strcpy (fname, argv[0]);
53     strcat (fname, ".pdf");
54
55     pdf = HPDF_New (error_handler, NULL);
56     if (!pdf) {
57         printf ("error: cannot create PdfDoc object\n");
58         return 1;
59     }
60
61     if (setjmp(env)) {
62         HPDF_Free (pdf);
63         return 1;
64     }
65
66     /* create default-font */
67     font = HPDF_GetFont (pdf, "Helvetica", NULL);
68
69     /* add a new page object. */
70     page = HPDF_AddPage (pdf);
71
72     HPDF_Page_SetSize (page, HPDF_PAGE_SIZE_B5, HPDF_PAGE_LANDSCAPE);
73
74     HPDF_Page_BeginText (page);
75     HPDF_Page_SetFontAndSize (page, font, 20);
76     tw = HPDF_Page_TextWidth (page, text);
77     HPDF_Page_MoveTextPos (page, (HPDF_Page_GetWidth (page) - tw) / 2,
78                 (HPDF_Page_GetHeight (page)  - 20) / 2);
79     HPDF_Page_ShowText (page, text);
80     HPDF_Page_EndText (page);
81
82     HPDF_SetPassword (pdf, owner_passwd, user_passwd);
83     HPDF_SetPermission (pdf, HPDF_ENABLE_READ);
84     HPDF_SetEncryptionMode (pdf, HPDF_ENCRYPT_R3, 16);
85
86     /* save the document to a file */
87     HPDF_SaveToFile (pdf, fname);
88
89     /* clean up */
90     HPDF_Free (pdf);
91
92     return 0;
93 }
94