Debian Codegraph @

if/c#/demo/LineDemo.cs

raw
1 /*
2 * << Haru Free PDF Library 2.0.5 >> -- LineDemo.cs
3 *
4 * Copyright (c) 1999-2006 Takeshi Kanno <[email protected]>
5 *
6 * Permission to use, copy, modify, distribute and sell this software
7 * and its documentation for any purpose is hereby granted without fee,
8 * provided that the above copyright notice appear in all copies and
9 * that both that copyright notice and this permission notice appear
10 * in supporting documentation.
11 * It is provided "as is" without express or implied warranty.
12 *
13 */
14
15 using System;
16 using System.IO;
17 using HPdf;
18
19
20 public class LineDemo {
21 private static void DrawLine(HPdfPage page, float x, float y, string label) {
22 page.BeginText();
23 page.MoveTextPos(x, y - 10);
24 page.ShowText(label);
25 page.EndText();
26
27 page.MoveTo(x, y - 15);
28 page.LineTo(x + 220, y - 15);
29 page.Stroke();
30 }
31
32 private static void DrawLine2(HPdfPage page, float x, float y, string label) {
33 page.BeginText();
34 page.MoveTextPos(x, y);
35 page.ShowText(label);
36 page.EndText();
37
38 page.MoveTo(x + 30, y - 25);
39 page.LineTo(x + 160, y - 25);
40 page.Stroke();
41 }
42
43 private static void DrawRect(HPdfPage page, float x, float y, string label) {
44 page.BeginText();
45 page.MoveTextPos(x, y - 10);
46 page.ShowText(label);
47 page.EndText();
48
49 page.Rectangle(x, y - 40, 220, 25);
50 }
51
52 public static void Main() {
53 Console.WriteLine("libhpdf-" + HPdfDoc.HPdfGetVersion());
54
55 try {
56 const string page_title = "LineDemo";
57
58 HPdfDoc pdf = new HPdfDoc();
59
60 /* create default-font */
61 HPdfFont font = pdf.GetFont ("Helvetica", null);
62
63 /* add a new page object. */
64 HPdfPage page = pdf.AddPage();
65
66 /* print the lines of the page. */
67 page.SetLineWidth(1);
68 page.Rectangle(50, 50, page.GetWidth() - 100,
69 page.GetHeight() - 110);
70 page.Stroke();
71
72 /* print the title of the page (with positioning center). */
73 page.SetFontAndSize(font, 24);
74 float tw = page.TextWidth(page_title);
75 page.BeginText();
76 page.MoveTextPos ((page.GetWidth() - tw) / 2,
77 page.GetHeight() - 50);
78 page.ShowText(page_title);
79 page.EndText();
80
81 page.SetFontAndSize(font, 10);
82
83 /* Draw verious widths of lines. */
84 page.SetLineWidth(0);
85 DrawLine(page, 60, 770, "line width = 0");
86
87 page.SetLineWidth(1.0f);
88 DrawLine(page, 60, 740, "line width = 1.0");
89
90 page.SetLineWidth(2.0f);
91 DrawLine(page, 60, 710, "line width = 2.0");
92
93 /* Line dash pattern */
94 ushort[] dash_mode1 = new ushort[] {3};
95 ushort[] dash_mode2 = new ushort[] {3, 7};
96 ushort[] dash_mode3 = new ushort[] {8, 7, 2, 7};
97
98 page.SetLineWidth(1.0f);
99
100 page.SetDash(dash_mode1, 1);
101 DrawLine(page, 60, 680, "dash_ptn=[3], phase=1 -- 2 on, 3 off, 3 on...");
102
103 Console.Error.WriteLine("001");
104 page.SetDash(dash_mode2, 2);
105 DrawLine(page, 60, 650, "dash_ptn=[7, 3], phase=2 -- 5 on 3 off, 7 on,...");
106
107 page.SetDash(dash_mode3, 0);
108 DrawLine(page, 60, 620, "dash_ptn=[8, 7, 2, 7], phase=0");
109
110 page.SetDash(null, 0);
111
112 page.SetLineWidth(30);
113 page.SetRGBStroke(0.0f, 0.5f, 0.0f);
114
115 /* Line Cap Style */
116 page.SetLineCap(HPdfLineCap.HPDF_BUTT_END);
117 DrawLine2(page, 60, 570, "HPDF_BUTT_END");
118
119 page.SetLineCap(HPdfLineCap.HPDF_ROUND_END);
120 DrawLine2(page, 60, 505, "HPDF_ROUND_END");
121
122 page.SetLineCap(HPdfLineCap.HPDF_PROJECTING_SCUARE_END);
123 DrawLine2(page, 60, 440, "HPDF_PROJECTING_SCUARE_END");
124
125 /* Line Join Style */
126 page.SetLineWidth(30);
127 page.SetRGBStroke(0.0f, 0.0f, 0.5f);
128
129 page.SetLineJoin(HPdfLineJoin.HPDF_MITER_JOIN);
130 page.MoveTo(120, 300);
131 page.LineTo(160, 340);
132 page.LineTo(200, 300);
133 page.Stroke();
134
135 page.BeginText();
136 page.MoveTextPos(60, 360);
137 page.ShowText("HPDF_MITER_JOIN");
138 page.EndText();
139
140 page.SetLineJoin(HPdfLineJoin.HPDF_ROUND_JOIN);
141 page.MoveTo(120, 195);
142 page.LineTo(160, 235);
143 page.LineTo(200, 195);
144 page.Stroke();
145
146 page.BeginText();
147 page.MoveTextPos(60, 255);
148 page.ShowText("HPDF_ROUND_JOIN");
149 page.EndText();
150
151 page.SetLineJoin(HPdfLineJoin.HPDF_BEVEL_JOIN);
152 page.MoveTo(120, 90);
153 page.LineTo(160, 130);
154 page.LineTo(200, 90);
155 page.Stroke();
156
157 page.BeginText();
158 page.MoveTextPos(60, 150);
159 page.ShowText("HPDF_BEVEL_JOIN");
160 page.EndText();
161
162 /* Draw Rectangle */
163 page.SetLineWidth(2);
164 page.SetRGBStroke(0, 0, 0);
165 page.SetRGBFill(0.75f, 0.0f, 0.0f);
166
167 DrawRect(page, 300, 770, "Stroke");
168 page.Stroke();
169
170 DrawRect(page, 300, 720, "Fill");
171 page.Fill();
172
173 DrawRect(page, 300, 670, "Fill then Stroke");
174 page.FillStroke();
175
176 /* Clip Rect */
177 page.GSave(); /* Save the current graphic state */
178 DrawRect(page, 300, 620, "Clip Rectangle");
179 page.Clip();
180 page.Stroke();
181 page.SetFontAndSize(font, 13);
182
183 page.BeginText();
184 page.MoveTextPos(290, 600);
185 page.SetTextLeading(12);
186 page.ShowText("Clip Clip Clip Clip Clip Clipi Clip Clip Clip");
187 page.ShowTextNextLine("Clip Clip Clip Clip Clip Clip Clip Clip Clip");
188 page.ShowTextNextLine("Clip Clip Clip Clip Clip Clip Clip Clip Clip");
189 page.EndText();
190 page.GRestore();
191
192 /* Curve Example(CurveTo2) */
193 float x = 330;
194 float y = 440;
195 float x1 = 430;
196 float y1 = 530;
197 float x2 = 480;
198 float y2 = 470;
199 float x3 = 480;
200 float y3 = 90;
201
202 page.SetRGBFill(0, 0, 0);
203
204 page.BeginText();
205 page.MoveTextPos(300, 540);
206 page.ShowText("CurveTo2(x1, y1, x2. y2)");
207 page.EndText();
208
209 page.BeginText();
210 page.MoveTextPos(x + 5, y - 5);
211 page.ShowText("Current point");
212 page.MoveTextPos(x1 - x, y1 - y);
213 page.ShowText("(x1, y1)");
214 page.MoveTextPos(x2 - x1, y2 - y1);
215 page.ShowText("(x2, y2)");
216 page.EndText();
217
218 page.SetDash(dash_mode1, 0);
219
220 page.SetLineWidth(0.5f);
221 page.MoveTo(x1, y1);
222 page.LineTo(x2, y2);
223 page.Stroke();
224
225 page.SetDash(null, 0);
226
227 page.SetLineWidth(1.5f);
228
229 page.MoveTo(x, y);
230 page.CurveTo2(x1, y1, x2, y2);
231 page.Stroke();
232
233 /* Curve Example(CurveTo3) */
234 y -= 150;
235 y1 -= 150;
236 y2 -= 150;
237
238 page.BeginText();
239 page.MoveTextPos(300, 390);
240 page.ShowText("CurveTo3(x1, y1, x2. y2)");
241 page.EndText();
242
243 page.BeginText();
244 page.MoveTextPos(x + 5, y - 5);
245 page.ShowText("Current point");
246 page.MoveTextPos(x1 - x, y1 - y);
247 page.ShowText("(x1, y1)");
248 page.MoveTextPos(x2 - x1, y2 - y1);
249 page.ShowText("(x2, y2)");
250 page.EndText();
251
252 page.SetDash(dash_mode1, 0);
253
254 page.SetLineWidth(0.5f);
255 page.MoveTo(x, y);
256 page.LineTo(x1, y1);
257 page.Stroke();
258
259 page.SetDash(null, 0);
260
261 page.SetLineWidth(1.5f);
262 page.MoveTo(x, y);
263 page.CurveTo3(x1, y1, x2, y2);
264 page.Stroke();
265
266 /* Curve Example(CurveTo) */
267 y -= 150;
268 y1 -= 160;
269 y2 -= 130;
270 x2 += 10;
271
272 page.BeginText();
273 page.MoveTextPos(300, 240);
274 page.ShowText("CurveTo(x1, y1, x2. y2, x3, y3)");
275 page.EndText();
276
277 page.BeginText();
278 page.MoveTextPos(x + 5, y - 5);
279 page.ShowText("Current point");
280 page.MoveTextPos(x1 - x, y1 - y);
281 page.ShowText("(x1, y1)");
282 page.MoveTextPos(x2 - x1, y2 - y1);
283 page.ShowText("(x2, y2)");
284 page.MoveTextPos(x3 - x2, y3 - y2);
285 page.ShowText("(x3, y3)");
286 page.EndText();
287
288 page.SetDash(dash_mode1, 0);
289
290 page.SetLineWidth(0.5f);
291 page.MoveTo(x, y);
292 page.LineTo(x1, y1);
293 page.Stroke();
294 page.MoveTo(x2, y2);
295 page.LineTo(x3, y3);
296 page.Stroke();
297
298 page.SetDash(null, 0);
299
300 page.SetLineWidth(1.5f);
301 page.MoveTo(x, y);
302 page.CurveTo(x1, y1, x2, y2, x3, y3);
303 page.Stroke();
304
305 pdf.SaveToFile("LineDemo.pdf");
306 } catch (Exception e) {
307 Console.Error.WriteLine(e.Message);
308 }
309 }
310 }
311
312
313