| 1: | using System; | |
| 2: | using System.IO; | |
| 3: | ||
| 4: | using iTextSharp.text; | |
| 5: | using iTextSharp.text.pdf; | |
| 6: | ||
| 7: | namespace iTextSharp.tutorial.Chap12 | |
| 8: | { | |
| 9: | class MyTableEventColspan: IPdfPTableEvent | |
| 10: | { | |
| 11: | public void TableLayout(PdfPTable table, float[][] width, float[] heights, int headerRows, int rowStart, PdfContentByte[] canvases) | |
| 12: | { | |
| 13: | float[] widths = width[0]; | |
| 14: | PdfContentByte cb = canvases[PdfPTable.TEXTCANVAS]; | |
| 15: | cb.SaveState(); | |
| 16: | cb.SetLineWidth(2); | |
| 17: | cb.SetRGBColorStroke(255, 0, 0); | |
| 18: | ||
| 19: | cb.Rectangle(widths[0], heights[heights.Length - 1], widths[widths.Length - 1] - widths[0], heights[0] - heights[heights.Length - 1]); | |
| 20: | cb.Stroke(); | |
| 21: | if (headerRows > 0) | |
| 22: | { | |
| 23: | float headerHeight = heights[0]; | |
| 24: | for (int k = 0; k < headerRows; ++k) | |
| 25: | headerHeight += heights[k]; | |
| 26: | cb.SetRGBColorStroke(0, 0, 255); | |
| 27: | cb.Rectangle(widths[0], heights[headerRows], widths[widths.Length - 1] - widths[0], heights[0] - heights[headerRows]); | |
| 28: | cb.Stroke(); | |
| 29: | } | |
| 30: | cb.RestoreState(); | |
| 31: | cb = canvases[PdfPTable.BASECANVAS]; | |
| 32: | cb.SaveState(); | |
| 33: | cb.SetLineWidth(.5f); | |
| 34: | Random r = new Random(); | |
| 35: | for (int line = 0; line < heights.Length - 1; ++line) | |
| 36: | { | |
| 37: | widths = width[line]; | |
| 38: | for (int col = 0; col < widths.Length - 1; ++col) | |
| 39: | { | |
| 40: | if (line == 0 && col == 0) | |
| 41: | cb.SetAction(new PdfAction("http://www.Geocities.Com/itextpdf"), | |
| 42: | widths[col], heights[line + 1], widths[col + 1], heights[line]); | |
| 43: | cb.SetRGBColorStrokeF((float)r.NextDouble(), (float)r.NextDouble(), (float)r.NextDouble()); | |
| 44: | cb.MoveTo(widths[col], heights[line]); | |
| 45: | cb.LineTo(widths[col + 1], heights[line]); | |
| 46: | cb.Stroke(); | |
| 47: | cb.SetRGBColorStrokeF((float)r.NextDouble(), (float)r.NextDouble(), (float)r.NextDouble()); | |
| 48: | cb.MoveTo(widths[col], heights[line]); | |
| 49: | cb.LineTo(widths[col], heights[line + 1]); | |
| 50: | cb.Stroke(); | |
| 51: | } | |
| 52: | } | |
| 53: | cb.RestoreState(); | |
| 54: | } | |
| 55: | } | |
| 56: | ||
| 57: | public class Chap1203 | |
| 58: | { | |
| 59: | public Chap1203() | |
| 60: | { | |
| 61: | Console.WriteLine("Chapter 12 example 3: Table events with rowspan"); | |
| 62: | ||
| 63: | // step 1: creation of a document-object | |
| 64: | Document document = new Document(PageSize.A4, 50, 50, 50, 50); | |
| 65: | try { | |
| 66: | // step 2: we create a writer that listens to the document | |
| 67: | PdfWriter writer = PdfWriter.GetInstance(document, new FileStream("Chap1203.pdf", FileMode.Create)); | |
| 68: | // step 3: we open the document | |
| 69: | document.Open(); | |
| 70: | // step 4: we add some content | |
| 71: | BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED); | |
| 72: | // table 1 | |
| 73: | PdfPTable table = new PdfPTable(4); | |
| 74: | ||
| 75: | table.DefaultCell.Border=(Rectangle.NO_BORDER); | |
| 76: | for (int k = 0; k < 24; ++k) { | |
| 77: | if (k != 0) | |
| 78: | table.AddCell("" + k); | |
| 79: | else { | |
| 80: | table.DefaultCell.Colspan=(3); | |
| 81: | table.DefaultCell.HorizontalAlignment=(Element.ALIGN_CENTER); | |
| 82: | table.AddCell("This is a very big URL"); | |
| 83: | table.DefaultCell.Colspan=(1); | |
| 84: | table.DefaultCell.HorizontalAlignment=(Element.ALIGN_LEFT); | |
| 85: | k += 2; | |
| 86: | } | |
| 87: | } | |
| 88: | MyTableEventColspan evnt = new MyTableEventColspan(); | |
| 89: | table.TableEvent=(evnt); | |
| 90: | table.TotalWidth=(300); | |
| 91: | // write table 1 at some position | |
| 92: | table.WriteSelectedRows(0, -1, 100, 600, writer.DirectContent); | |
| 93: | // add table 1 (default position) | |
| 94: | document.Add(table); | |
| 95: | document.NewPage(); | |
| 96: | // table 2 | |
| 97: | table = new PdfPTable(4); | |
| 98: | float fontSize = 12; | |
| 99: | table.DefaultCell.PaddingTop=(bf.GetFontDescriptor(BaseFont.ASCENT, fontSize) - fontSize + 2); | |
| 100: | table.DefaultCell.Border=(Rectangle.NO_BORDER); | |
| 101: | for (int k = 0; k < 500 * 4; ++k) { | |
| 102: | if (k == 0) { | |
| 103: | table.DefaultCell.Colspan=(4); | |
| 104: | table.DefaultCell.HorizontalAlignment=(Element.ALIGN_CENTER); | |
| 105: | table.AddCell(new Phrase("This is an URL", new Font(bf, fontSize * 2))); | |
| 106: | table.DefaultCell.Colspan=(1); | |
| 107: | table.DefaultCell.HorizontalAlignment=(Element.ALIGN_LEFT); | |
| 108: | k += 3; | |
| 109: | } | |
| 110: | else | |
| 111: | table.AddCell(new Phrase("" + k, new Font(bf, fontSize))); | |
| 112: | } | |
| 113: | table.TableEvent=(evnt); | |
| 114: | table.HeaderRows=(3); | |
| 115: | document.Add(table); | |
| 116: | } | |
| 117: | catch (Exception de) { | |
| 118: | Console.Error.WriteLine(de.Message); | |
| 119: | } | |
| 120: | // step 5: close the document | |
| 121: | document.Close(); | |
| 122: | } | |
| 123: | } | |
| 124: | } |