| 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: | /// Images and other objects: | |
| 11: | /// Images inside a Chunk | |
| 12: | /// In some cases it can be handy to wrap an image inside a Chunk. | |
| 13: | /// Just create a Chunk with an image and an offset: | |
| 14: | /// </summary> | |
| 15: | public class Chap0614 | |
| 16: | { | |
| 17: | public Chap0614() | |
| 18: | { | |
| 19: | Document.Compress = false; | |
| 20: | Console.WriteLine("Chapter 6 example 14: images wrapped in a Chunk"); | |
| 21: | // step 1: creation of a document-object | |
| 22: | Document document = new Document(); | |
| 23: | try | |
| 24: | { | |
| 25: | // step 2: | |
| 26: | // we create a writer that listens to the document | |
| 27: | // and directs a PDF-stream to a file | |
| 28: | PdfWriter.GetInstance(document, new FileStream("Chap0614.pdf", FileMode.Create)); | |
| 29: | // step 3: we open the document | |
| 30: | document.Open(); | |
| 31: | // step 4: we create a table and add it to the document | |
| 32: | Image img = Image.GetInstance("pngnow.png"); | |
| 33: | img.ScalePercent(70); | |
| 34: | Chunk ck = new Chunk(img, 0, -5); | |
| 35: | Table table = new Table(3); | |
| 36: | table.WidthPercentage = 100; | |
| 37: | table.Padding = 2; | |
| 38: | table.DefaultHorizontalAlignment = Element.ALIGN_CENTER; | |
| 39: | Cell cell = new Cell(new Chunk(img, 0, -13)); | |
| 40: | cell.BackgroundColor = new Color(0xC0, 0xC0, 0xC0); | |
| 41: | cell.HorizontalAlignment = Element.ALIGN_CENTER; | |
| 42: | table.AddCell("I see an image\non my right"); | |
| 43: | table.AddCell(cell); | |
| 44: | table.AddCell("I see an image\non my left"); | |
| 45: | table.AddCell(cell); | |
| 46: | table.AddCell("I see images\neverywhere"); | |
| 47: | table.AddCell(cell); | |
| 48: | table.AddCell("I see an image\non my right"); | |
| 49: | table.AddCell(cell); | |
| 50: | table.AddCell("I see an image\non my left"); | |
| 51: | ||
| 52: | Phrase p1 = new Phrase("This is an image "); | |
| 53: | p1.Add(ck); | |
| 54: | p1.Add(" just here."); | |
| 55: | document.Add(p1); | |
| 56: | document.Add(p1); | |
| 57: | document.Add(p1); | |
| 58: | document.Add(p1); | |
| 59: | document.Add(p1); | |
| 60: | document.Add(p1); | |
| 61: | document.Add(p1); | |
| 62: | document.Add(table); | |
| 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: | // step 5: we close the document | |
| 73: | document.Close(); | |
| 74: | } | |
| 75: | } | |
| 76: | } |