Blog

 
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 Chap1012 
10:       {
11:           static public String[] headings = {
12:                                                 "Book/Product Model:",
13:                                                 "Sales Handle:",
14:                                                 "Why We Published this Book/Product Model:",
15:                                                 "Key benefits:",
16:                                                 "About the Author(s):",
17:                                                 "Technology/Topic Overview: ",
18:                                                 "Book/Product Content Summary:"
19:                                                 "Audience:",
20:                                                 "What's on the CD/DVD/Web:"
21:                                             };
22:  
23:           static public String[] texts = {
24:                                              "Ideally, choose one title (2-3 if absolutely necessary) that this book should perform like. Include full title, ISBN, author, and any sell through numbers if possible.",
25:                                              "One line description about the sales.",
26:                                              "Brief description (one-two lines) on the importance of this book to the audience.",
27:                                              "What benefit does this book provide to the consumer? (expert advice, speed, fun, productivity). Why should the Retailer/Wholesaler select this book over its competition? What are the unique features about this book should be highlighted? What makes this book different, better? From other books and the previous edition?",
28:                                              "What makes this person so special?  Is she/he an expert, creator of the technology, educational leader, etc.? What is their background, and what relevant experiences do they have to make them the BEST choice? Have he/she/they won awards or been recognized in any way. Other books poublished by the author.\n1. Book one.\n2. Book two.",
29:                                              "In brief two to five line description of the technology, topic or relevant information. Please keep descriptions succinct.",
30:                                              "Ideal describe the contents of this book. What will this book do for the reader? Will this book help them optimize their system? Increase productivity? offer tips and stragegies?",
31:                                              "Who is your intended customer? Experts? Power users? Business professionals? Programmers? What are the demographics?",
32:                                              "What is included on the Cd or Web site? Why is it necessary and what will it do for the purchaser (source code, examples, case studies)?\nIs there a value that can be associated with what is on the CD/DVD or Web?"
33:                                          };
34:  
35:           public Chap1012() 
36:           {
37:           
38:               Console.WriteLine("Chapter 10 example 12: PdfPTables and columns");
39:               
40:               // step 1: creation of a document-object
41:               Document document new Document(PageSize.LETTER90547272);
42:               try 
43:               {
44:                   // step 2: we create a writer that listens to the document
45:                   PdfWriter writer PdfWriter.GetInstance(documentnew FileStream("Chap1012.pdf"FileMode.Create));
46:               
47:                   float gutter 20;
48:                   int numColumns 3;
49:                   float fullWidth document.Right document.Left;
50:                   float columnWidth = (fullWidth - (numColumns 1) * gutter) / numColumns;
51:                   float[] allColumns new float[numColumns]; // left
52:                   for (int 0numColumns; ++k
53:                   {
54:                       allColumns[k] = document.Left + (columnWidth gutter) * k;
55:                   }
56:                   // set the fonts
57:                   Font font24B FontFactory.GetFont(FontFactory.TIMES_ROMAN24Font.BOLD);
58:                   Font font10B FontFactory.GetFont(FontFactory.TIMES_ROMAN10Font.BOLD);
59:                   Font font14B FontFactory.GetFont(FontFactory.TIMES_ROMAN14Font.BOLDnew Color(25500));
60:                   Font font9 FontFactory.GetFont(FontFactory.TIMES_ROMAN9);
61:                   Font font11 FontFactory.GetFont(FontFactory.TIMES_ROMAN11);
62:               
63:                   // step 3: we open the document
64:                   document.Open();            
65:                   // step 4:
66:                   // get the stream content
67:                   PdfContentByte cb writer.DirectContent;
68:                   // headers
69:                   Phrase fullTitle new Phrase("Full Title"font24B);
70:                   float currentY document.Top;
71:                   ColumnText ct new ColumnText(cb);
72:                   ct.SetSimpleColumn(fullTitledocument.Left0document.Rightdocument.Top24Element.ALIGN_JUSTIFIED);
73:                   ct.Go();
74:                   currentY ct.YLine;
75:                   currentY -= 4;
76:                   cb.SetLineWidth(1);
77:                   cb.MoveTo(document.LeftcurrentY);
78:                   cb.LineTo(document.RightcurrentY);
79:                   
80:                   cb.Stroke();
81:                   currentY -= 4;
82:                   ct.YLine currentY;
83:                   ct.AddText(new Chunk("Author: Name of the author comes here"font10B));
84:                   ct.Leading 10;
85:                   ct.Go();
86:                   currentY ct.YLine;
87:                   currentY -= 15;
88:                   float topColumn currentY;
89:                   for (int 1numColumns; ++k
90:                   {
91:                       float allColumns[k] - gutter 2;
92:                       cb.MoveTo(xtopColumn);
93:                       cb.LineTo(xdocument.Bottom);
94:                   }
95:                   cb.Stroke();
96:                   Image img Image.GetInstance("cover.png");
97:                   cb.AddImage(imgimg.ScaledWidth00img.ScaledHeightdocument.LeftcurrentY img.ScaledHeight);
98:                   currentY -= img.ScaledHeight 10;
99:                   ct.YLine currentY;
100:                   ct.AddText(new Chunk("Key Data:"font14B));
101:                   ct.Go();
102:                   currentY ct.YLine;
103:                   currentY -= 4;
104:                   PdfPTable ptable new PdfPTable(2);
105:                   ptable.DefaultCell.PaddingLeft 4;
106:                   ptable.DefaultCell.PaddingTop 0;
107:                   ptable.DefaultCell.PaddingBottom 4;
108:                   ptable.AddCell(new Phrase("Imprint Name:"font9));
109:                   ptable.AddCell(new Phrase("Prentice Hall"font9));
110:                   ptable.AddCell(new Phrase("Series Name:"font9));
111:                   ptable.AddCell(new Phrase(""font9));
112:                   ptable.AddCell(new Phrase("ISBN:"font9));
113:                   ptable.AddCell(new Phrase("Hall"font9));
114:                   ptable.AddCell(new Phrase("UPC Code:"font9));
115:                   ptable.AddCell(new Phrase("0789718103"font9));
116:                   ptable.AddCell(new Phrase("EAN #"font9));
117:                   ptable.AddCell(new Phrase("0786718103"font9));
118:                   ptable.AddCell(new Phrase("Price:"font9));
119:                   ptable.AddCell(new Phrase("49.99"font9));
120:                   ptable.AddCell(new Phrase("Page Count:"font9));
121:                   ptable.AddCell(new Phrase("500"font9));
122:                   ptable.AddCell(new Phrase("Discount:"font9));
123:                   ptable.AddCell(new Phrase("10%"font9));
124:                   ptable.AddCell(new Phrase("Trim Size:"font9));
125:                   ptable.AddCell(new Phrase("420x340"font9));
126:                   ptable.AddCell(new Phrase("Cover:"font9));
127:                   ptable.AddCell(new Phrase("Hard"font9));
128:                   ptable.AddCell(new Phrase("Interior Color:"font9));
129:                   ptable.AddCell(new Phrase("none"font9));
130:                   ptable.AddCell(new Phrase("Media with book:"font9));
131:                   ptable.AddCell(new Phrase("CD"font9));
132:                   ptable.AddCell(new Phrase("Author(s):"font9));
133:                   ptable.AddCell(new Phrase("Ben Forta"font9));
134:                   ptable.AddCell(new Phrase("Editor:"font9));
135:                   ptable.AddCell(new Phrase("Ben Forta"font9));
136:                   ptable.AddCell(new Phrase("Pub Date:"font9));
137:                   ptable.AddCell(new Phrase("06/05/1998"font9));
138:                   ptable.TotalWidth columnWidth;
139:  
140:                   currentY ptable.WriteSelectedRows(0, -1document.LeftcurrentYcb) - 20;
141:                   for (int 0headings.Length; ++k
142:                   {
143:                       ct.AddText(new Chunk(headings[k] + "\n"font14B));
144:                       ct.AddText(new Chunk(texts[k] + "\n\n"font11));
145:                   }
146:  
147:                   int currentColumn 0;
148:                   ct.SetSimpleColumn(allColumns[currentColumn], document.Bottom,
149:                       allColumns[currentColumn] + columnWidthcurrentY15Element.ALIGN_JUSTIFIED);
150:                   ct.SetLeading(21);
151:                   for (;;) 
152:                   {
153:                       int rc ct.Go();
154:                       if ((rc ColumnText.NO_MORE_TEXT) != 0)
155:                           break;
156:                       // we run out of column. Let's go to another one
157:                       ++currentColumn;
158:                       if (currentColumn >= allColumns.Length)
159:                           break;
160:                       ct.SetSimpleColumn(allColumns[currentColumn], document.Bottom,
161:                           allColumns[currentColumn] + columnWidthtopColumn15Element.ALIGN_JUSTIFIED);
162:                       ct.SetLeading(21);
163:                   }
164:                   // step 5: we close the document
165:                   document.Close();
166:               }
167:               catch (Exception de
168:               {
169:                   Console.Error.WriteLine(de.Message);
170:                   Console.Error.WriteLine(de.StackTrace);
171:               }
172:           }
173:       }
174:   }