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:       /// Text at absolute positions
11:       /// </summary>
12:       public class Chap1002 
13:       {
14:       
15:           public Chap1002() 
16:           {
17:           
18:               Console.WriteLine("Chapter 10 example 2: Text at absolute positions");
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("Chap1002.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:                   // first we draw some lines to be able to visualize the text alignment functions
38:                   cb.SetLineWidth(0f);
39:                   cb.MoveTo(250500);
40:                   cb.LineTo(250800);
41:                   cb.MoveTo(50700);
42:                   cb.LineTo(400700);
43:                   cb.MoveTo(50650);
44:                   cb.LineTo(400650);
45:                   cb.MoveTo(50600);
46:                   cb.LineTo(400600);
47:                   cb.Stroke();
48:               
49:                   // we tell the ContentByte we're ready to draw text
50:                   cb.BeginText();
51:               
52:                   BaseFont bf BaseFont.CreateFont(BaseFont.HELVETICABaseFont.CP1252BaseFont.NOT_EMBEDDED);
53:                   cb.SetFontAndSize(bf12);
54:                   String text "Sample text for alignment";
55:                   // we show some text starting on some absolute position with a given alignment
56:                   cb.ShowTextAligned(PdfContentByte.ALIGN_CENTERtext " Center"2507000);
57:                   cb.ShowTextAligned(PdfContentByte.ALIGN_RIGHTtext " Right"2506500);
58:                   cb.ShowTextAligned(PdfContentByte.ALIGN_LEFTtext " Left"2506000);
59:               
60:                   // we draw some text on a certain position
61:                   cb.SetTextMatrix(100400);
62:                   cb.ShowText("Text at position 100,400.");
63:               
64:                   // we draw some rotated text on a certain position
65:                   cb.SetTextMatrix(01, -10100300);
66:                   cb.ShowText("Text at position 100,300, rotated 90 degrees.");
67:               
68:                   // we draw some mirrored, rotated text on a certain position
69:                   cb.SetTextMatrix(0110200200);
70:                   cb.ShowText("Text at position 200,200, mirrored and rotated 90 degrees.");
71:               
72:                   // we tell the contentByte, we've finished drawing text
73:                   cb.EndText();
74:               }
75:               catch(DocumentException de
76:               {
77:                   Console.Error.WriteLine(de.Message);
78:               }
79:               catch(IOException ioe
80:               {
81:                   Console.Error.WriteLine(ioe.Message);
82:               }
83:           
84:               // step 5: we close the document
85:               document.Close();
86:           }
87:       }
88:   }