zhao
2021-07-07 2fdf959ac739edd6de84aa8053b8b9683dce8e8b
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
//============================================================================
//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.Collections;
using System.Drawing;
 
namespace HH.WMS.Utils.Gios.Pdf
{
    /// <summary>
    /// an area to be use for Text writingsz
    /// </summary>
    public class PdfTextArea : PdfObject
    {
        internal ArrayList tl;
        internal string text;
        internal PdfDocument PdfDocument
        {
            get
            {
                return this.PdfArea.PdfDocument;
            }
        }
        //internal ArrayList TextLines;
        internal PdfArea textArea;
        /// <summary>
        /// gets the Area available for the Text
        /// </summary>
        public PdfArea PdfArea
        {
            get
            {
                return this.textArea;
            }
        }
        internal System.Drawing.Font Font;
        internal System.Drawing.Color Color=System.Drawing.Color.Black;
        internal int maxlines
        {
            get
            {
                    return (int)(this.textArea.height/this.lineHeight);
            }
        }
        internal double lineHeight;
        
        internal int DrawnLines
        {
            get
            {
                int l=this.RenderLines().Count;
                if (l>this.maxlines) return maxlines;
                return l;
            }
 
        }
        /// <summary>
        /// returns the Text that can't fit inside the estabilished area
        /// </summary>
        public string OverFlowText
        {
            get
            {
                ArrayList lines=this.RenderLines();
                string s="";
                for (int index=this.maxlines;index<lines.Count;index++)
                {
                    s+=lines[index];
                }
                return s;
            }
        }
        internal ContentAlignment textAlign;
        /// <summary>
        /// creates a new PdfTextArea
        /// </summary>
        /// <param name="Font">the font that will be used</param>
        /// <param name="Color">the color of the font that will be used</param>
        /// <param name="TextArea">the estabilished area for the Text</param>
        /// <param name="PdfTextAlign">the ContentAlignment for the Text inside the area</param>
        /// <param name="Text">the text that will be written inside the area</param>
        public PdfTextArea(System.Drawing.Font Font,System.Drawing.Color Color,PdfArea TextArea,ContentAlignment PdfTextAlign,string Text)
        {
            if (Text==null) throw new Exception("Text cannot be null.");
            this.Font=Font;
            this.Color=Color;
            this.textArea=TextArea;
            this.text=Text;
            this.textAlign=PdfTextAlign;
            this.lineHeight=(double)(Font.Size);
        }
        internal ArrayList RenderLines()
        {
            if (tl==null)
            {
                tl=new ArrayList();
                
                string line="",oldline="";
                double lineWidth=0,oldlineWidth=0;
                char[] textchars=text.ToCharArray();
 
                ArrayList words=new ArrayList();
 
                string aWord="";
                for (int index=0;index<textchars.Length;index++)
                {
                    char c=textchars[index];
                    switch (c)
                    {
                        case ' ':
                        {
                            if (aWord!="") words.Add(aWord); 
                            words.Add(" "); aWord=""; break;
                        }
                        case '\n': words.Add(aWord); words.Add("\n"); aWord=""; break;
                        default: aWord+=c; break;
                    }
                }
                if (aWord!="") words.Add(aWord);
                
                for (int s2index=0;s2index<words.Count;s2index++)
                {
                    string s=(string)words[s2index];
                    oldline=line;
                    line+=s;
                    double wordWidth=Utility.Measure(Font,s);
                    oldlineWidth=lineWidth;
                    lineWidth+=wordWidth;
                    
                    if (s=="\n")
                    {
                        tl.Add(oldline);
                        line="";
                        lineWidth=0;
                    }
                    else
                        if (lineWidth>this.textArea.width)
                    {
                        if (oldline!=" "&&oldline!="")
                        {
                            tl.Add(oldline); 
                            if (s==" ")
                            { line=""; lineWidth=0;}
                            else
                            { line=s; lineWidth=wordWidth;}
                        } 
                    }
                    if (s2index==words.Count-1)
                        if (line!=" "&&line!="")
                            tl.Add(line); 
                                        
                    
                    
                }
            }
            return this.tl;
        }
        internal string ToLineStream()
        {
            System.Text.StringBuilder sb=new System.Text.StringBuilder();
            //string text="";
            double posx,posy;
            ArrayList al=this.RenderLines();
            //if (this.LineFits)
            int l=al.Count;
            int ml=this.maxlines;
            for(int index=0;((index<l)&&(index<ml));index++)
            {
                //string line=(al[index] as string).TrimEnd(new char[]{' '});
                string line=(al[index] as string);
                posx=textArea.posx;
                posy=textArea.posy;
                double xdiff=0,ydiff=0;
                if ((this.textAlign!=ContentAlignment.MiddleLeft)&&
                    (this.textAlign!=ContentAlignment.TopLeft)&&
                    (this.textAlign!=ContentAlignment.BottomLeft))
                {
                    xdiff=textArea.width-Utility.Measure(this.Font,line);
                    if ((this.textAlign==ContentAlignment.TopCenter)||
                        (this.textAlign==ContentAlignment.MiddleCenter)||
                        (this.textAlign==ContentAlignment.BottomCenter)) xdiff=xdiff/2;
                }
                if ((this.textAlign!=ContentAlignment.TopCenter)&&
                    (this.textAlign!=ContentAlignment.TopLeft)&&
                    (this.textAlign!=ContentAlignment.TopRight))
                {
                    ydiff=(double)(this.lineHeight-(this.Font.Height*0.6)
                        +(this.maxlines-this.DrawnLines)*this.lineHeight);
                    if ((this.textAlign==ContentAlignment.MiddleLeft)||
                        (this.textAlign==ContentAlignment.MiddleCenter)||
                        (this.textAlign==ContentAlignment.MiddleRight)) ydiff=ydiff/2;
                }
                posy+=ydiff;
                sb.Append("1 0 0 1 ");
                sb.Append((posx+xdiff).ToString("0.##").Replace(",","."));
                sb.Append(" ");
                sb.Append((this.PdfDocument.PH-posy-(this.lineHeight*(index))-(Font.Height*0.525)).ToString("0.##").Replace(",","."));
                sb.Append(" Tm (");
                sb.Append(Utility.TextEncode(line));
                sb.Append(") Tj\n");
            }
            return sb.ToString();
        }
        internal string CompleteLineStream()
        {
            System.Text.StringBuilder sb=new System.Text.StringBuilder();
            sb.Append("BT\n");
            sb.Append(Utility.FontToFontLine(this.Font));
            sb.Append(Utility.ColorrgLine(this.Color));
            sb.Append(this.ToLineStream());
            sb.Append("ET\n");
            return sb.ToString();
        }
        
        internal override int StreamWrite(System.IO.Stream stream)
        {
            int num=this.id;
            string text=this.CompleteLineStream();
            Byte[] part2;
            if (PdfDocument.FlateCompression) part2=Utility.Deflate(text); else
                part2=System.Text.ASCIIEncoding.ASCII.GetBytes(text);
 
            string s1="";
            s1+=num.ToString()+" 0 obj\n";
            s1+="<< /Lenght "+part2.Length;
            if (PdfDocument.FlateCompression) s1+=" /Filter /FlateDecode";
            s1+=">>\n";
            s1+="stream\n";
 
            string s3="\nendstream\n";
            s3+="endobj\n";
                
            Byte[] part1=System.Text.ASCIIEncoding.ASCII.GetBytes(s1);
            Byte[] part3=System.Text.ASCIIEncoding.ASCII.GetBytes(s3);
                
                
            stream.Write(part1,0,part1.Length);
            stream.Write(part2,0,part2.Length);
            stream.Write(part3,0,part3.Length);
 
            return part1.Length+part2.Length+part3.Length;
        }
 
    }
}