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(documentnew 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.25fnew CMYKColor(0.9f.2f.3f.1f));
37:                   PdfSpotColor spc_rgb new PdfSpotColor("PANTONE 147"0.9fnew Color(1149438));
38:                   PdfSpotColor spc_g new PdfSpotColor("PANTONE 100 CV"0.5fnew 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(100700100100);
45:                   // add the diagonal
46:                   cb.MoveTo(100700);
47:                   cb.LineTo(200800);
48:                   // stroke the lines
49:                   cb.Stroke();
50:               
51:                   // Fill a rectangle with CMYK alternate
52:                   cb.SetColorFill(spc_cmykspc_cmyk.Tint);
53:                   cb.Rectangle(250700100100);
54:                   cb.Fill();
55:               
56:                   // Stroke a circle with RGB alternate
57:                   cb.SetColorStroke(spc_rgbspc_rgb.Tint);
58:                   cb.SetLineWidth(5f);
59:                   cb.Circle(150f500f100f);
60:                   cb.Stroke();
61:               
62:                   // Fill the circle with RGB alternate
63:                   cb.SetColorFill(spc_rgbspc_rgb.Tint);
64:                   cb.Circle(150f500f50f);
65:                   cb.Fill();
66:               
67:                   // example with colorfill
68:                   cb.SetColorFill(spc_gspc_g.Tint);
69:                   cb.MoveTo(100f200f);
70:                   cb.LineTo(200f250f);
71:                   cb.LineTo(400f150f);
72:                   cb.Fill();
73:                   document.NewPage();
74:                   String text "Some text to show";
75:                   document.Add(new Paragraph(textFontFactory.GetFont(FontFactory.HELVETICA24Font.NORMALnew SpotColor(spc_cmyk))));
76:                   document.Add(new Paragraph(textFontFactory.GetFont(FontFactory.HELVETICA24Font.NORMALnew SpotColor(spc_cmyk0.5f))));
77:               
78:                   // example with template
79:                   PdfTemplate t cb.CreateTemplate(500f500f);
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(10010100100);
85:                   // add the diagonal
86:                   t.MoveTo(10010);
87:                   t.LineTo(200100);
88:                   // stroke the lines
89:                   t.Stroke();
90:               
91:                   // Fill a rectangle with CMYK alternate
92:                   t.SetColorFill(spc_gspc_g.Tint);
93:                   t.Rectangle(100125100100);
94:                   t.Fill();
95:                   t.BeginText();
96:                   t.SetFontAndSize(bf20f);
97:                   t.SetTextMatrix(1f0f0f1f10f10f);
98:                   t.ShowText("Template text upside down");
99:                   t.EndText();
100:                   t.Rectangle(00499499);
101:                   t.Stroke();
102:                   cb.AddTemplate(t, -1.0f0.00f0.00f, -1.0f550f550f);
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:   }