| 1 |
/* |
| 2 |
* << Haru Free PDF Library 2.0.5 >> -- TextDemo.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 TextDemo { |
| 21 |
private static void ShowStripePattern (HPdfPage page, float x, float y) { |
| 22 |
int iy = 0; |
| 23 |
|
| 24 |
while (iy < 50) { |
| 25 |
page.SetRGBStroke(0.0f, 0.0f, 0.5f); |
| 26 |
page.SetLineWidth(1); |
| 27 |
page.MoveTo(x, y + iy); |
| 28 |
page.LineTo(x + page.TextWidth("ABCabc123"), |
| 29 |
y + iy); |
| 30 |
page.Stroke(); |
| 31 |
iy += 3; |
| 32 |
} |
| 33 |
|
| 34 |
page.SetLineWidth(2.5f); |
| 35 |
} |
| 36 |
|
| 37 |
private static void ShowDescription (HPdfPage page, float x, float y, |
| 38 |
string text) { |
| 39 |
float fsize = page.GetCurrentFontSize(); |
| 40 |
HPdfFont font = page.GetCurrentFont(); |
| 41 |
HPdfRGBColor c = page.GetRGBFill(); |
| 42 |
|
| 43 |
page.BeginText(); |
| 44 |
page.SetRGBFill(0, 0, 0); |
| 45 |
page.SetTextRenderingMode(HPdfTextRenderingMode.HPDF_FILL); |
| 46 |
page.SetFontAndSize(font, 10); |
| 47 |
page.TextOut(x, y - 12, text); |
| 48 |
page.EndText(); |
| 49 |
|
| 50 |
page.SetFontAndSize(font, fsize); |
| 51 |
page.SetRGBFill(c.r, c.g, c.b); |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
public static void Main() { |
| 57 |
const string page_title = "Text Demo"; |
| 58 |
const string samp_text = "abcdefgABCDEFG123!#$%&+-@?"; |
| 59 |
const string samp_text2 = "The quick brown fox jumps over the lazy dog."; |
| 60 |
Console.WriteLine("libhpdf-" + HPdfDoc.HPdfGetVersion()); |
| 61 |
|
| 62 |
try { |
| 63 |
HPdfDoc pdf = new HPdfDoc(); |
| 64 |
|
| 65 |
pdf.SetCompressionMode(HPdfDoc.HPDF_COMP_ALL); |
| 66 |
|
| 67 |
/* create default-font */ |
| 68 |
HPdfFont font = pdf.GetFont("Helvetica", null); |
| 69 |
|
| 70 |
/* add a new page object. */ |
| 71 |
HPdfPage page = pdf.AddPage(); |
| 72 |
|
| 73 |
/*print the title of the page (with positioning center). */ |
| 74 |
page.SetFontAndSize(font, 24); |
| 75 |
float tw = page.TextWidth(page_title); |
| 76 |
page.BeginText(); |
| 77 |
page.TextOut((page.GetWidth() - tw) / 2, |
| 78 |
page.GetHeight () - 50, page_title); |
| 79 |
page.EndText(); |
| 80 |
|
| 81 |
page.BeginText(); |
| 82 |
page.MoveTextPos(60, page.GetHeight() - 60); |
| 83 |
|
| 84 |
/* |
| 85 |
* font size |
| 86 |
*/ |
| 87 |
float fsize = 8; |
| 88 |
while (fsize < 60) { |
| 89 |
/* set style and size of font. */ |
| 90 |
page.SetFontAndSize(font, fsize); |
| 91 |
|
| 92 |
/* set the position of the text. */ |
| 93 |
page.MoveTextPos(0, -5 - fsize); |
| 94 |
|
| 95 |
/* measure the number of characters which included in the page. */ |
| 96 |
uint tlen = page.MeasureText(samp_text, |
| 97 |
page.GetWidth() - 120, false); |
| 98 |
|
| 99 |
page.ShowText(samp_text.Substring(0, (int)tlen)); |
| 100 |
|
| 101 |
/* print the description. */ |
| 102 |
page.MoveTextPos(0, -10); |
| 103 |
page.SetFontAndSize(font, 8); |
| 104 |
page.ShowText("Fontsize=" + fsize); |
| 105 |
|
| 106 |
fsize *= 1.5f; |
| 107 |
} |
| 108 |
|
| 109 |
/* |
| 110 |
* font color |
| 111 |
*/ |
| 112 |
page.SetFontAndSize(font, 8); |
| 113 |
page.MoveTextPos(0, -30); |
| 114 |
page.ShowText("Font color"); |
| 115 |
|
| 116 |
page.SetFontAndSize(font, 18); |
| 117 |
page.MoveTextPos(0, -20); |
| 118 |
int len = samp_text.Length; |
| 119 |
|
| 120 |
for (int i = 0; i < len; i++) { |
| 121 |
string buf; |
| 122 |
|
| 123 |
float r = (float)i / (float)len; |
| 124 |
float g = 1 - ((float)i / (float)len); |
| 125 |
buf = samp_text.Substring(i, 1); |
| 126 |
|
| 127 |
page.SetRGBFill(r, g, 0); |
| 128 |
page.ShowText(buf); |
| 129 |
} |
| 130 |
page.MoveTextPos(0, -25); |
| 131 |
|
| 132 |
for (int i = 0; i < len; i++) { |
| 133 |
string buf; |
| 134 |
|
| 135 |
float r = (float)i / (float)len; |
| 136 |
float b = 1 - ((float)i / (float)len); |
| 137 |
buf = samp_text.Substring(i, 1); |
| 138 |
|
| 139 |
page.SetRGBFill(r, 0, b); |
| 140 |
page.ShowText(buf); |
| 141 |
} |
| 142 |
page.MoveTextPos(0, -25); |
| 143 |
|
| 144 |
for (int i = 0; i < len; i++) { |
| 145 |
string buf; |
| 146 |
|
| 147 |
float b = (float)i / (float)len; |
| 148 |
float g = 1 - ((float)i / (float)len); |
| 149 |
buf = samp_text.Substring(i, 1); |
| 150 |
|
| 151 |
page.SetRGBFill(0, g, b); |
| 152 |
page.ShowText(buf); |
| 153 |
} |
| 154 |
|
| 155 |
page.EndText(); |
| 156 |
|
| 157 |
int ypos = 450; |
| 158 |
|
| 159 |
/* |
| 160 |
* Font rendering mode |
| 161 |
*/ |
| 162 |
page.SetFontAndSize(font, 32); |
| 163 |
page.SetRGBFill(0.5f, 0.5f, 0); |
| 164 |
page.SetLineWidth(1.5f); |
| 165 |
|
| 166 |
/* PDF_FILL */ |
| 167 |
ShowDescription(page, 60, ypos, |
| 168 |
"RenderingMode=PDF_FILL"); |
| 169 |
page.SetTextRenderingMode(HPdfTextRenderingMode.HPDF_FILL); |
| 170 |
page.BeginText(); |
| 171 |
page.TextOut(60, ypos, "ABCabc123"); |
| 172 |
page.EndText(); |
| 173 |
|
| 174 |
/*PDF_STROKE */ |
| 175 |
ShowDescription(page, 60, ypos - 50, |
| 176 |
"RenderingMode=PDF_STROKE"); |
| 177 |
page.SetTextRenderingMode(HPdfTextRenderingMode.HPDF_STROKE); |
| 178 |
page.BeginText(); |
| 179 |
page.TextOut(60, ypos - 50, "ABCabc123"); |
| 180 |
page.EndText(); |
| 181 |
|
| 182 |
/*PDF_FILL_THEN_STROKE */ |
| 183 |
ShowDescription(page, 60, ypos - 100, |
| 184 |
"RenderingMode=PDF_FILL_THEN_STROKE"); |
| 185 |
page.SetTextRenderingMode(HPdfTextRenderingMode.HPDF_FILL_THEN_STROKE); |
| 186 |
page.BeginText(); |
| 187 |
page.TextOut(60, ypos - 100, "ABCabc123"); |
| 188 |
page.EndText(); |
| 189 |
|
| 190 |
/*PDF_FILL_CLIPPING */ |
| 191 |
ShowDescription(page, 60, ypos - 150, |
| 192 |
"RenderingMode=PDF_FILL_CLIPPING"); |
| 193 |
page.GSave(); |
| 194 |
page.SetTextRenderingMode(HPdfTextRenderingMode.HPDF_FILL_CLIPPING); |
| 195 |
page.BeginText(); |
| 196 |
page.TextOut(60, ypos - 150, "ABCabc123"); |
| 197 |
page.EndText(); |
| 198 |
ShowStripePattern(page, 60, ypos - 150); |
| 199 |
page.GRestore(); |
| 200 |
|
| 201 |
/*PDF_STROKE_CLIPPING */ |
| 202 |
ShowDescription(page, 60, ypos - 200, |
| 203 |
"RenderingMode=PDF_STROKE_CLIPPING"); |
| 204 |
page.GSave(); |
| 205 |
page.SetTextRenderingMode(HPdfTextRenderingMode.HPDF_STROKE_CLIPPING); |
| 206 |
page.BeginText(); |
| 207 |
page.TextOut(60, ypos - 200, "ABCabc123"); |
| 208 |
page.EndText(); |
| 209 |
ShowStripePattern(page, 60, ypos - 200); |
| 210 |
page.GRestore(); |
| 211 |
|
| 212 |
/*PDF_FILL_STROKE_CLIPPING */ |
| 213 |
ShowDescription(page, 60, ypos - 250, |
| 214 |
"RenderingMode=PDF_FILL_STROKE_CLIPPING"); |
| 215 |
page.GSave(); |
| 216 |
page.SetTextRenderingMode(HPdfTextRenderingMode.HPDF_FILL_STROKE_CLIPPING); |
| 217 |
page.BeginText(); |
| 218 |
page.TextOut(60, ypos - 250, "ABCabc123"); |
| 219 |
page.EndText(); |
| 220 |
ShowStripePattern(page, 60, ypos - 250); |
| 221 |
page.GRestore(); |
| 222 |
|
| 223 |
/*Reset text attributes */ |
| 224 |
page.SetTextRenderingMode(HPdfTextRenderingMode.HPDF_FILL); |
| 225 |
page.SetRGBFill(0, 0, 0); |
| 226 |
page.SetFontAndSize(font, 30); |
| 227 |
|
| 228 |
|
| 229 |
/* |
| 230 |
* Rotating text |
| 231 |
*/ |
| 232 |
float angle1 = 30; /* A rotation of 30 degrees. */ |
| 233 |
float rad1 = angle1 / 180 * 3.141592f; /* Calcurate the radian value. */ |
| 234 |
|
| 235 |
ShowDescription(page, 320, ypos - 60, "Rotating text"); |
| 236 |
page.BeginText(); |
| 237 |
page.SetTextMatrix((float)Math.Cos(rad1), (float)Math.Sin(rad1), |
| 238 |
-(float)Math.Sin(rad1), (float)Math.Cos(rad1), |
| 239 |
330, ypos - 60); |
| 240 |
page.ShowText("ABCabc123"); |
| 241 |
page.EndText(); |
| 242 |
|
| 243 |
|
| 244 |
/* |
| 245 |
* Skewing text. |
| 246 |
*/ |
| 247 |
ShowDescription(page, 320, ypos - 120, "Skewing text"); |
| 248 |
page.BeginText(); |
| 249 |
|
| 250 |
angle1 = 10; |
| 251 |
float angle2 = 20; |
| 252 |
rad1 = angle1 / 180 * 3.141592f; |
| 253 |
float rad2 = angle2 / 180 * 3.141592f; |
| 254 |
|
| 255 |
page.SetTextMatrix(1, (float)Math.Tan(rad1), (float)Math.Tan(rad2), 1, |
| 256 |
320, ypos - 120); |
| 257 |
page.ShowText("ABCabc123"); |
| 258 |
page.EndText(); |
| 259 |
|
| 260 |
|
| 261 |
/* |
| 262 |
* scaling text (X direction) |
| 263 |
*/ |
| 264 |
ShowDescription(page, 320, ypos - 175, "Scaling text (X direction)"); |
| 265 |
page.BeginText(); |
| 266 |
page.SetTextMatrix(1.5f, 0, 0, 1, 320, ypos - 175); |
| 267 |
page.ShowText("ABCabc12"); |
| 268 |
page.EndText(); |
| 269 |
|
| 270 |
|
| 271 |
/* |
| 272 |
* scaling text (Y direction) |
| 273 |
*/ |
| 274 |
ShowDescription(page, 320, ypos - 250, "Scaling text (Y direction)"); |
| 275 |
page.BeginText(); |
| 276 |
page.SetTextMatrix(1, 0, 0, 2, 320, ypos - 250); |
| 277 |
page.ShowText("ABCabc123"); |
| 278 |
page.EndText(); |
| 279 |
|
| 280 |
|
| 281 |
/* |
| 282 |
* char spacing, word spacing |
| 283 |
*/ |
| 284 |
|
| 285 |
ShowDescription(page, 60, 140, "char-spacing 0"); |
| 286 |
ShowDescription(page, 60, 100, "char-spacing 1.5"); |
| 287 |
ShowDescription(page, 60, 60, "char-spacing 1.5, word-spacing 2.5"); |
| 288 |
|
| 289 |
page.SetFontAndSize(font, 20); |
| 290 |
page.SetRGBFill(0.1f, 0.3f, 0.1f); |
| 291 |
|
| 292 |
/*char-spacing 0 */ |
| 293 |
page.BeginText(); |
| 294 |
page.TextOut(60, 140, samp_text2); |
| 295 |
page.EndText(); |
| 296 |
|
| 297 |
/*char-spacing 1.5 */ |
| 298 |
page.SetCharSpace(1.5f); |
| 299 |
|
| 300 |
page.BeginText(); |
| 301 |
page.TextOut(60, 100, samp_text2); |
| 302 |
page.EndText(); |
| 303 |
|
| 304 |
/*char-spacing 1.5, word-spacing 3.5 */ |
| 305 |
page.SetWordSpace(2.5f); |
| 306 |
|
| 307 |
page.BeginText(); |
| 308 |
page.TextOut(60, 60, samp_text2); |
| 309 |
page.EndText(); |
| 310 |
|
| 311 |
/* save the document to a file */ |
| 312 |
pdf.SaveToFile("TextDemo.pdf"); |
| 313 |
|
| 314 |
} catch (Exception e) { |
| 315 |
Console.Error.WriteLine(e.Message); |
| 316 |
} |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
|
| 321 |
|