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(documentnew 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(2525);
36:               
37:                   // we add some crosses to visualize the destinations
38:                   template.MoveTo(130);
39:                   template.LineTo(1325);
40:                   template.MoveTo(013);
41:                   template.LineTo(5013);
42:                   template.Stroke();
43:               
44:                   // we add the template on different positions
45:                   cb.AddTemplate(template287787);
46:                   cb.AddTemplate(template187487);
47:                   cb.AddTemplate(template487287);
48:                   cb.AddTemplate(template8787);
49:               
50:                   // we define the destinations
51:                   PdfDestination d1 new PdfDestination(PdfDestination.XYZ3008000);
52:                   PdfDestination d2 new PdfDestination(PdfDestination.FITH500);
53:                   PdfDestination d3 new PdfDestination(PdfDestination.FITR200300400500);
54:                   PdfDestination d4 new PdfDestination(PdfDestination.FITBV100);
55:                   PdfDestination d5 new PdfDestination(PdfDestination.FIT);
56:               
57:                   // we define the outlines
58:                   PdfOutline out1 new PdfOutline(cb.RootOutlined1"root");
59:                   PdfOutline out2 new PdfOutline(out1d2"sub 1");
60:                   PdfOutline out3 new PdfOutline(out1d3"sub 2");
61:                   PdfOutline out4 new PdfOutline(out2d4"sub 2.1");
62:                   PdfOutline out5 new PdfOutline(out2d5"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:   }