Source Viewer:  iTextSharp-NestedTables.cs

Font Size:
AssemblyVersion.cs  CollectionView.cs  CombineWordDocs.cs  ConfigSectionHandler.cs  DateTimeParse.cs  DaylightSavings.cs  GetIeProxySettings.cs  iTextSharp-NestedTables.cs  iTextSharp-Simple.cs  shellex.aspx  TypeView.cs  Util.ConsoleTools.cs 
.. 

// // Itextsharp-NestedTables.cs // // example of how to use itextsharp library (v3.0.10.0) to dynamically generate nested tables in PDF. // // compile with: // // csc /debug+ /target:exe /r:itextsharp.dll iTextSharp-NestedTables.cs // // Thu, 02 Feb 2006 09:15 // // ------------------------------------------------------- using System; using System.IO; using System.Reflection; using System.Diagnostics; using iTextSharp.text; [assembly: AssemblyVersion("1.0.1.0")] namespace iTextSharpExamples { public class NestedTables { public static void Main() { try { string assyName= Path.GetFileName(Assembly.GetExecutingAssembly().GetName().CodeBase.ToString()); string assyVersion = Assembly.GetExecutingAssembly().GetName().Version.ToString(); Random rnd= new Random(); // step 1: create a document Document document = new Document(); // step 2: we set the ContentType and create an instance of the Writer int pid= Process.GetCurrentProcess().Id; string Filename= String.Format("{0}-{1}-{2}.pdf", assyName, pid, rnd.Next(1000)); iTextSharp.text.pdf.PdfWriter.GetInstance(document, new FileStream(Filename, FileMode.Create)); // step 3: add metadata (before document.Open()) document.AddTitle("Sample Document"); document.AddSubject("Csharp PDF creation example"); document.AddKeywords("csharp dotnet examples"); document.AddCreator(".NET Assembly: " + assyName); document.AddAuthor("Dino Chiesa"); document.AddProducer(); // step 4: open the doc document.Open(); // step 5: Add content to the document Font font24= FontFactory.GetFont(FontFactory.HELVETICA, 24); Font font18= FontFactory.GetFont(FontFactory.HELVETICA, 18); Font fontAnchor= FontFactory.GetFont(FontFactory.HELVETICA, 10, Font.UNDERLINE, new Color(0, 0, 255)); Chunk bullet= new Chunk("\u2022", font18); // int i,j,n,m; // Table bigtable = new Table(3,3); // Table[][] t= new Table[3][]; // for (i=0; i< t.Length; i++) { // t[i]= new Table[3]; // for (j=0; j< t[i].Length; j++) { // t[i][j]= new Table(3,3); // for (n=0; n< t[i][j].Columns; n++) { // for (m=0; m< t[i][j].Size; m++) { // //t[i][j].AddCell(new Cell(), n, m); // t[i][j].AddCell((rnd.Next(9)+1).ToString()); // } // } // bigtable.AddCell(new Cell(t[i][j]), i,j); // } // } #if NOWORKY Table bigtable = new Table(3); bigtable.DefaultCellBorderWidth=5; bigtable.DefaultCellBorder=1; // bigtable.Debug= true; for (int i=0; i < 3; i++) { for (int j=0; j < 3; j++) { Table t = new Table(3); for (int k=0; k < 9; k++) { //bigtable.AddCell(new Cell(t[i][j]), i,j); t.AddCell((k+1).ToString()); } Cell c= new Cell(t); System.Console.WriteLine("little table:"); System.Console.WriteLine(" Columns: {0}", t.Columns); System.Console.WriteLine(" rows.Count: {0}", t.Size); System.Console.WriteLine("big table:"); System.Console.WriteLine(" Columns: {0}", bigtable.Columns); System.Console.WriteLine(" rows.Count: {0}", bigtable.Size); System.Console.WriteLine("Cell:"); System.Console.WriteLine(" Colspan: {0}", c.Colspan); System.Console.WriteLine(" Rowspan: {0}", c.Rowspan); System.Console.WriteLine(); //bigtable.AddCell("Hello"); bigtable.AddCell(c,i,j); bigtable.AddCell(c,i,j); //bigtable.InsertTable(t,i,j); } } bigtable.Complete(); #endif #if THIS_WORKS iTextSharp.text.pdf.PdfPTable bigtable= new iTextSharp.text.pdf.PdfPTable(3); Paragraph pp= new Paragraph("header with colspan 3, borderwidth=2, padding=3"); iTextSharp.text.pdf.PdfPCell c11 = new iTextSharp.text.pdf.PdfPCell(new Paragraph("1.1")); iTextSharp.text.pdf.PdfPCell c12 = new iTextSharp.text.pdf.PdfPCell(new Paragraph("1.2")); iTextSharp.text.pdf.PdfPCell c13 = new iTextSharp.text.pdf.PdfPCell(new Paragraph("1.3")); c11.HorizontalAlignment= Element.ALIGN_CENTER; c12.HorizontalAlignment= Element.ALIGN_RIGHT; iTextSharp.text.pdf.PdfPCell cell = new iTextSharp.text.pdf.PdfPCell(pp); cell.Colspan= 3; cell.BorderWidth=2; cell.Padding=3; bigtable.AddCell(cell); bigtable.AddCell(c11); bigtable.AddCell(c12); bigtable.AddCell(c13); bigtable.AddCell("1.4"); iTextSharp.text.pdf.PdfPTable nested= new iTextSharp.text.pdf.PdfPTable(3); nested.DefaultCell.HorizontalAlignment= Element.ALIGN_CENTER; nested.AddCell(c11); nested.AddCell(c12); nested.AddCell(c13); nested.AddCell("2.1"); nested.AddCell("2.2"); nested.AddCell("2.3"); bigtable.AddCell(nested); bigtable.AddCell("1.6"); #endif iTextSharp.text.pdf.PdfPTable bigtable= new iTextSharp.text.pdf.PdfPTable(3); bigtable.WidthPercentage= 60; int i,j,n,m, x=0; for (i=0; i<3; i++) { for (j=0; j<3; j++) { iTextSharp.text.pdf.PdfPTable nested= new iTextSharp.text.pdf.PdfPTable(3); nested.DefaultCell.HorizontalAlignment= Element.ALIGN_CENTER; nested.DefaultCell.VerticalAlignment= Element.ALIGN_MIDDLE; nested.DefaultCell.MinimumHeight= 24; for (n=0; n<3; n++) { for (m=0; m<3; m++) { nested.AddCell(new String((char)(65+x),1)+(n+1)+","+(m+1)); } } bigtable.AddCell(nested); x++; } } document.Add(bigtable); document.Add(new Paragraph("\n")); document.Add(new Paragraph("This PDF document was generated dynamically: ")); List list= new List(false, 20); // true= ordered, false= unordered list.ListSymbol = bullet; // use "bullet" as list symbol list.Add(new ListItem("on " + DateTime.Now.ToString("dddd, MMM d, yyyy"))); list.Add(new ListItem("at " + DateTime.Now.ToString("hh:mm:ss tt zzzz"))); list.Add(new ListItem("on machine " + Environment.MachineName)); list.Add(new ListItem("by .NET assembly: " + assyName + " " + assyVersion)); list.Add(new ListItem("on a machine running " + Environment.OSVersion.ToString())); list.Add(new ListItem("and .NET CLR " + Environment.Version)); string v1= "(none)"; string v2= "(none)"; try { v1= list.GetType().Assembly.GetName().Version.ToString(); v2= list.GetType().Assembly.ImageRuntimeVersion; } catch (Exception e1) {v1 = e1.ToString();} ListItem li= new ListItem(String.Format("iTextSharp v{0} (compiled with .NET {1}) see ", v1,v2)); Anchor anchor = new Anchor("http://itextsharp.sourceforge.net/", fontAnchor); anchor.Reference = "http://itextsharp.sourceforge.net"; //anchor.Name = "website"; li.Add(anchor); list.Add(li); document.Add(list); // step 6: Close document document.Close(); Process p= new Process(); p.StartInfo.FileName= Filename; //p.StartInfo.WindowStyle = Diagnostics.ProcessWindowStyle.Hidden; p.StartInfo.RedirectStandardOutput = false; p.StartInfo.UseShellExecute = true; p.Start(); } catch (DocumentException ex) { Console.Error.WriteLine(ex.StackTrace); Console.Error.WriteLine(ex.Message); } } } }

The srcview page has been enjoyed 291584 times since 18 September 2003