Blog

 
1:   using System;
2:   using System.IO;
3:  
4:   using iTextSharp.text;
5:   using iTextSharp.text.pdf;
6:  
7:   namespace iTextSharp.tutorial.Chap11
8:   {
9:       public class Chap1102 
10:       {
11:       
12:           public Chap1102() 
13:           {
14:           
15:               Console.WriteLine("Chapter 11 example 2: anchor and remote goto");
16:           
17:               // step 1: creation of a document-object
18:               Document document new Document();
19:           
20:               try 
21:               {
22:               
23:                   // step 2:
24:                   // we create a writer that listens to the document
25:                   // and directs a PDF-stream to a file
26:                   PdfWriter writerA PdfWriter.GetInstance(documentnew FileStream("Chap1102a.pdf"FileMode.Create));
27:                   PdfWriter writerB PdfWriter.GetInstance(documentnew FileStream("Chap1102b.pdf"FileMode.Create));
28:               
29:                   // step 3: we open the document
30:                   document.Open();
31:               
32:                   // step 4: we add some content
33:               
34:                   Paragraph p1 new Paragraph("We discussed anchors in chapter 3, but you can add an URL to a chunk to to make it an "FontFactory.GetFont(FontFactory.HELVETICA12));
35:                   p1.Add(new Chunk("anchor"FontFactory.GetFont(FontFactory.HELVETICA12Font.UNDERLINEnew Color(00255))).SetAnchor(new Uri("http://iTextSharp.Sourceforge.Net")));
36:                   p1.Add(" you will automatically jump to another location in this document.");
37:                   Paragraph p2 new Paragraph("blah, blah, blah");
38:                   Paragraph p3a new Paragraph("This paragraph contains a ");
39:                   p3a.Add(new Chunk("local destination in document A"FontFactory.GetFont(FontFactory.HELVETICA12Font.NORMALnew Color(02550))).SetLocalDestination("test"));
40:                   Paragraph p3b new Paragraph("This paragraph contains a local ");
41:                   p3b.Add(new Chunk("local destination in document B"FontFactory.GetFont(FontFactory.HELVETICA12Font.NORMALnew Color(02550))).SetLocalDestination("test"));
42:                   Paragraph p4a new Paragraph(new Chunk("Click this paragraph to go to a certain destination on document B").SetRemoteGoto("Chap1102b.pdf""test"));
43:                   Paragraph p4b new Paragraph(new Chunk("Click this paragraph to go to a certain destination on document A").SetRemoteGoto("Chap1102a.pdf""test"));
44:                   Paragraph p5a new Paragraph("you can also jump to a ");
45:                   p5a.Add(new Chunk("specific page on another document"FontFactory.GetFont(FontFactory.HELVETICA12Font.ITALIC)).SetRemoteGoto("Chap1102b.pdf"3));
46:                   document.Add(p1);
47:                   document.Add(p2);
48:                   document.Add(p2);
49:                   document.Add(p2);
50:                   document.Add(p2);
51:                   document.Add(p2);
52:                   document.Add(p2);
53:                   document.Add(p2);
54:                   writerA.Pause();
55:                   document.Add(p4b);
56:                   writerA.Resume();
57:                   writerB.Pause();
58:                   document.Add(p4a);
59:                   document.Add(p5a);
60:                   writerB.Resume();
61:                   document.Add(p2);
62:                   document.Add(p2);
63:                   document.Add(p2);
64:                   document.Add(p2);
65:                   writerA.Pause();
66:                   document.Add(p3b);
67:                   document.Add(p2);
68:                   document.Add(p2);
69:                   document.NewPage();
70:                   document.Add(p2);
71:                   document.Add(p2);
72:                   document.NewPage();
73:                   writerA.Resume();
74:                   writerB.Pause();
75:                   document.Add(p3a);
76:                   writerB.Resume();
77:                   document.Add(p2);
78:                   document.Add(p2);
79:               }
80:               catch(DocumentException de
81:               {
82:                   Console.Error.WriteLine(de.Message);
83:               }
84:               catch(IOException ioe
85:               {
86:                   Console.Error.WriteLine(ioe.Message);
87:               }
88:               catch(Exception e
89:               {
90:                   Console.Error.WriteLine(e.Message);
91:               }
92:           
93:               // step 5: we close the document
94:               document.Close();
95:           }
96:       }
97:   }