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
//============================================================================
//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.Text;
using System.Drawing;
 
namespace HH.WMS.Utils.Gios.Pdf
{
    /// <summary>
    /// a generic page for a PdfDocument
    /// </summary>
    public class PdfPage : PdfObject
    {
        private string FontsLine
        {
            get
            {
                string s= "Font << ";
                foreach (PdfFont pf in this.PdfDocument.FontList) s+="/"+pf.Name+" "+pf.HeadR;
                s+=">>\n";
                return s;
            }
        }
        private ArrayList Contents;
        internal ArrayList PagePdfObjects;
        
        internal string MediaBoxLine
        {
            get
            {
                return "/MediaBox [0 0 "+this.PdfDocument.PW.ToString("0.##").Replace(",",".")
                    +" "+this.PdfDocument.PH.ToString("0.##").Replace(",",".")+"]\n";
            }
        }
        internal string ContentsLine
        {
            get
            {
                string s="/Contents [";
                foreach (PdfObject po in this.PagePdfObjects) 
                {
                    if (po.GetType()!=typeof(PdfImage)) s+=po.HeadR;
                    /*if (po.GetType()==typeof(PdfRectangle)) s+=po.HeadR;
                    if (po.GetType()==typeof(PdfCircle)) s+=po.HeadR;
                    if (po.GetType()==typeof(PdfLine)) s+=po.HeadR;
                    //if (po.GetType()==typeof(PdfGrid)) s+=po.HeadR;
                    //if (po.GetType()==typeof(PdfTable)) s+=po.HeadR;
                    if (po.GetType()==typeof(PdfTablePage)) s+=po.HeadR;
                    if (po.GetType()==typeof(PdfTextArea)) s+=po.HeadR;
                    if (po.GetType()==typeof(PdfImageContent)) s+=po.HeadR;*/
                }
                s+="]\n";
                return s;
            }
        }
        internal string XObjectLine
        {
            get
            {
                string s="/XObject <<";
                foreach (PdfObject po in this.PagePdfObjects) 
                {
                    if (po.GetType()==typeof(PdfImage)) s+="/I"+po.ID+" "+po.HeadR;
                }
                s+=" >>\n";
                return s;
            }
        }
        
        internal PdfPage()
        {
            Contents=new ArrayList();
            PagePdfObjects=new ArrayList();
        }
        /// <summary>
        /// adds a Pdf Element into this PdfPage.
        /// </summary>
        /// <param name="PdfImage"></param>
        /// <param name="posx"></param>
        /// <param name="posy"></param>
        public void Add(PdfImage PdfImage,double posx,double posy)
        {
            this.PagePdfObjects.Add(PdfImage);
            this.PagePdfObjects.Add(new PdfImageContent(PdfDocument.GetNextId,"I"+PdfImage.ID,PdfImage.ID,PdfImage.Width,PdfImage.Height,posx,this.PdfDocument.PH-posy,PdfImage.bmp.HorizontalResolution));
        }
        /// <summary>
        /// adds a Pdf Element into this PdfPage.
        /// </summary>
        /// <param name="PdfImage"></param>
        /// <param name="posx">The target X positioning of the picture.</param>
        /// <param name="posy">The target Y positioning of the picture.</param>
        /// <param name="DPI">The target resolution of the picture.</param>
        public void Add(PdfImage PdfImage,double posx,double posy,double DPI)
        {
            if (DPI<=0) throw new Exception("DPI must be greater than zero.");
            this.PagePdfObjects.Add(PdfImage);
            this.PagePdfObjects.Add(new PdfImageContent(PdfDocument.GetNextId,"I"+PdfImage.ID,PdfImage.ID,PdfImage.Width,PdfImage.Height,posx,this.PdfDocument.PH-posy,DPI));
        }
        /// <summary>
        /// adds a Pdf Element into this PdfPage.
        /// </summary>
        /// <param name="PdfRectangle"></param>
        public void Add(PdfRectangle PdfRectangle)
        {
            PdfRectangle.ID=this.PdfDocument.GetNextId;
            this.PagePdfObjects.Add(PdfRectangle);
        }
        /// <summary>
        /// adds a Pdf Element into this PdfPage.
        /// </summary>
        /// <param name="PdfCircle"></param>
        public void Add(PdfCircle PdfCircle)
        {
            PdfCircle.ID=this.PdfDocument.GetNextId;
            this.PagePdfObjects.Add(PdfCircle);
        }
        /// <summary>
        /// adds a Pdf Element into this PdfPage.
        /// </summary>
        /// <param name="PdfLine"></param>
        public void Add(PdfLine PdfLine)
        {
            PdfLine.ID=this.PdfDocument.GetNextId;
            this.PagePdfObjects.Add(PdfLine);
        }
        /// <summary>
        /// adds a Pdf Element into this PdfPage.
        /// </summary>
        /// <param name="PdfTablePage"></param>
        public void Add(PdfTablePage PdfTablePage)
        {
            if (PdfTablePage!=null)
            {
                
                PdfTablePage.ID=this.PdfDocument.GetNextId;
                this.PagePdfObjects.Add(PdfTablePage);
            }
        }
        /// <summary>
        /// adds a Pdf Element into this PdfPage.
        /// </summary>
        /// <param name="PdfTextArea"></param>
        public void Add(PdfTextArea PdfTextArea)
        {
            PdfTextArea.ID=this.PdfDocument.GetNextId;
            if (!this.PdfDocument.FontList.Contains(PdfTextArea.Font.Name))
                this.PdfDocument.AddFont(PdfTextArea.Font);
            this.PagePdfObjects.Add(PdfTextArea);
        }
                
        internal override int StreamWrite(System.IO.Stream stream)
        {
            string s="";
            s+=this.HeadObj;
            s+="<< /Type /Page\n/";
            s+="Parent "+PdfDocument.PdfRoot.HeadR+"\n";
            s+=this.MediaBoxLine;
            s+="/Resources\n";
            s+="<<\n/";
            s+=this.FontsLine;
            s+="/ProcSet [/PDF/ImageC/ImageI/ImageB/Text]\n";
            s+=this.XObjectLine;
            s+=">>\n";
            s+=this.ContentsLine;
            s+=" >>\n";
            s+="endobj\n";
            Byte[] b=ASCIIEncoding.ASCII.GetBytes(s);
            stream.Write(b,0,b.Length);
            return b.Length;
        }
        /// <summary>
        /// creates a copy of this page in order to use it as a template
        /// </summary>
        /// <returns></returns>
        public PdfPage CreateCopy()
        {
            PdfPage clone=new PdfPage();
            clone.PdfDocument=this.PdfDocument;
            foreach (PdfObject o in this.PagePdfObjects) clone.PagePdfObjects.Add(o);
            return clone;
        }
        /// <summary>
        /// save this page to the document and discard any further change
        /// </summary>
        public void SaveToDocument ()
        {
            /*if (!PdfDocument.Registered) 
                this.Add(new PdfTextArea(
                    new System.Drawing.Font("Courier New",38,System.Drawing.FontStyle.Bold)
                    ,System.Drawing.Color.Red
                    ,new PdfArea(0,0,PdfDocument.PageWidth,PdfDocument.PageHeight)
                    ,ContentAlignment.MiddleCenter,"This is a Demo Library, please register!\nsee http://www.gios-tech.com"));
        */    this.id=this.PdfDocument.GetNextId;
            this.PdfDocument.AddPdfObject(this);
            foreach (PdfObject o2 in this.PagePdfObjects) 
            {
                this.PdfDocument.AddPdfObject(o2);
            }
            
        }
    }
}