| 1 |
/* |
| 2 |
* << Haru Free PDF Library 2.0.5 >> -- ImageDemo.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 ImageDemo { |
| 21 |
private static void ShowDescription(HPdfPage page, float x, float y, |
| 22 |
string text) { |
| 23 |
string buf; |
| 24 |
|
| 25 |
page.MoveTo(x, y - 10); |
| 26 |
page.LineTo(x, y + 10); |
| 27 |
page.MoveTo(x - 10, y); |
| 28 |
page.LineTo(x + 10, y); |
| 29 |
page.Stroke(); |
| 30 |
|
| 31 |
page.SetFontAndSize(page.GetCurrentFont(), 8); |
| 32 |
page.SetRGBFill(0, 0, 0); |
| 33 |
|
| 34 |
page.BeginText(); |
| 35 |
|
| 36 |
buf = "(x=" + x + ",y=" + y + ")"; |
| 37 |
page.MoveTextPos(x - page.TextWidth(buf) - 5, |
| 38 |
y - 10); |
| 39 |
page.ShowText(buf); |
| 40 |
page.EndText(); |
| 41 |
|
| 42 |
page.BeginText(); |
| 43 |
page.MoveTextPos(x - 20, y - 25); |
| 44 |
page.ShowText(text); |
| 45 |
page.EndText(); |
| 46 |
} |
| 47 |
|
| 48 |
public static void Main() { |
| 49 |
Console.WriteLine("libhpdf-" + HPdfDoc.HPdfGetVersion()); |
| 50 |
|
| 51 |
try { |
| 52 |
HPdfDoc pdf = new HPdfDoc(); |
| 53 |
|
| 54 |
pdf.SetCompressionMode(HPdfDoc.HPDF_COMP_ALL); |
| 55 |
|
| 56 |
/* create default-font */ |
| 57 |
HPdfFont font = pdf.GetFont("Helvetica", null); |
| 58 |
|
| 59 |
/* add a new page object. */ |
| 60 |
HPdfPage page = pdf.AddPage(); |
| 61 |
|
| 62 |
page.SetWidth(550); |
| 63 |
page.SetHeight(500); |
| 64 |
|
| 65 |
HPdfDestination dst = page.CreateDestination(); |
| 66 |
dst.SetXYZ(0, page.GetHeight(), 1); |
| 67 |
pdf.SetOpenAction(dst); |
| 68 |
|
| 69 |
page.BeginText(); |
| 70 |
page.SetFontAndSize(font, 20); |
| 71 |
page.MoveTextPos(220, page.GetHeight() - 70); |
| 72 |
page.ShowText("ImageDemo"); |
| 73 |
page.EndText(); |
| 74 |
|
| 75 |
/* load image file. */ |
| 76 |
HPdfImage image = pdf.LoadPngImageFromFile("demo\\pngsuite\\basn3p02.png"); |
| 77 |
|
| 78 |
/* image1 is masked by image2. */ |
| 79 |
HPdfImage image1 = pdf.LoadPngImageFromFile("demo\\pngsuite\\basn3p02.png"); |
| 80 |
|
| 81 |
/* image2 is a mask image. */ |
| 82 |
HPdfImage image2 = pdf.LoadPngImageFromFile("demo\\pngsuite\\basn0g01.png"); |
| 83 |
|
| 84 |
/* image3 is a RGB-color image. we use this image for color-mask |
| 85 |
* demo. |
| 86 |
*/ |
| 87 |
HPdfImage image3 = pdf.LoadPngImageFromFile("demo\\pngsuite\\maskimage.png"); |
| 88 |
|
| 89 |
float iw = image.GetWidth(); |
| 90 |
float ih = image.GetHeight(); |
| 91 |
|
| 92 |
page.SetLineWidth(0.5f); |
| 93 |
|
| 94 |
float x = 100; |
| 95 |
float y = page.GetHeight() - 150; |
| 96 |
|
| 97 |
/* Draw image to the canvas. (normal-mode with actual size.)*/ |
| 98 |
page.DrawImage(image, x, y, iw, ih); |
| 99 |
|
| 100 |
ShowDescription(page, x, y, "Actual Size"); |
| 101 |
|
| 102 |
x += 150; |
| 103 |
|
| 104 |
/* Scalling image (X direction) */ |
| 105 |
page.DrawImage(image, x, y, iw * 1.5f, ih); |
| 106 |
|
| 107 |
ShowDescription(page, x, y, "Scalling image (X direction)"); |
| 108 |
|
| 109 |
x += 150; |
| 110 |
|
| 111 |
/* Scalling image (Y direction). */ |
| 112 |
page.DrawImage(image, x, y, iw, ih * 1.5f); |
| 113 |
ShowDescription(page, x, y, "Scalling image (Y direction)"); |
| 114 |
|
| 115 |
x = 100; |
| 116 |
y -= 120; |
| 117 |
|
| 118 |
/* Skewing image. */ |
| 119 |
float angle1 = 10; |
| 120 |
float angle2 = 20; |
| 121 |
float rad1 = angle1 / 180 * 3.141592f; |
| 122 |
float rad2 = angle2 / 180 * 3.141592f; |
| 123 |
|
| 124 |
page.GSave(); |
| 125 |
|
| 126 |
page.Concat(iw, (float)Math.Tan(rad1) * iw, (float)Math.Tan(rad2) * ih, |
| 127 |
ih, x, y); |
| 128 |
|
| 129 |
page.ExecuteXObject(image); |
| 130 |
page.GRestore(); |
| 131 |
|
| 132 |
ShowDescription(page, x, y, "Skewing image"); |
| 133 |
|
| 134 |
x += 150; |
| 135 |
|
| 136 |
/* Rotating image */ |
| 137 |
float angle = 30; /* rotation of 30 degrees. */ |
| 138 |
float rad = angle / 180 * 3.141592f; /* Calcurate the radian value. */ |
| 139 |
|
| 140 |
page.GSave(); |
| 141 |
|
| 142 |
page.Concat((float)(iw * Math.Cos(rad)), |
| 143 |
(float)(iw * Math.Sin(rad)), |
| 144 |
(float)(ih * -Math.Sin(rad)), |
| 145 |
(float)(ih * Math.Cos(rad)), |
| 146 |
x, y); |
| 147 |
|
| 148 |
page.ExecuteXObject(image); |
| 149 |
page.GRestore(); |
| 150 |
|
| 151 |
ShowDescription(page, x, y, "Rotating image"); |
| 152 |
|
| 153 |
x += 150; |
| 154 |
|
| 155 |
/* draw masked image. */ |
| 156 |
|
| 157 |
/* Set image2 to the mask image of image1 */ |
| 158 |
image1.SetMaskImage (image2); |
| 159 |
|
| 160 |
page.SetRGBFill(0, 0, 0); |
| 161 |
page.BeginText(); |
| 162 |
page.MoveTextPos(x - 6, y + 14); |
| 163 |
page.ShowText("MASKMASK"); |
| 164 |
page.EndText(); |
| 165 |
|
| 166 |
page.DrawImage(image1, x - 3, y - 3, iw + 6, ih + 6); |
| 167 |
|
| 168 |
ShowDescription(page, x, y, "masked image"); |
| 169 |
|
| 170 |
x = 100; |
| 171 |
y -= 120; |
| 172 |
|
| 173 |
/* color mask. */ |
| 174 |
page.SetRGBFill(0, 0, 0); |
| 175 |
page.BeginText(); |
| 176 |
page.MoveTextPos(x - 6, y + 14); |
| 177 |
page.ShowText("MASKMASK"); |
| 178 |
page.EndText(); |
| 179 |
|
| 180 |
image3.SetColorMask (0, 255, 0, 0, 0, 255); |
| 181 |
page.DrawImage(image3, x, y, iw, ih); |
| 182 |
|
| 183 |
ShowDescription(page, x, y, "Color Mask"); |
| 184 |
|
| 185 |
/* save the document to a file */ |
| 186 |
pdf.SaveToFile("ImageDemo.pdf"); |
| 187 |
|
| 188 |
} catch (Exception e) { |
| 189 |
Console.Error.WriteLine(e.Message); |
| 190 |
} |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
|