zhao
2021-07-09 0821715ebc11d3934d0594a1cc2c39686d808906
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
//============================================================================
//Gios Pdf.NET - A library for exporting Pdf Documents in C#
//Copyright (C) 2005  Paolo Gios - www.paologios.com
//
//This library is free software; you can redistribute it and/or
//modify it under the terms of the GNU Lesser General Public
//License as published by the Free Software Foundation; either
//version 2.1 of the License, or (at your option) any later version.
//
//This library is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//Lesser General Public License for more details.
//
//You should have received a copy of the GNU Lesser General Public
//License along with this library; if not, write to the Free Software
//Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//=============================================================================
using System;
using System.Data;
using System.Collections;
using System.Text;
using System.Drawing;
using System.IO;
 
namespace HH.WMS.Utils.Gios.Pdf
{
    /// <summary>
    /// the generic Pdf Table class.
    /// </summary>
    public class PdfTable :HH.WMS.Utils.Gios.Pdf.PdfCellRange
    {
        internal int columns,rows;
        internal Hashtable cells;
        internal PdfArea TableArea;
        internal PdfTable header=null;
        internal PdfDocument PdfDocument;
        internal int renderingIndex;
        internal int renderingRows;
        internal bool visibleHeaders=true;
        internal ArrayList pdfRows;
        internal ArrayList pdfColumns;
        /// <summary>
        /// gets or sets if table headers will be visible.
        /// </summary>
        public bool VisibleHeaders
        {
            get
            {
                return this.visibleHeaders;
            }
            set
            {
                this.visibleHeaders=value;
            }
        }
        /// <summary>
        /// returns the header of the the Table. It will be considered as a usual PdfRow.
        /// </summary>
        public PdfRow HeadersRow
        {
            get
            {
                return this.header.Rows[0];
            }
        }
        private PdfRow[] _Rows;
        /// <summary>
        /// the Collection of the Rows of the Table.
        /// </summary>
        public PdfRow[] Rows
        {
            get
            {
                if (this._Rows==null) this._Rows=this.pdfRows.ToArray(typeof(PdfRow)) as PdfRow[];
                return this._Rows;
            }
        }
        private PdfColumn[] _Columns;
        /// <summary>
        /// the Collection of the Columns of the Table.
        /// </summary>
        public PdfColumn[] Columns
        {
            get
            {
                if (this._Columns==null) this._Columns=this.pdfColumns.ToArray(typeof(PdfColumn)) as PdfColumn[];
                return this._Columns;
            }
        }
        private double borderWidth;
        private Color borderColor;
        private BorderType borderType;
        /// <summary>
        /// sets the borders style of the Table.
        /// </summary>
        /// <param name="BorderColor"></param>
        /// <param name="BorderWidth"></param>
        /// <param name="BorderType"></param>
        public void SetBorders(Color BorderColor,double BorderWidth,BorderType BorderType)
        {
            if (BorderWidth<=0) throw new Exception("BorderWidth must be grater than zero.");
            this.borderColor=BorderColor;
            this.borderType=BorderType;
            this.borderWidth=BorderWidth;
            if (this.header!=null)
            {
                this.header.borderColor=BorderColor;
                this.header.borderType=BorderType;
                this.header.borderWidth=BorderWidth;
            }
        }
        /// <summary>
        /// sets the widths of the Columns.
        /// </summary>
        /// <param name="ColumnsWidthArray"></param>
        public void SetColumnsWidth(int[] ColumnsWidthArray)
        {
            if (ColumnsWidthArray.Length>this.columns) throw new Exception("Table has only "+this.columns+" columns.");
            for (int index=0;index<ColumnsWidthArray.Length;index++)
            {
                if (ColumnsWidthArray[index]<=0) throw new Exception("Column size must be greater than zero.");
                this.Columns[index].SetWidth(ColumnsWidthArray[index]);
            }
        }
        /// <summary>
        /// gets a Cell of the Table.
        /// </summary>
        /// <param name="Row"></param>
        /// <param name="Column"></param>
        /// <returns></returns>
        public PdfCell Cell(int Row,int Column)
        {
            object o=this.cells[Row+","+Column];
            if (o==null) throw new Exception("Cell ["+Row+","+Column+"] does not exist in the Table.");
            return o as PdfCell;
        }
        /// <summary>
        /// gets a range of Cells of the Table.
        /// </summary>
        /// <param name="startRow"></param>
        /// <param name="startColumn"></param>
        /// <param name="endRow"></param>
        /// <param name="endColumn"></param>
        /// <returns></returns>
        public PdfCellRange CellRange(int startRow,int startColumn,int endRow,int endColumn)
        {
            return new PdfCellRange(this,startRow,startColumn,endRow,endColumn);
        }
        internal PdfTable(PdfDocument PdfDocument,ContentAlignment DefaultContentAlignment,Font Font,Color DefaultForegroundColor,int Rows
            ,int Columns,double CellPadding)
        {
            cells=new Hashtable();
            pdfRows=new ArrayList();
            this.owner=this;
            this.PdfDocument=PdfDocument;
            this.borderWidth=0;
            this.pdfColumns=new ArrayList();
            this.rows=Rows;
            this.startRow=0;
            this.startColumn=0;
            this.endColumn=Columns-1;
            this.endRow=Rows-1;
            this.columns=Columns;
            
            for (int c=0;c<columns;c++)
                for (int r=0;r<rows;r++)
                {
                    this.cells[r+","+c]=new PdfCell(this,r,c,DefaultContentAlignment,DefaultForegroundColor
                        ,Font,CellPadding);
                }
            for (int r=0;r<rows;r++)
            {
                this.pdfRows.Add(new PdfRow(this,r));
            }
            for (int c=0;c<columns;c++)
            {
                PdfColumn pc=new PdfColumn(this,c);
                pc.Width=100/this.columns;
                this.pdfColumns.Add(pc);
            }
        }
        /// <summary>
        /// sets the Height of the Rows of the Table. We suggest you not to use it
        /// unless each rows contains more or less the same quantity of Text.
        /// </summary>
        /// <param name="RowHeight"></param>
        public void SetRowHeight(double RowHeight)
        {
            if (RowHeight<=0) throw new Exception("Row Height must be grater than zero.");
            foreach (PdfRow pr in this.Rows) pr.SetRowHeight(RowHeight);
        }
        /// <summary>
        /// Imports text from a datatable.
        /// </summary>
        /// <param name="dt">The Source Datatable</param>
        /// <param name="PdfTableStartRow">the starting row of the Pdf Table that will import datas.</param>
        /// <param name="PdfTableStartColumn">the starting column of the Pdf Table that will import datas.</param>
        /// <param name="DataTableStartRow">the starting row of the DataTable that will export datas.</param>
        /// <param name="DataTableEndRow">the ending row of the DataTable that will export datas.</param>
        public void ImportDataTable(DataTable dt,int PdfTableStartRow,int PdfTableStartColumn,
            int DataTableStartRow,int DataTableEndRow)
        {
            for (int r=DataTableStartRow;((r<dt.Rows.Count)&&(r<=DataTableEndRow)&&
                (r+PdfTableStartRow-DataTableStartRow<this.rows));r++)
            {
                for (int c=0;((c<dt.Columns.Count)&&(c+PdfTableStartColumn<this.columns));c++)
                {
                    if (c+PdfTableStartColumn>=0&&c+PdfTableStartColumn<dt.Columns.Count) 
                    {
                        Cell(r+PdfTableStartRow-DataTableStartRow,c+PdfTableStartColumn)
                            .SetContent(dt.Rows[r][c]);    
                    }
                }
            }
            for (int c=0;((c<dt.Columns.Count)&&(c+PdfTableStartColumn<this.columns));c++)
            {
                if (c+PdfTableStartColumn>=0&&c+PdfTableStartColumn<dt.Columns.Count) 
                {
                    this.HeadersRow[c+PdfTableStartColumn].SetContent(dt.Columns[c].ColumnName);
                }
            }
        }
        /// <summary>
        /// Imports text from a datatable.
        /// </summary>
        /// <param name="dt">The Source Datatable</param>
        /// <param name="PdfTableStartRow">the starting row of the Pdf Table that will import datas.</param>
        /// <param name="PdfTableStartColumn">the starting column of the Pdf Table that will import datas.</param>
        public void ImportDataTable(DataTable dt,int PdfTableStartRow,int PdfTableStartColumn)
        {
            ImportDataTable(dt,PdfTableStartRow,PdfTableStartColumn,0,999999);
        }
        /// <summary>
        /// Imports text from a datatable.
        /// </summary>
        /// <param name="dt">The Source Datatable</param>
        public void ImportDataTable(DataTable dt)
        {
            ImportDataTable(dt,0,0,0,999999);
        }
        /// <summary>
        /// returns true if all the pages of the Table are already rasterized to a TablePage.
        /// </summary>
        public bool AllTablePagesCreated
        {
            get
            {
                if (this.renderingIndex+this.renderingRows>this.rows-1) return true;
                return false;
            }
        }
        /// <summary>
        /// Creates the TablePage, the rasterized page of a Table.
        /// </summary>
        /// <param name="PageArea"></param>
        /// <returns></returns>
        public PdfTablePage CreateTablePage(PdfArea PageArea)
        {
            this.TableArea=PageArea.Clone();
            PdfTablePage ptp;
            if (!this.visibleHeaders)
                ptp=this.createTablePage();
            else
            
            {
                this.header.TableArea=PageArea.Clone();
                this.header.TableArea.height=this.HeadersRow.Height;
                this.TableArea.posy+=this.HeadersRow.Height;
                this.TableArea.height-=this.HeadersRow.Height;
                
                ptp=this.createTablePage();
                ptp.stream+=this.header.createTablePage().stream;
 
                this.header.renderingIndex=0;
            }
 
                
            foreach (PdfColumn pc in this.Columns) pc.CompensatedWidth=-1;
            foreach (PdfColumn pc in this.header.Columns) pc.CompensatedWidth=-1;
            return ptp;
        }
        internal PdfTablePage createTablePage()
        {
            int index=this.renderingIndex;
            double h=this.Rows[index].Height;
            double oh=0;
            while ((h<=this.TableArea.height)&&(index<this.rows))
            {
                index++;
                oh=h;
                if (index<this.rows) h+=this.Rows[index].Height;
            }
            this.renderingRows=index-this.renderingIndex;
            this.TableArea.height=(double)oh;
            PdfTablePage ptp=new PdfTablePage(renderingIndex,renderingIndex+renderingRows-1,columns);
            
            //ptp.stream="";
            //caculates areas
            double y=this.TableArea.posy;
            for (int rowIndex=this.renderingIndex;(rowIndex<this.renderingIndex+this.renderingRows);rowIndex++)
            {
                double x=this.TableArea.posx;
                for (int columnIndex=0;columnIndex<this.columns;columnIndex++)
                {
                    PdfCell pc=this.Cell(rowIndex,columnIndex);
                    double width=pc.Width;
                    pc.area=new PdfArea(this.PdfDocument,x,y,width,pc.Height);
                    x+=width;
                }
                y+=this.Rows[rowIndex].Height;
            }
            
            for (int rowIndex=this.renderingIndex;(rowIndex<this.renderingIndex+this.renderingRows);rowIndex++)
                for (int columnIndex=0;columnIndex<this.columns;columnIndex++)
                {
                    PdfCell pc=this.Cell(rowIndex,columnIndex);
                    if (!pc.isSpanned)    ptp.cellAreas.Add(pc.row+","+pc.column,pc.Area);
                }
 
            ptp.stream=this.ToLineStream();
            ptp.SetArea();
            
            this.renderingIndex=index;
            this.renderingRows=0;
            return ptp;
        }
    
