| 1: | using System; | |
| 2: | using System.IO; | |
| 3: | ||
| 4: | using iTextSharp.text; | |
| 5: | using iTextSharp.text.pdf; | |
| 6: | ||
| 7: | ||
| 8: | namespace iTextSharp.tutorial.Chap05 | |
| 9: | { | |
| 10: | /// <summary> | |
| 11: | /// Force a table or a cell to fit a page | |
| 12: | /// </summary> | |
| 13: | public class Chap0512 | |
| 14: | { | |
| 15: | public Chap0512() | |
| 16: | { | |
| 17: | Console.WriteLine("Chapter 5 example 12: avoid cell splitting"); | |
| 18: | // creation of the document with a certain size and certain margins | |
| 19: | Document document = new Document(PageSize.A4.Rotate(), 50, 50, 50, 50); | |
| 20: | ||
| 21: | try | |
| 22: | { | |
| 23: | // creation of the different writers | |
| 24: | PdfWriter.GetInstance(document, new FileStream("Chap0512.pdf", FileMode.Create)); | |
| 25: | ||
| 26: | // we add some meta information to the document | |
| 27: | document.AddAuthor("Gerald Henson"); | |
| 28: | document.AddSubject("This is the result of a Test."); | |
| 29: | ||
| 30: | document.Open(); | |
| 31: | ||
| 32: | Table datatable = new Table(10); | |
| 33: | datatable.CellsFitPage = true; | |
| 34: | ||
| 35: | datatable.Padding = 4; | |
| 36: | datatable.Spacing = 0; | |
| 37: | //datatable.setBorder(Rectangle.NO_BORDER); | |
| 38: | float[] headerwidths = {10, 24, 12, 12, 7, 7, 7, 7, 7, 7}; | |
| 39: | datatable.Widths = headerwidths; | |
| 40: | datatable.WidthPercentage = 100; | |
| 41: | ||
| 42: | // the first cell spans 10 columns | |
| 43: | Cell cell = new Cell(new Phrase("Administration -System Users Report", FontFactory.GetFont(FontFactory.HELVETICA, 24, Font.BOLD))); | |
| 44: | cell.HorizontalAlignment = Element.ALIGN_CENTER; | |
| 45: | cell.Leading = 30; | |
| 46: | cell.Colspan = 10; | |
| 47: | cell.Border = Rectangle.NO_BORDER; | |
| 48: | cell.BackgroundColor = new Color(0xC0, 0xC0, 0xC0); | |
| 49: | datatable.AddCell(cell); | |
| 50: | ||
| 51: | // These cells span 2 rows | |
| 52: | datatable.DefaultCellBorderWidth = 2; | |
| 53: | datatable.DefaultHorizontalAlignment = 1; | |
| 54: | datatable.DefaultRowspan = 2; | |
| 55: | datatable.AddCell("User Id"); | |
| 56: | datatable.AddCell(new Phrase("Name", FontFactory.GetFont(FontFactory.HELVETICA, 14, Font.BOLD))); | |
| 57: | datatable.AddCell("Company"); | |
| 58: | datatable.AddCell("Department"); | |
| 59: | ||
| 60: | // This cell spans the remaining 6 columns in 1 row | |
| 61: | datatable.DefaultRowspan = 1; | |
| 62: | datatable.DefaultColspan = 6; | |
| 63: | datatable.AddCell("Permissions"); | |
| 64: | ||
| 65: | // These cells span 1 row and 1 column | |
| 66: | datatable.DefaultColspan = 1; | |
| 67: | datatable.AddCell("Admin"); | |
| 68: | datatable.AddCell("Data"); | |
| 69: | datatable.AddCell("Expl"); | |
| 70: | datatable.AddCell("Prod"); | |
| 71: | datatable.AddCell("Proj"); | |
| 72: | datatable.AddCell("Online"); | |
| 73: | ||
| 74: | // this is the end of the table header | |
| 75: | datatable.EndHeaders(); | |
| 76: | ||
| 77: | datatable.DefaultCellBorderWidth = 1; | |
| 78: | datatable.DefaultRowspan = 1; | |
| 79: | ||
| 80: | for (int i = 1; i < 30; i++) | |
| 81: | { | |
| 82: | ||
| 83: | datatable.DefaultHorizontalAlignment = Element.ALIGN_LEFT; | |
| 84: | ||
| 85: | datatable.AddCell("myUserId"); | |
| 86: | datatable.AddCell("Somebody with a very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very, very long long name"); | |
| 87: | datatable.AddCell("No Name Company"); | |
| 88: | datatable.AddCell("D" + i); | |
| 89: | ||
| 90: | datatable.DefaultHorizontalAlignment = Element.ALIGN_CENTER; | |
| 91: | datatable.AddCell("No"); | |
| 92: | datatable.AddCell("Yes"); | |
| 93: | datatable.AddCell("No"); | |
| 94: | datatable.AddCell("Yes"); | |
| 95: | datatable.AddCell("No"); | |
| 96: | datatable.AddCell("Yes"); | |
| 97: | ||
| 98: | } | |
| 99: | ||
| 100: | ||
| 101: | document.Add(datatable); | |
| 102: | } | |
| 103: | catch(Exception e) | |
| 104: | { | |
| 105: | Console.Error.WriteLine(e.StackTrace); | |
| 106: | } | |
| 107: | ||
| 108: | // we close the document | |
| 109: | document.Close(); | |
| 110: | ||
| 111: | } | |
| 112: | } | |
| 113: | } |