1 /*
2 * << H a r u --free pdf library >> -- LinkExample.cpp
3 *
4 * Copyright (c) 1999-2003 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 "libharu.h"
16
17 /*------ Simple example for Link-Annotation ----------------------------------*/
18
19 void PrintPage(PdfContents* canvas, int page);
20
PrintPage(PdfContents * canvas,int page)21 void PrintPage(PdfContents* canvas, int page)
22 {
23 char buf[50];
24
25 canvas->BeginText();
26 canvas->SetFontAndSize("Helvetica", 20);
27 canvas->MoveTextPos(50, 150);
28 #ifdef __WIN32__
29 _snprintf(buf, 50, "Page:%d", page);
30 #else
31 snprintf(buf, 50, "Page:%d", page);
32 #endif
33 canvas->ShowText(buf);
34 canvas->EndText();
35 }
36
main()37 int main()
38 {
39 /* Create a PdfDoc object and start to make new PDF file. */
40 PdfDoc* doc = new PdfDoc();
41 try {
42 doc->NewDoc();
43
44 /* Create font object and register it to the PdfDoc object.*/
45 doc->AddType1Font(new PdfHelveticaFontDef());
46
47 /* Add new page(index page) and set the size of the page. */
48 PdfPage* index_page = doc->AddPage();
49 index_page->SetSize(200, 200);
50
51 /* Add new page (page 2). */
52 PdfPage* page2 = doc->RootPages()->AddPage();
53 page2->SetSize(200, 200);
54 PrintPage(page2->Canvas(), 2);
55
56 /* Add new page (page 3). */
57 PdfPage* page3 = doc->RootPages()->AddPage();
58 page3->SetSize(200, 200);
59 PrintPage(page3->Canvas(), 3);
60
61 /* Add new page (page 4). */
62 PdfPage* page4 = doc->RootPages()->AddPage();
63 page4->SetSize(200, 200);
64 PrintPage(page4->Canvas(), 4);
65
66 /* make Destination objects for Link-Annotation. */
67 PdfDestination* dest1 = new PdfDestination(page2, true);
68 PdfDestination* dest2 = new PdfDestination(page3);
69 PdfDestination* dest3 = new PdfDestination(page4);
70
71 /*
72 * Create Link-Annotation object with these Destination objects
73 * on index page
74 */
75 PdfContents* canvas = index_page->Canvas();
76 canvas->BeginText();
77 canvas->SetFontAndSize("Helvetica", 15);
78 canvas->MoveTextPos(50, 150);
79 canvas->SetTextLeading(25);
80
81 int text_width = (int)canvas->TextWidth("Jump to PageX");
82 pdf_rect rect = PdfRect(canvas->TextPos().x,
83 canvas->TextPos().y,
84 canvas->TextPos().x + text_width,
85 canvas->TextPos().y + 20);
86 index_page->AddLink(rect, dest1);
87 canvas->ShowText("Jump to Page2");
88 canvas->MoveToNextLine();
89
90 rect.bottom = canvas->TextPos().y;
91 rect.top = rect.bottom + 20;
92 index_page->AddLink(rect, dest2);
93
94 canvas->ShowText("Jump to Page3");
95 canvas->MoveToNextLine();
96
97 rect.bottom = canvas->TextPos().y;
98 rect.top = rect.bottom + 20;
99 index_page->AddLink(rect, dest3);
100
101 canvas->ShowText("Jump to Page4");
102 canvas->MoveToNextLine();
103
104 canvas->EndText();
105
106 /* Save the document to the file. */
107 doc->WriteToFile("LinkExample.pdf");
108 } catch (PDF_STD_EXCEPTION& e) {
109 fprintf(stderr, "%s\n", e.what());
110 delete doc;
111 return 1;
112 }
113 delete doc;
114 return 0;
115 }
116
117