        internal string ToLineStream()
        {
            System.Text.StringBuilder sb=new StringBuilder();
            // draw background rectangles
            for (int rowIndex=this.renderingIndex;(rowIndex<this.renderingIndex+this.renderingRows);rowIndex++)
            {
                for (int columnIndex=0;columnIndex<this.columns;columnIndex++)
                {
                    PdfCell pc=this.Cell(rowIndex,columnIndex);
                    if (!pc.isSpanned)
                    {
                        if (!pc.transparent)
                            sb.Append(pc.Area.InnerArea(1).ToRectangle(pc.backgroundColor
                                ,pc.backgroundColor).ToLineStream());
                    }
                    
                }
            }
            sb.Append("BT\n");
            
            Font actualFont=null;
            Color actualColor=Color.Black;
            sb.Append(Utility.ColorrgLine(Color.Black));
            for (int rowIndex=this.renderingIndex;(rowIndex<this.renderingIndex+this.renderingRows);rowIndex++)
            {
                for (int columnIndex=0;columnIndex<this.columns;columnIndex++)
                {
                    PdfCell pc=this.Cell(rowIndex,columnIndex);
                    if (!pc.isSpanned)
                    {
                        PdfTextArea pt=new PdfTextArea(pc.Font,pc.foregroundColor
                            ,pc.Area.InnerArea(pc.cellPadding*2),pc.ContentAlignment,pc.text);
                        
                        if (pc.Font!=actualFont)
                        {
                            string actualFontLine=Utility.FontToFontLine(pc.Font);
                            if (!this.PdfDocument.FontNameList.Contains(PdfFont.FontToPdfType(pc.Font)))
                                this.PdfDocument.AddFont(pc.Font);
                            sb.Append(actualFontLine);
                            actualFont=pc.Font;
                        }
                        if (pc.foregroundColor!=actualColor)
                        {
                            sb.Append(Utility.ColorrgLine(pc.foregroundColor));
                            actualColor=pc.foregroundColor;
                        }
                        sb.Append(pt.ToLineStream());
                    }
                }
                
            }
            sb.Append("ET\n");
            
            if (this.borderWidth>0)
            {
                sb.Append(new PdfRectangle(this.PdfDocument,new PdfArea(this.PdfDocument,0,0,1,1),this.borderColor
                    ,this.borderWidth).ToColorAndWidthStream());
                int bt=(int)this.borderType;
                if ((bt==1)||(bt==3)||(bt==5)||(bt==6))
                    sb.Append(this.TableArea.ToRectangle(this.borderColor,this.borderWidth).ToRectangleStream());
                for (int rowIndex=this.renderingIndex;(rowIndex<this.renderingIndex+this.renderingRows);rowIndex++)
                    for (int columnIndex=0;columnIndex<this.columns;columnIndex++)
                    {
                        PdfCell pc=this.Cell(rowIndex,columnIndex);
                        if (!pc.isSpanned)
                        {
                            if (rowIndex!=this.renderingIndex)
                            {
                                if ((bt==6)||(bt==2)||(bt==3)||(bt==7))
                                    sb.Append(pc.Area.UpperBound(this.borderColor,this.borderWidth).ToLineStream());
                            }
                            if (columnIndex!=0)
                            {
                                if ((bt==6)||(bt==4)||(bt==5)||(bt==7))
                                    sb.Append(pc.Area.LeftBound(this.borderColor,this.borderWidth).ToLineStream());
                            }
                        }
                    }
            }
            return sb.ToString();
        }
    }
}