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: | /// Raw Image data: | |
11: | /// Uptil now all examples used images that were stored on your harddisk or that were retrieved from some webserver, | |
12: | /// but it is also possible to get an instance of an image using an array of bytes containing the image data | |
13: | /// </summary> | |
14: | public class Chap0609 | |
15: | { | |
16: | ||
17: | public Chap0609() | |
18: | { | |
19: | ||
20: | Console.WriteLine("Chapter 6 example 9: bytes() / Raw Image"); | |
21: | ||
22: | // step 1: creation of a document-object | |
23: | Document document = new Document(); | |
24: | ||
25: | try | |
26: | { | |
27: | ||
28: | // step 2: | |
29: | // we create a writer that listens to the document | |
30: | // and directs a PDF-stream to a file | |
31: | ||
32: | PdfWriter.GetInstance(document, new FileStream("Chap0609.pdf", FileMode.Create)); | |
33: | ||
34: | // step 3: we open the document | |
35: | document.Open(); | |
36: | ||
37: | // step 4: we add content (example by Paulo Soares) | |
38: | ||
39: | // creation a jpeg passed as an array of bytes to the Image | |
40: | FileStream rf = new FileStream("mykids.jpg", FileMode.Open, FileAccess.Read); | |
41: | int size = (int)rf.Length; | |
42: | byte[] imext = new byte[size]; | |
43: | rf.Read(imext, 0, size); | |
44: | rf.Close(); | |
45: | Image img1 = Image.GetInstance(imext); | |
46: | img1.SetAbsolutePosition(50, 500); | |
47: | document.Add(img1); | |
48: | ||
49: | // creation of an image of 100 x 100 pixels (x 3 bytes for the Red, Green and Blue value) | |
50: | byte[] data = new byte[100*100*3]; | |
51: | for (int k = 0; k < 100; ++k) | |
52: | { | |
53: | for (int j = 0; j < 300; j += 3) | |
54: | { | |
55: | data[k * 300 + j] = (byte)(255 * Math.Sin(j * .5 * Math.PI / 300)); | |
56: | data[k * 300 + j + 1] = (byte)(256 - j * 256 / 300); | |
57: | data[k * 300 + j + 2] = (byte)(255 * Math.Cos(k * .5 * Math.PI / 100)); | |
58: | } | |
59: | } | |
60: | Image img2 = Image.GetInstance(100, 100, 3, 8, data); | |
61: | img2.SetAbsolutePosition(200, 200); | |
62: | document.Add(img2); | |
63: | } | |
64: | catch(DocumentException de) | |
65: | { | |
66: | Console.Error.WriteLine(de.Message); | |
67: | } | |
68: | catch(IOException ioe) | |
69: | { | |
70: | Console.Error.WriteLine(ioe.Message); | |
71: | } | |
72: | ||
73: | // step 5: we close the document | |
74: | document.Close(); | |
75: | } | |
76: | } | |
77: | } |