Blog

 
1:   using System;
2:   using System.IO;
3:  
4:   using iTextSharp.text;
5:   using iTextSharp.text.pdf;
6:  
7:   namespace iTextSharp.tutorial.Chap06
8:   {
9:       /// <summary>
10:       /// Image masks
11:       /// </summary>
12:       public class Chap0613 
13:       {
14:           public Chap0613() 
15:           {
16:               Console.WriteLine("Chapter 6 example 13: masked images");
17:           
18:               Document document = new Document(PageSize.A4, 50, 50, 50, 50);
19:               try 
20:               {
21:                   PdfWriter writer = PdfWriter.GetInstance(document, new FileStream("Chap0613.pdf", FileMode.Create));
22:               
23:                   document.Open();
24:                   Paragraph p = new Paragraph("Some text behind a masked image.");
25:                   document.Add(p);
26:                   document.Add(p);
27:                   document.Add(p);
28:                   document.Add(p);
29:                   document.Add(p);
30:                   document.Add(p);
31:                   document.Add(p);
32:                   document.Add(p);
33:                   document.Add(p);
34:                   document.Add(p);
35:               
36:                   document.Add(p);
37:                   document.Add(p);
38:                   document.Add(p);
39:                   document.Add(p);
40:                   document.Add(p);
41:                   document.Add(p);
42:                   document.Add(p);
43:                   document.Add(p);
44:                   document.Add(p);
45:                   document.Add(p);
46:                   document.Add(p);
47:                   document.Add(p);
48:                   document.Add(p);
49:                   document.Add(p);
50:                   document.Add(p);
51:                   PdfContentByte cb = writer.DirectContent;
52:                   byte[] maskr = {(byte)0x3c, (byte)0x7e, (byte)0xe7, (byte)0xc3, (byte)0xc3, (byte)0xe7, (byte)0x7e, (byte)0x3c};
53:                   Image mask = Image.GetInstance(8, 8, 1, 1, maskr);
54:                   mask.MakeMask();
55:                   mask.Inverted=true;
56:   //                mask.InvertMask = true;
57:                   Image image = Image.GetInstance("vonnegut.gif");
58:                   image.ImageMask = mask;
59:                   image.SetAbsolutePosition(60, 620);
60:                   // explicit masking
61:                   cb.AddImage(image);
62:                   // stencil masking
63:                   cb.SetRGBColorFill(255, 0, 0);
64:                   cb.AddImage(mask, mask.ScaledWidth * 8, 0, 0, mask.ScaledHeight * 8, 100, 400);
65:                   cb.SetRGBColorFill(0, 255, 0);
66:                   cb.AddImage(mask, mask.ScaledWidth * 8, 0, 0, mask.ScaledHeight * 8, 200, 400);
67:                   cb.SetRGBColorFill(0, 0, 255);
68:                   cb.AddImage(mask, mask.ScaledWidth * 8, 0, 0, mask.ScaledHeight * 8, 300, 400);
69:                   document.Close();
70:               }
71:               catch (Exception de) 
72:               {
73:                   Console.Error.WriteLine(de.StackTrace);
74:               }
75:           }
76:       }
77:   }