Blog

 
1:   using System;
2:   using System.IO;
3:  
4:   using iTextSharp.text;
5:   using iTextSharp.text.pdf;
6:  
7:   namespace iTextSharp.tutorial.Chap10
8:   {
9:       /// <summary>
10:       /// Simple Graphic
11:       /// </summary>
12:       public class Chap1001 
13:       {
14:       
15:           public Chap1001() 
16:           {
17:           
18:               Console.WriteLine("Chapter 10 example 1: Simple Graphic");
19:           
20:               // step 1: creation of a document-object
21:               Document document new Document();
22:           
23:               try 
24:               {
25:               
26:                   // step 2:
27:                   // we create a writer that listens to the document
28:                   // and directs a PDF-stream to a file
29:                   PdfWriter writer PdfWriter.GetInstance(documentnew FileStream("Chap1001.pdf"FileMode.Create));
30:               
31:                   // step 3: we open the document
32:                   document.Open();
33:               
34:                   // step 4: we grab the ContentByte and do some stuff with it
35:                   PdfContentByte cb writer.DirectContent;
36:               
37:                   // an example of a rectangle with a diagonal in very thick lines
38:                   //cb.SetLineWidth(10f;
39:                   cb.SetLineWidth(10f);
40:                   // draw a rectangle
41:                   cb.Rectangle(100700100100);
42:                   // add the diagonal
43:                   cb.MoveTo(100700);
44:                   cb.LineTo(200800);
45:                   // stroke the lines
46:                   cb.Stroke();
47:               
48:                   // an example of some circles
49:                   cb.SetLineDash(330);
50:                   cb.SetRGBColorStrokeF(0f255f0f);
51:                   cb.Circle(150f500f100f);
52:                   cb.Stroke();
53:               
54:                   cb.SetLineWidth(5f);
55:                   cb.ResetRGBColorStroke();
56:                   cb.Circle(150f500f50f);
57:                   cb.Stroke();
58:               
59:                   // example with colorfill
60:                   cb.SetRGBColorFillF(0f255f0f);
61:                   cb.MoveTo(100f200f);
62:                   cb.LineTo(200f250f);
63:                   cb.LineTo(400f150f);
64:                   // because we change the fill color BEFORE we stroke the triangle
65:                   // the color of the triangle will be red instead of green
66:                   cb.SetRGBColorFillF(255f0f0f);
67:                   cb.ClosePathFillStroke();
68:               }
69:               catch(DocumentException de
70:               {
71:                   Console.Error.WriteLine(de.Message);
72:               }
73:               catch(IOException ioe
74:               {
75:                   Console.Error.WriteLine(ioe.Message);
76:               }
77:           
78:               // step 5: we close the document
79:               document.Close();
80:           }
81:       }
82:   }