Blog

 
1:   using System;
2:   using System.IO;
3:  
4:   using iTextSharp.text;
5:   using iTextSharp.text.pdf;
6:  
7:  
8:   namespace iTextSharp.tutorial.Chap10
9:   {
10:       public class Chap1013 
11:       {
12:       
13:           public Chap1013() 
14:           {
15:           
16:               Console.WriteLine("Chapter 10 Example 13: Spot Color");
17:           
18:               // step 1: creation of a document-object
19:               Document document = new Document();
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("Chap1013.pdf", FileMode.Create));
27:                   BaseFont bf = BaseFont.CreateFont("Helvetica", "winansi", BaseFont.NOT_EMBEDDED);
28:               
29:                   // step 3: we open the document
30:                   document.Open();
31:               
32:                   // step 4: we add some content
33:                   PdfContentByte cb = writer.DirectContent;
34:               
35:                   // Note: I made up these names unless someone give me a PANTONE swatch as gift (phillip@formstar.Com)
36:                   PdfSpotColor spc_cmyk = new PdfSpotColor("PANTONE 280 CV", 0.25f, new CMYKColor(0.9f, .2f, .3f, .1f));
37:                   PdfSpotColor spc_rgb = new PdfSpotColor("PANTONE 147", 0.9f, new Color(114, 94, 38));
38:                   PdfSpotColor spc_g = new PdfSpotColor("PANTONE 100 CV", 0.5f, new GrayColor(0.9f));
39:               
40:                   // Stroke a rectangle with CMYK alternate
41:                   cb.SetColorStroke(spc_cmyk, .5f);
42:                   cb.SetLineWidth(10f);
43:                   // draw a rectangle
44:                   cb.Rectangle(100, 700, 100, 100);
45:                   // add the diagonal
46:                   cb.MoveTo(100, 700);
47:                   cb.LineTo(200, 800);
48:                   // stroke the lines
49:                   cb.Stroke();
50:               
51:                   // Fill a rectangle with CMYK alternate
52:                   cb.SetColorFill(spc_cmyk, spc_cmyk.Tint);
53:                   cb.Rectangle(250, 700, 100, 100);
54:                   cb.Fill();
55:               
56:                   // Stroke a circle with RGB alternate
57:                   cb.SetColorStroke(spc_rgb, spc_rgb.Tint);
58:                   cb.SetLineWidth(5f);
59:                   cb.Circle(150f, 500f, 100f);
60:                   cb.Stroke();
61:               
62:                   // Fill the circle with RGB alternate
63:                   cb.SetColorFill(spc_rgb, spc_rgb.Tint);
64:                   cb.Circle(150f, 500f, 50f);
65:                   cb.Fill();
66:               
67:                   // example with colorfill
68:                   cb.SetColorFill(spc_g, spc_g.Tint);
69:                   cb.MoveTo(100f, 200f);
70:                   cb.LineTo(200f, 250f);
71:                   cb.LineTo(400f, 150f);
72:                   cb.Fill();
73:                   document.NewPage();
74:                   String text = "Some text to show";
75:                   document.Add(new Paragraph(text, FontFactory.GetFont(FontFactory.HELVETICA, 24, Font.NORMAL, new SpotColor(spc_cmyk))));
76:                   document.Add(new Paragraph(text, FontFactory.GetFont(FontFactory.HELVETICA, 24, Font.NORMAL, new SpotColor(spc_cmyk, 0.5f))));
77:               
78:                   // example with template
79:                   PdfTemplate t = cb.CreateTemplate(500f, 500f);
80:                   // Stroke a rectangle with CMYK alternate
81:                   t.SetColorStroke(new SpotColor(spc_cmyk, .5f));
82:                   t.SetLineWidth(10f);
83:                   // draw a rectangle
84:                   t.Rectangle(100, 10, 100, 100);
85:                   // add the diagonal
86:                   t.MoveTo(100, 10);
87:                   t.LineTo(200, 100);
88:                   // stroke the lines
89:                   t.Stroke();
90:               
91:                   // Fill a rectangle with CMYK alternate
92:                   t.SetColorFill(spc_g, spc_g.Tint);
93:                   t.Rectangle(100, 125, 100, 100);
94:                   t.Fill();
95:                   t.BeginText();
96:                   t.SetFontAndSize(bf, 20f);
97:                   t.SetTextMatrix(1f, 0f, 0f, 1f, 10f, 10f);
98:                   t.ShowText("Template text upside down");
99:                   t.EndText();
100:                   t.Rectangle(0, 0, 499, 499);
101:                   t.Stroke();
102:                   cb.AddTemplate(t, -1.0f, 0.00f, 0.00f, -1.0f, 550f, 550f);
103:               }
104:               catch(Exception de) 
105:               {
106:                   Console.Error.WriteLine(de.Message);
107:                   Console.Error.WriteLine(de.StackTrace);
108:               }
109:           
110:               // step 5: we close the document
111:               document.Close();
112:           }
113:       }
114:   }