Blog

 
1:   using System;
2:   using System.IO;
3:  
4:   using iTextSharp.text;
5:   using iTextSharp.text.pdf;
6:  
7:   namespace iTextSharp.tutorial.Chap03
8:   {
9:       /// <summary>
10:       /// Annotations:Text,External links,External PDF file,Named action,Application
11:       /// </summary>
12:       public class Chap0304
13:       {
14:           public Chap0304()
15:           {
16:               Console.WriteLine("Chapter 3 example 4: annotations at absolute positions");
17:           
18:               // step 1: creation of a document-object
19:               Document document = new Document(PageSize.A4, 50, 50, 50, 50);
20:               try 
21:               {
22:                   // step 2:
23:                   // we create a writer that listens to the document
24:                   PdfWriter writer = PdfWriter.GetInstance(document, new FileStream("Chap0304.pdf", FileMode.Create));
25:                   // step 3: we Open the document
26:                   document.Open();
27:                   // step 4: we Add some content
28:               
29:                   PdfContentByte cb = writer.DirectContent;
30:  
31:                   // draw a rectangle
32:                   cb.SetRGBColorStroke(0x00, 0x00, 0xFF);
33:                   cb.Rectangle(100, 700, 100, 100);
34:                   cb.Stroke();
35:                   Annotation annot = new Annotation(100f, 700f, 200f, 800f, "http://itextsharp.Sourceforge.net");
36:                   document.Add(annot);
37:                   cb.SetRGBColorStroke(0xFF, 0x00, 0x00);
38:                   cb.Rectangle(200, 700, 100, 100);
39:                   cb.Stroke();
40:                   try 
41:                   {
42:                       document.Add(new Annotation(200f, 700f, 300f, 800f, new Uri("http://itextsharp.Sourceforge.net")));
43:                   }
44:                   catch 
45:                   {
46:                   }
47:                   cb.SetRGBColorStroke(0x00, 0xFF, 0x00);
48:                   cb.Rectangle(300, 700, 100, 100);
49:                   cb.Stroke();
50:                   document.Add(new Annotation(300f, 700f, 400f, 800f, "c:/winnt/notepad.exe", null, null, null));
51:                   cb.SetRGBColorStroke(0x00, 0x00, 0xFF);
52:                   cb.Rectangle(100, 500, 100, 100);
53:                   cb.Stroke();
54:                   document.Add(new Annotation("annotation", "This annotation is placed on an absolute position", 100f, 500f, 200f, 600f));
55:                   cb.SetRGBColorStroke(0xFF, 0x00, 0x00);
56:                   cb.Rectangle(200, 500, 100, 100);
57:                   cb.Stroke();
58:                   document.Add(new Annotation(200f, 500f, 300f, 600f, "Chap1102a.pdf", "test"));
59:                   cb.SetRGBColorStroke(0x00, 0xFF, 0x00);
60:                   cb.Rectangle(300, 500, 100, 100);
61:                   cb.Stroke();
62:                   document.Add(new Annotation(300f, 500f, 400f, 600f, "Chap1102b.pdf", 3));
63:               }
64:               catch (Exception de) 
65:               {
66:                   Console.WriteLine(de.StackTrace);
67:               }
68:           
69:               // step 5: we close the document
70:               document.Close();
71:  
72:           }
73:       }
74:   }