-
Notifications
You must be signed in to change notification settings - Fork 0
/
PdfPage.cs
243 lines (199 loc) · 7.65 KB
/
PdfPage.cs
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
using String = System.String;
using Char = System.Char;
using IO = System.IO;
using Generic = System.Collections.Generic;
namespace Pdf {
public class PdfImage
{
public int Width, Height, Obj;
public PdfImage( int w, int h, int o ) { Width = w; Height = h; Obj = o; }
}
public struct PageLayout
{
public float Width, Height, MarginLeft, MarginRight, MarginTop, MarginBottom;
public PageLayout( int width, int height, int margin )
{
Width = width; Height = height;
MarginLeft = margin; MarginRight = margin; MarginTop = margin; MarginBottom = margin;
}
}
public class PdfPage
{
public PageLayout Layout;
public float X, Y; // Current text writing position ( useful when mixing graphics and text ).
public int Number;
// Graphics operations
public void Line( float x0, float y0, float x1, float y1 ) // Draw a line from (x0,y0) to (x1,y1)
{ Put("\n" + x0 + " " + y0 + " m " + x1 + " " + y1 + " l S"); }
public void Rect( float x0, float y0, float x1, float y1 ) // Draw a rectange corners (x0,y0) to (x1,y1)
{ Put("\n" + x0 + " " + y0 + " " + x1 + " " + y1 + " re S"); }
public void DrawImage( PdfImage I, float x, float y, float scale )
{
if ( I == null ) return;
float w = I.Width * scale;
float h = I.Height * scale;
NoteXobj( I.Obj );
Put( "\nq " + w.ToString("G5") + " 0 0 " + h.ToString("G5") + " " + x + " " + y + " cm /X" + I.Obj + " Do Q");
}
public void Put( String s )
{ for ( int i = 0; i < s.Length; i += 1 ) OS.WriteByte( (byte) s[i] ); }
// Text functions.
private Generic.List<byte> StrBuffer = new Generic.List<byte>(); // For accumulating a "Tj" string.
public void Txt( String s, int start, int end )
{
if ( start >= end ) return;
CurFont.Encode( s, start, end, StrBuffer );
}
public void Txt( String s ){ Txt( s, 0, s.Length ); } // Write string to Str Buffer ( for Tj )
public void FlushStrBuffer()
{
if ( StrBuffer.Count == 0 ) return;
if ( LastFont != CurFont || LastFontSize != FontSize )
{
if ( !Fonts.Contains( CurFont.Obj) ) Fonts.Add( CurFont.Obj );
TSW( "/F" + CurFont.Obj + " " + FontSize + " Tf" );
LastFont = CurFont;
LastFontSize = FontSize;
}
bool useHex = false;
foreach ( byte b in StrBuffer ) if ( b < 32 || b >= 128 ) { useHex = true; break; }
if ( useHex )
{
TS.WriteByte( (byte)'<' );
foreach ( byte b in StrBuffer )
{
int x = b>>4; x += x < 10 ? 48 : 55; TS.WriteByte( (byte)x );
x = b & 15; x += x < 10 ? 48 : 55; TS.WriteByte( (byte)x );
}
TSW( "> Tj" );
}
else
{
TS.WriteByte( (byte)'(' );
foreach ( byte b in StrBuffer )
{
if ( b == (byte)'(' || b == (byte)')' || b == (byte) '\\' ) TS.WriteByte( (byte) '\\' );
TS.WriteByte( b );
}
TSW( ") Tj" );
}
StrBuffer.Clear();
}
public void TSW( String s ) // Write directly to TS ( text stream )
{ for ( int i = 0; i < s.Length; i += 1 ) TS.WriteByte( (byte)s[i] ); }
// Functions to update text style.
private PdfFont CurFont = null, LastFont = null;
private int FontSize, LastFontSize, Super=0;
private float CharSpacing=0;
private String Color = null;
public String Other = null;
public void SetCharSpacing( float x )
{
if ( CharSpacing != x )
{
FlushStrBuffer();
CharSpacing = x;
TSW( " " + x + " Tc" );
}
}
public void SetFont( PdfFont f, int fontSize )
{
if ( CurFont != f || FontSize != fontSize )
{
FlushStrBuffer();
CurFont = f;
FontSize = fontSize;
// Delay the actual Tf operation until the next Tj, to eliminate redundant Tf operations.
}
}
public void SetSuper( int super )
{ if ( Super != super ) { FlushStrBuffer(); Super = super; TSW( " " + super + " Ts" ); } }
public void SetColor( String color )
{ if ( Color != color ) { FlushStrBuffer(); Color = color; TSW( " " + color + " rg" ); } }
public void SetOther( String other )
{ FlushStrBuffer(); Other = other; TSW( " " + other ); }
// Text positioning functions ( newline ).
public void Td(float x, float y) // Start a new line ( relative to previous line )
{
FlushStrBuffer();
TSW( "\n" + x + " " + y + " Td " );
X += x; Y += y;
}
public void Goto( float x, float y ) // Start a new line ( absolute position )
{ Td(x - X, y - Y); }
// Functions for drawing a path.
float Px, Py, Ps, Pcx, Pcy;
public void PathInit( float x, float y, float s ){ Px = x; Py = y; Ps = s; }
public void PathMove( float x, float y ){ x=Px+x*Ps; y=Py+y*Ps; Put( " " + x + " " + y + " m" ); Pcx=x; Pcy=y; }
public void PathLine( float x, float y ){ x=Px+x*Ps; y=Py+y*Ps; Put( " " + x + " " + y + " l" ); Pcx=x; Pcy=y; }
public void PathCurve( float x1, float y1, float x2, float y2 )
{
x1 = Px+x1*Ps; y1 = Py+y1*Ps;
x2 = Px+x2*Ps; y2 = Py+y2*Ps; // shift and scale
float x3 = Pcx/3 + 2*x1/3;
float y3 = Pcy/3 + 2*y1/3;
float x4 = x2/3 + 2*x1/3;
float y4 = y2/3 + 2*y1/3;
Put( " " + x3 + " " + y3 + " " + x4 + " " + y4 + " " + x2 + " " + y2 + " c" ); // cubic bezier
Pcx = x2; Pcy = y2;
}
public void PathFill(){ Put( " f"); }
public void InitTxtFrom( PdfPage p )
{
if ( p == null ) return;
if ( p.CurFont != null ) SetFont( p.CurFont, p.FontSize );
if ( p.Super!=0 ) SetSuper( p.Super );
if ( p.Color != null && p.Color != "0 0 0" ) SetColor( p.Color );
if ( p.Other != null ) SetOther( p.Other );
}
public void ClearState()
{
FlushStrBuffer();
X = 0; Y = 0; CurFont = null; LastFont = null;
Super = 0; Color = null; Other = null;
}
// Writing pages to the PDF file.
private System.IO.MemoryStream
OS = new System.IO.MemoryStream(), // Final output stream, includes graphics and text.
TS = new System.IO.MemoryStream(); // Text stream.
private readonly Generic.List<int> Fonts = new Generic.List<int>(); // Fonts used by page
private readonly Generic.List<int> Xobjs = new Generic.List<int>(); // XObjects used by page ( typically images )
private void NoteXobj( int objnum ){ if ( !Xobjs.Contains(objnum) ) Xobjs.Add(objnum); }
private void EndText() // Copies TS to OS enclosed by "BT" and "ET".
{
FlushStrBuffer();
Put( "\nBT" );
byte [] b = TS.ToArray();
OS.Write( b, 0, b.Length );
Put( "\nET" );
}
private static void PutResourceSet( PdfWriter w, Generic.List<int> S, String n1, String n2 )
{
if ( S.Count > 0 ){ w.Put( n1 + "<<" ); foreach (int i in S) w.Put( n2 + i + " " + i + " 0 R" ); w.Put( ">>" ); }
}
public static int WritePages( PdfWriter w, Generic.List<PdfPage> pages )
{
System.Text.StringBuilder kids = new System.Text.StringBuilder();
int pagesobj = w.AllocObj();
foreach ( PdfPage p in pages )
{
w.CP = p;
w.FinishPage();
p.EndText();
byte[] Content = p.OS.ToArray();
int contentobj = w.PutStream( Content );
int pageobj = w.StartObj();
kids.Append( pageobj + " 0 R " );
w.Put("<</Type/Page/Parent " + pagesobj
+ " 0 R/MediaBox[0 0 " + p.Layout.Width + " " + p.Layout.Height + "]/Contents " + contentobj
+ " 0 R/Resources <<");
PutResourceSet( w, p.Fonts, "/Font", "/F" );
PutResourceSet( w, p.Xobjs, "/XObject", "/X" );
w.Put( " >> >>");
w.EndObj();
}
w.StartObj( pagesobj ); w.Put("<</Type/Pages/Count " + pages.Count + "/Kids[" + kids + "]>>"); w.EndObj();
return pagesobj;
}
} // End class PdfPage
} // namespace