1Imports HPdf
2
3Module ImageDemo
4    Private Sub ShowDescription(ByRef page As HPdfPage, ByVal x As Single, ByVal y As Single, ByVal text As String)
5        Dim buf As String
6
7        page.MoveTo(x, y - 10)
8        page.LineTo(x, y + 10)
9        page.MoveTo(x - 10, y)
10        page.LineTo(x + 10, y)
11        page.Stroke()
12
13        page.SetFontAndSize(page.GetCurrentFont(), 8)
14        page.SetRGBFill(0, 0, 0)
15
16        page.BeginText()
17
18        buf = "(x=" & x & ",y=" & y & ")"
19        page.MoveTextPos(x - page.TextWidth(buf) - 5, y - 10)
20        page.ShowText(buf)
21        page.EndText()
22
23        page.BeginText()
24        page.MoveTextPos(x - 20, y - 25)
25        page.ShowText(text)
26        page.EndText()
27    End Sub
28
29    Sub Main()
30        Console.WriteLine("libhpdf-" & HPdfDoc.HPdfGetVersion())
31
32        Try
33            Dim pdf As HPdfDoc = New HPdfDoc()
34
35            pdf.SetCompressionMode(HPdfDoc.HPDF_COMP_ALL)
36
37            ' create default-font
38            Dim font As HPdfFont = pdf.GetFont("Helvetica", Nothing)
39
40            ' add a new page object.
41            Dim page As HPdfPage = pdf.AddPage()
42
43            page.SetWidth(550)
44            page.SetHeight(500)
45
46            Dim dst As HPdfDestination = page.CreateDestination()
47            dst.SetXYZ(0, page.GetHeight(), 1)
48            pdf.SetOpenAction(dst)
49
50            page.BeginText()
51            page.SetFontAndSize(font, 20)
52            page.MoveTextPos(220, page.GetHeight() - 70)
53            page.ShowText("ImageDemo")
54            page.EndText()
55
56            ' load image file.
57            Dim image As HPdfImage = pdf.LoadPngImageFromFile("demo\\pngsuite\\basn3p02.png")
58
59            ' image1 is masked by image2.
60            Dim image1 As HPdfImage = pdf.LoadPngImageFromFile("demo\\pngsuite\\basn3p02.png")
61
62            ' image2 is a mask image.
63            Dim image2 As HPdfImage = pdf.LoadPngImageFromFile("demo\\pngsuite\\basn0g01.png")
64
65            ' image3 is a RGB-color image. we use this image for color-mask
66            ' demo.
67            '
68            Dim image3 As HPdfImage = pdf.LoadPngImageFromFile("demo\\pngsuite\\maskimage.png")
69
70            Dim iw As Single = image.GetWidth()
71            Dim ih As Single = image.GetHeight()
72
73            page.SetLineWidth(0.5F)
74
75            Dim x As Single = 100
76            Dim y As Single = page.GetHeight() - 150
77
78            ' Draw image to the canvas. (normal-mode with actual size.)
79            page.DrawImage(image, x, y, iw, ih)
80
81            ShowDescription(page, x, y, "Actual Size")
82
83            x += 150
84
85            ' Scalling image (X direction)
86            page.DrawImage(image, x, y, iw * 1.5F, ih)
87
88            ShowDescription(page, x, y, "Scalling image (X direction)")
89
90            x += 150
91
92            ' Scalling image (Y direction).
93            page.DrawImage(image, x, y, iw, ih * 1.5F)
94            ShowDescription(page, x, y, "Scalling image (Y direction)")
95
96            x = 100
97            y -= 120
98
99            ' Skewing image.
100            Dim angle1 As Single = 10
101            Dim angle2 As Single = 20
102            Dim rad1 As Single = angle1 / 180 * 3.141592F
103            Dim rad2 As Single = angle2 / 180 * 3.141592F
104
105            page.GSave()
106
107            page.Concat(iw, Math.Tan(rad1) * iw, Math.Tan(rad2) * ih, ih, x, y)
108
109            page.ExecuteXObject(image)
110            page.GRestore()
111
112            ShowDescription(page, x, y, "Skewing image")
113
114            x += 150
115
116            ' Rotating image
117            Dim angle As Single = 30                        ' rotation of 30 degrees.
118            Dim rad As Single = angle / 180 * 3.141592F     ' Calcurate the radian value.
119
120            page.GSave()
121
122            page.Concat((iw * Math.Cos(rad)), (iw * Math.Sin(rad)), (ih * -Math.Sin(rad)), (ih * Math.Cos(rad)), x, y)
123
124            page.ExecuteXObject(image)
125            page.GRestore()
126
127            ShowDescription(page, x, y, "Rotating image")
128
129            x += 150
130
131            '  draw masked image.
132
133            '  Set image2 to the mask image of image1
134            image1.SetMaskImage(image2)
135
136            page.SetRGBFill(0, 0, 0)
137            page.BeginText()
138            page.MoveTextPos(x - 6, y + 14)
139            page.ShowText("MASKMASK")
140            page.EndText()
141
142            page.DrawImage(image1, x - 3, y - 3, iw + 6, ih + 6)
143
144            ShowDescription(page, x, y, "masked image")
145
146            x = 100
147            y -= 120
148
149            ' color mask.
150            page.SetRGBFill(0, 0, 0)
151            page.BeginText()
152            page.MoveTextPos(x - 6, y + 14)
153            page.ShowText("MASKMASK")
154            page.EndText()
155
156            image3.SetColorMask(0, 255, 0, 0, 0, 255)
157            page.DrawImage(image3, x, y, iw, ih)
158
159            ShowDescription(page, x, y, "Color Mask")
160
161            ' save the document to a file
162            pdf.SaveToFile("ImageDemo.pdf")
163
164        Catch ex As Exception
165            Console.Error.WriteLine(ex.Message)
166        End Try
167    End Sub
168
169End Module
170
171