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 Chap1107 
10:       {
11:       
12:           public Chap1107() 
13:           {
14:           
15:               Console.WriteLine("Chapter 11 example 7: Outlines and Destinations");
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 writer = PdfWriter.GetInstance(document, new FileStream("Chap1107.pdf", FileMode.Create));
27:               
28:                   // step 3: we open the document
29:                   document.Open();
30:               
31:                   // step 4: we grab the ContentByte and do some stuff with it
32:                   PdfContentByte cb = writer.DirectContent;
33:               
34:                   // we create a PdfTemplate
35:                   PdfTemplate template = cb.CreateTemplate(25, 25);
36:               
37:                   // we add some crosses to visualize the destinations
38:                   template.MoveTo(13, 0);
39:                   template.LineTo(13, 25);
40:                   template.MoveTo(0, 13);
41:                   template.LineTo(50, 13);
42:                   template.Stroke();
43:               
44:                   // we add the template on different positions
45:                   cb.AddTemplate(template, 287, 787);
46:                   cb.AddTemplate(template, 187, 487);
47:                   cb.AddTemplate(template, 487, 287);
48:                   cb.AddTemplate(template, 87, 87);
49:               
50:                   // we define the destinations
51:                   PdfDestination d1 = new PdfDestination(PdfDestination.XYZ, 300, 800, 0);
52:                   PdfDestination d2 = new PdfDestination(PdfDestination.FITH, 500);
53:                   PdfDestination d3 = new PdfDestination(PdfDestination.FITR, 200, 300, 400, 500);
54:                   PdfDestination d4 = new PdfDestination(PdfDestination.FITBV, 100);
55:                   PdfDestination d5 = new PdfDestination(PdfDestination.FIT);
56:               
57:                   // we define the outlines
58:                   PdfOutline out1 = new PdfOutline(cb.RootOutline, d1, "root");
59:                   PdfOutline out2 = new PdfOutline(out1, d2, "sub 1");
60:                   PdfOutline out3 = new PdfOutline(out1, d3, "sub 2");
61:                   PdfOutline out4 = new PdfOutline(out2, d4, "sub 2.1");
62:                   PdfOutline out5 = new PdfOutline(out2, d5, "sub 2.2");
63:               
64:                   cb.AddOutline(out1);
65:                   cb.AddOutline(out2);
66:                   cb.AddOutline(out3);
67:                   cb.AddOutline(out4);
68:                   cb.AddOutline(out5);
69:               
70:               }
71:               catch(DocumentException de) 
72:               {
73:                   Console.Error.WriteLine(de.Message);
74:               }
75:               catch(IOException ioe) 
76:               {
77:                   Console.Error.WriteLine(ioe.Message);
78:               }
79:           
80:               // step 5: we close the document
81:               document.Close();
82:           }
83:       }
84:   }