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:       public class Chap1015 
10:       {
11:       
12:           public Chap1015() 
13:           {
14:           
15:               Console.WriteLine("Chapter 10 Example 15: Tiled Patterns");
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("Chap1015.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:                   BaseFont bf BaseFont.CreateFont("Times-Roman""winansi"false);
35:               
36:                   // step 5: we create some PdfPatternPainter instances for drawing path, text, or placing image
37:               
38:                   // Image instance to be placed in PdfPatternPainter canvas. Any nice one?
39:                   Image img Image.GetInstance("pngnow.png");
40:  
41:                   PdfPatternPainter p cb.CreatePattern(60f60f60f60f);
42:                   PdfPatternPainter p1 cb.CreatePattern(60f60f60f60f);
43:                   PdfPatternPainter p2 cb.CreatePattern(img.ScaledWidthimg.ScaledHeightimg.ScaledWidthimg.ScaledHeight);
44:               
45:               
46:                   // step 6: put your drawing instruction in the painter canvas
47:               
48:                   // A star pattern taken from Adobe PDF Reference Book p.207
49:                   String star "0.3 g\n15.000 27.000 m\n"
50:                       "7.947 5.292 l\n26.413 18.708 l\n"
51:                       "3.587 18.708 l\n22.053 5.292 l\nf\n"
52:                       "45.000 57.000 m\n37.947 35.292 l\n"
53:                       "56.413 48.708 l\n33.587 48.708 l\n"
54:                       "52.053 35.292 l\nf\n"
55:                       "0.7 g\n15.000 57.000 m\n"
56:                       "7.947 35.292 l\n26.413 48.708 l\n"
57:                       "3.587 48.708 l\n22.053 35.292 l\nf\n"
58:                       "45.000 27.000 m\n37.947 5.292 l\n"
59:                       "56.413 18.708 l\n33.587 18.708 l\n"
60:                       "52.053 5.292 l\nf";
61:               
62:                   p.SetLiteral(star);
63:               
64:                   // A Pattern with some text drawing                
65:                   p1.SetGrayFill(0.3f);
66:                   p1.SetFontAndSize(bf12);
67:                   p1.BeginText();
68:                   p1.SetTextMatrix(1f0f0f1f0f0f);
69:                   p1.ShowText("A B C D");
70:                   p1.EndText();
71:                   p1.MoveTo(0f0f);
72:                   p1.LineTo(60f60f);
73:                   p1.Stroke();
74:               
75:                   // A pattern with an image and position
76:                   p2.AddImage(imgimg.ScaledWidth0f0fimg.ScaledHeight0f0f);
77:                   p2.SetPatternMatrix(1f0f0f1f60f60f);
78:               
79:                   // See if we can apply the pattern color to chunk, phrase or paragraph
80:                   PatternColor pat new PatternColor(p);
81:                   PatternColor pat1 new PatternColor(p1);
82:                   PatternColor pat2 new PatternColor(p2);
83:                   String text "Text with pattern";
84:                   document.Add(new Paragraph(textFontFactory.GetFont(FontFactory.HELVETICA60Font.BOLDnew GrayColor(0.3f))));
85:                   document.Add(new Paragraph(textFontFactory.GetFont(FontFactory.HELVETICA60Font.BOLDpat)));
86:               
87:                   // draw a rectangle filled with star pattern
88:                   cb.SetPatternFill(p);
89:                   cb.SetGrayStroke(0.0f);
90:                   cb.Rectangle(2020284120);
91:                   cb.FillStroke();
92:               
93:                   // draw some characters filled with star.
94:                   // Note: A gray, rgb, cmyk or spot color should be applied first
95:                   // otherwise, you will not be able to see the character glyph
96:                   // since the glyph path is filled by pattern
97:                   cb.BeginText();
98:                   cb.SetFontAndSize(bf1);
99:                   cb.SetTextMatrix(270f0f0f270f20f100f);
100:                   cb.SetGrayFill(0.9f);
101:                   cb.ShowText("ABC");
102:                   cb.SetPatternFill(p);
103:                   cb.MoveTextWithLeading(0.0f0.0f);
104:                   cb.ShowText("ABC");
105:                   cb.EndText();
106:                   cb.SetPatternFill(p);
107:               
108:                   // draw a circle. Similar to rectangle
109:                   cb.SetGrayStroke(0.0f);
110:                   cb.Circle(150f400f150f);
111:                   cb.FillStroke();
112:               
113:                   // New Page to draw text in the pattern painter's canvas
114:                   document.NewPage();
115:               
116:                   document.Add(new Paragraph(textFontFactory.GetFont(FontFactory.HELVETICA60Font.BOLDnew GrayColor(0.3f))));
117:                   document.Add(new Paragraph(textFontFactory.GetFont(FontFactory.HELVETICA60Font.BOLDpat1)));
118:                   // draw a rectangle
119:                   cb.SetPatternFill(p1);
120:                   cb.SetGrayStroke(0.0f);
121:                   cb.Rectangle(00284120);
122:                   cb.FillStroke();
123:               
124:                   // draw some characters
125:                   cb.BeginText();
126:                   cb.SetFontAndSize(bf1);
127:                   cb.SetTextMatrix(270f0f0f270f20f100f);
128:                   cb.SetGrayFill(0.9f);
129:                   cb.ShowText("ABC");
130:                   cb.SetPatternFill(p1);
131:                   cb.MoveTextWithLeading(0.0f0.0f);
132:                   cb.ShowText("ABC");
133:                   cb.EndText();
134:               
135:                   // draw a circle
136:                   cb.SetPatternFill(p1);
137:                   cb.SetGrayStroke(0.0f);
138:                   cb.Circle(150f400f150f);
139:                   cb.FillStroke();
140:               
141:                   // New page to place image in the pattern painter's canvas
142:                   document.NewPage();
143:                   document.Add(new Paragraph(textFontFactory.GetFont(FontFactory.HELVETICA60Font.BOLDnew GrayColor(0.3f))));
144:                   document.Add(new Paragraph(textFontFactory.GetFont(FontFactory.HELVETICA60Font.BOLDpat2)));
145:                   // The original Image for comparison reason.
146:                   // Note: The width and height is the same as bbox in pattern
147:                   cb.AddImage(imgimg.ScaledWidth0f0fimg.ScaledHeight350f400f);
148:               
149:                   // draw a rectangle
150:                   cb.SetPatternFill(p2);
151:                   cb.SetGrayStroke(0.0f);
152:                   cb.Rectangle(6060300120);
153:                   cb.FillStroke();
154:               
155:                   // draw some characters.
156:                   // Note: if the image fills up the pattern, there's no need to draw text twice
157:                   // since colors in image will be clipped to character glyph path
158:                   cb.BeginText();
159:                   cb.SetFontAndSize(bf1);
160:                   cb.SetTextMatrix(270f0f0f270f60f120f);
161:                   cb.SetPatternFill(p2);
162:                   cb.ShowText("ABC");
163:                   cb.EndText();
164:               
165:                   // draw a circle
166:                   cb.SetPatternFill(p2);
167:                   cb.SetGrayStroke(0.0f);
168:                   cb.Circle(150f400f150f);
169:                   cb.FillStroke();
170:               }
171:               catch(Exception e
172:               {
173:                   Console.Error.WriteLine(e.Message);
174:                   Console.Error.WriteLine(e.StackTrace);
175:               }
176:           
177:               // finally, we close the document
178:               document.Close();
179:           }
180:       }
181:   }