1 /*
2  * << Haru Free PDF Library 2.0.0 >> -- link_annotation.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 
21 jmp_buf env;
22 
23 #ifdef HPDF_DLL
24 void  __stdcall
25 #else
26 void
27 #endif
error_handler(HPDF_STATUS error_no,HPDF_STATUS detail_no,void * user_data)28 error_handler  (HPDF_STATUS   error_no,
29                 HPDF_STATUS   detail_no,
30                 void         *user_data)
31 {
32     printf ("ERROR: error_no=%04X, detail_no=%u\n", (HPDF_UINT)error_no,
33                 (HPDF_UINT)detail_no);
34     longjmp(env, 1);
35 }
36 
37 
38 void
print_page(HPDF_Page page,HPDF_Font font,int page_num)39 print_page  (HPDF_Page page, HPDF_Font font, int page_num)
40 {
41     char buf[50];
42 
43     HPDF_Page_SetWidth (page, 200);
44     HPDF_Page_SetHeight (page, 200);
45 
46     HPDF_Page_SetFontAndSize (page, font, 20);
47 
48     HPDF_Page_BeginText (page);
49     HPDF_Page_MoveTextPos (page, 50, 150);
50 #ifdef __WIN32__
51     _snprintf(buf, 50, "Page:%d", page_num);
52 #else
53     snprintf(buf, 50, "Page:%d", page_num);
54 #endif
55     HPDF_Page_ShowText (page, buf);
56     HPDF_Page_EndText (page);
57 }
58 
main(int argc,char ** argv)59 int main(int argc, char **argv)
60 {
61     HPDF_Doc  pdf;
62     HPDF_Font font;
63     HPDF_Page index_page;
64     HPDF_Page page[9];
65     HPDF_Destination dst;
66     char fname[256];
67     HPDF_Rect rect;
68     HPDF_Point tp;
69     HPDF_Annotation annot;
70     HPDF_UINT i;
71     const char *uri = "http://libharu.org";
72 
73     strcpy (fname, argv[0]);
74     strcat (fname, ".pdf");
75 
76     pdf = HPDF_New (error_handler, NULL);
77     if (!pdf) {
78         printf ("error: cannot create PdfDoc object\n");
79         return 1;
80     }
81 
82     if (setjmp(env)) {
83         HPDF_Free (pdf);
84         return 1;
85     }
86 
87     /* create default-font */
88     font = HPDF_GetFont (pdf, "Helvetica", NULL);
89 
90     /* create index page */
91     index_page = HPDF_AddPage (pdf);
92     HPDF_Page_SetWidth (index_page, 300);
93     HPDF_Page_SetHeight (index_page, 220);
94 
95     /* Add 7 pages to the document. */
96     for (i = 0; i < 7; i++) {
97         page[i] = HPDF_AddPage (pdf);
98         print_page(page[i], font, i + 1);
99     }
100 
101     HPDF_Page_BeginText (index_page);
102     HPDF_Page_SetFontAndSize (index_page, font, 10);
103     HPDF_Page_MoveTextPos (index_page, 15, 200);
104     HPDF_Page_ShowText (index_page, "Link Annotation Demo");
105     HPDF_Page_EndText (index_page);
106 
107     /*
108      * Create Link-Annotation object on index page.
109      */
110     HPDF_Page_BeginText(index_page);
111     HPDF_Page_SetFontAndSize (index_page, font, 8);
112     HPDF_Page_MoveTextPos (index_page, 20, 180);
113     HPDF_Page_SetTextLeading (index_page, 23);
114 
115     /* page1 (HPDF_ANNOT_NO_HIGHTLIGHT) */
116     tp = HPDF_Page_GetCurrentTextPos (index_page);
117 
118     HPDF_Page_ShowText (index_page, "Jump to Page1 (HilightMode=HPDF_ANNOT_NO_HIGHTLIGHT)");
119     rect.left = tp.x - 4;
120     rect.bottom = tp.y - 4;
121     rect.right = HPDF_Page_GetCurrentTextPos (index_page).x + 4;
122     rect.top = tp.y + 10;
123 
124     HPDF_Page_MoveToNextLine (index_page);
125 
126     dst = HPDF_Page_CreateDestination (page[0]);
127 
128     annot = HPDF_Page_CreateLinkAnnot (index_page, rect, dst);
129 
130     HPDF_LinkAnnot_SetHighlightMode (annot, HPDF_ANNOT_NO_HIGHTLIGHT);
131 
132 
133     /* page2 (HPDF_ANNOT_INVERT_BOX) */
134     tp = HPDF_Page_GetCurrentTextPos (index_page);
135 
136     HPDF_Page_ShowText (index_page, "Jump to Page2 (HilightMode=HPDF_ANNOT_INVERT_BOX)");
137     rect.left = tp.x - 4;
138     rect.bottom = tp.y - 4;
139     rect.right = HPDF_Page_GetCurrentTextPos (index_page).x + 4;
140     rect.top = tp.y + 10;
141 
142     HPDF_Page_MoveToNextLine (index_page);
143 
144     dst = HPDF_Page_CreateDestination (page[1]);
145 
146     annot = HPDF_Page_CreateLinkAnnot (index_page, rect, dst);
147 
148     HPDF_LinkAnnot_SetHighlightMode (annot, HPDF_ANNOT_INVERT_BOX);
149 
150 
151     /* page3 (HPDF_ANNOT_INVERT_BORDER) */
152     tp = HPDF_Page_GetCurrentTextPos (index_page);
153 
154     HPDF_Page_ShowText (index_page, "Jump to Page3 (HilightMode=HPDF_ANNOT_INVERT_BORDER)");
155     rect.left = tp.x - 4;
156     rect.bottom = tp.y - 4;
157     rect.right = HPDF_Page_GetCurrentTextPos (index_page).x + 4;
158     rect.top = tp.y + 10;
159 
160     HPDF_Page_MoveToNextLine (index_page);
161 
162     dst = HPDF_Page_CreateDestination (page[2]);
163 
164     annot = HPDF_Page_CreateLinkAnnot (index_page, rect, dst);
165 
166     HPDF_LinkAnnot_SetHighlightMode (annot, HPDF_ANNOT_INVERT_BORDER);
167 
168 
169     /* page4 (HPDF_ANNOT_DOWN_APPEARANCE) */
170     tp = HPDF_Page_GetCurrentTextPos (index_page);
171 
172     HPDF_Page_ShowText (index_page, "Jump to Page4 (HilightMode=HPDF_ANNOT_DOWN_APPEARANCE)");
173     rect.left = tp.x - 4;
174     rect.bottom = tp.y - 4;
175     rect.right = HPDF_Page_GetCurrentTextPos (index_page).x + 4;
176     rect.top = tp.y + 10;
177 
178     HPDF_Page_MoveToNextLine (index_page);
179 
180     dst = HPDF_Page_CreateDestination (page[3]);
181 
182     annot = HPDF_Page_CreateLinkAnnot (index_page, rect, dst);
183 
184     HPDF_LinkAnnot_SetHighlightMode (annot, HPDF_ANNOT_DOWN_APPEARANCE);
185 
186 
187     /* page5 (dash border) */
188     tp = HPDF_Page_GetCurrentTextPos (index_page);
189 
190     HPDF_Page_ShowText (index_page, "Jump to Page5 (dash border)");
191     rect.left = tp.x - 4;
192     rect.bottom = tp.y - 4;
193     rect.right = HPDF_Page_GetCurrentTextPos (index_page).x + 4;
194     rect.top = tp.y + 10;
195 
196     HPDF_Page_MoveToNextLine (index_page);
197 
198     dst = HPDF_Page_CreateDestination (page[4]);
199 
200     annot = HPDF_Page_CreateLinkAnnot (index_page, rect, dst);
201 
202     HPDF_LinkAnnot_SetBorderStyle (annot, 1, 3, 2);
203 
204 
205     /* page6 (no border) */
206     tp = HPDF_Page_GetCurrentTextPos (index_page);
207 
208     HPDF_Page_ShowText (index_page, "Jump to Page6 (no border)");
209     rect.left = tp.x - 4;
210     rect.bottom = tp.y - 4;
211     rect.right = HPDF_Page_GetCurrentTextPos (index_page).x + 4;
212     rect.top = tp.y + 10;
213 
214     HPDF_Page_MoveToNextLine (index_page);
215 
216     dst = HPDF_Page_CreateDestination (page[5]);
217 
218     annot = HPDF_Page_CreateLinkAnnot (index_page, rect, dst);
219 
220     HPDF_LinkAnnot_SetBorderStyle (annot, 0, 0, 0);
221 
222 
223     /* page7 (bold border) */
224     tp = HPDF_Page_GetCurrentTextPos (index_page);
225 
226     HPDF_Page_ShowText (index_page, "Jump to Page7 (bold border)");
227     rect.left = tp.x - 4;
228     rect.bottom = tp.y - 4;
229     rect.right = HPDF_Page_GetCurrentTextPos (index_page).x + 4;
230     rect.top = tp.y + 10;
231 
232     HPDF_Page_MoveToNextLine (index_page);
233 
234     dst = HPDF_Page_CreateDestination (page[6]);
235 
236     annot = HPDF_Page_CreateLinkAnnot (index_page, rect, dst);
237 
238     HPDF_LinkAnnot_SetBorderStyle (annot, 2, 0, 0);
239 
240 
241     /* URI link */
242     tp = HPDF_Page_GetCurrentTextPos (index_page);
243 
244     HPDF_Page_ShowText (index_page, "URI (");
245     HPDF_Page_ShowText (index_page, uri);
246     HPDF_Page_ShowText (index_page, ")");
247 
248     rect.left = tp.x - 4;
249     rect.bottom = tp.y - 4;
250     rect.right = HPDF_Page_GetCurrentTextPos (index_page).x + 4;
251     rect.top = tp.y + 10;
252 
253     HPDF_Page_CreateURILinkAnnot (index_page, rect, uri);
254 
255     HPDF_Page_EndText (index_page);
256 
257     /* save the document to a file */
258     HPDF_SaveToFile (pdf, fname);
259 
260     /* clean up */
261     HPDF_Free (pdf);
262 
263     return 0;
264 }
265