| 1: | using System; | |
| 2: | using System.IO; | |
| 3: | ||
| 4: | using iTextSharp.text; | |
| 5: | using iTextSharp.text.pdf; | |
| 6: | ||
| 7: | namespace iTextSharp.tutorial.Chap10 | |
| 8: | { | |
| 9: | public class Chap1011 | |
| 10: | { | |
| 11: | ||
| 12: | public Chap1011() | |
| 13: | { | |
| 14: | ||
| 15: | Console.WriteLine("Chapter 10 example 11: a PdfPTable in a template"); | |
| 16: | ||
| 17: | // step 1: creation of a document-object | |
| 18: | Rectangle rect = new Rectangle(PageSize.A4); | |
| 19: | rect.BackgroundColor = new Color(238, 221, 88); | |
| 20: | Document document = new Document(rect, 50, 50, 50, 50); | |
| 21: | try | |
| 22: | { | |
| 23: | // step 2: we create a writer that listens to the document | |
| 24: | PdfWriter writer = PdfWriter.GetInstance(document, new FileStream("Chap1011.pdf", FileMode.Create)); | |
| 25: | // step 3: we open the document | |
| 26: | document.Open(); | |
| 27: | // step 4: | |
| 28: | PdfTemplate template = writer.DirectContent.CreateTemplate(20, 20); | |
| 29: | BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED); | |
| 30: | String text = "Vertical"; | |
| 31: | float size = 16; | |
| 32: | float width = bf.GetWidthPoint(text, size); | |
| 33: | template.BeginText(); | |
| 34: | template.SetRGBColorFillF(1, 1, 1); | |
| 35: | template.SetFontAndSize(bf, size); | |
| 36: | template.SetTextMatrix(0, 2); | |
| 37: | template.ShowText(text); | |
| 38: | template.EndText(); | |
| 39: | template.Width = width; | |
| 40: | template.Height = size + 2; | |
| 41: | Image img = Image.GetInstance(template); | |
| 42: | img.RotationDegrees=90; | |
| 43: | Chunk ck = new Chunk(img, 0, 0); | |
| 44: | PdfPTable table = new PdfPTable(3); | |
| 45: | table.WidthPercentage = 100; | |
| 46: | table.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER; | |
| 47: | table.DefaultCell.VerticalAlignment = Element.ALIGN_MIDDLE; | |
| 48: | PdfPCell cell = new PdfPCell(img); | |
| 49: | cell.Padding = 4; | |
| 50: | cell.BackgroundColor = new Color(0, 0, 255); | |
| 51: | cell.HorizontalAlignment = Element.ALIGN_CENTER; | |
| 52: | table.AddCell("I see a template on my right"); | |
| 53: | table.AddCell(cell); | |
| 54: | table.AddCell("I see a template on my left"); | |
| 55: | table.AddCell(cell); | |
| 56: | table.AddCell("I see a template everywhere"); | |
| 57: | table.AddCell(cell); | |
| 58: | table.AddCell("I see a template on my right"); | |
| 59: | table.AddCell(cell); | |
| 60: | table.AddCell("I see a template on my left"); | |
| 61: | ||
| 62: | Paragraph p1 = new Paragraph("This is a template "); | |
| 63: | p1.Add(ck); | |
| 64: | p1.Add(" just here."); | |
| 65: | p1.Leading = img.ScaledHeight * 1.1f; | |
| 66: | document.Add(p1); | |
| 67: | document.Add(table); | |
| 68: | Paragraph p2 = new Paragraph("More templates "); | |
| 69: | p2.Leading = img.ScaledHeight * 1.1f; | |
| 70: | p2.Alignment = Element.ALIGN_JUSTIFIED; | |
| 71: | img.ScalePercent(70); | |
| 72: | for (int k = 0; k < 20; ++k) | |
| 73: | p2.Add(ck); | |
| 74: | document.Add(p2); | |
| 75: | // step 5: we close the document | |
| 76: | document.Close(); | |
| 77: | } | |
| 78: | catch (Exception de) | |
| 79: | { | |
| 80: | Console.Error.WriteLine(de.Message); | |
| 81: | Console.Error.WriteLine(de.StackTrace); | |
| 82: | } | |
| 83: | } | |
| 84: | } | |
| 85: | } |