/
Program.vb
50 lines (44 loc) · 2.19 KB
/
Program.vb
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
Imports System.Drawing
Imports DevExpress.Pdf
Namespace ReplaceFormFieldWithImage
Friend Class Program
Shared Sub Main(ByVal args As String())
Using processor As PdfDocumentProcessor = New PdfDocumentProcessor()
' Load a PDF document with AcroForm data.
processor.LoadDocument("..\..\InteractiveForm.pdf")
For Each field As PdfInteractiveFormField In processor.Document.AcroForm.Fields
If Equals(field.Name, "Address") Then
Dim cropBox As PdfRectangle = field.Widget.Page.CropBox
Dim rect As PdfRectangle = field.Widget.Rect
Dim x As Double = rect.Left - cropBox.Left
Dim y As Double = cropBox.Top - rect.Bottom
' Create graphics and draw an image.
Using graphics As PdfGraphics = processor.CreateGraphics()
DrawImage(graphics, rect, x, y)
graphics.AddToPageForeground(processor.Document.Pages(0), 72, 72)
End Using
End If
Next
processor.RemoveFormField("Address")
processor.SaveDocument("..\..\Result.pdf")
End Using
End Sub
Private Shared Sub DrawImage(ByVal graphics As PdfGraphics, ByVal rect As PdfRectangle, ByVal x As Double, ByVal y As Double)
Dim image As Bitmap = New Bitmap("..\..\AddressFormField.png")
Dim aspectRatio As Double = image.Width \ image.Height
Dim scaleX As Double = image.Width / rect.Width
Dim scaleY As Double = image.Height / rect.Height
Dim width As Double
Dim height As Double
If scaleX < scaleY Then
width = rect.Width
height = width / aspectRatio
Else
height = rect.Height
width = height * aspectRatio
End If
Dim imageRect As RectangleF = New RectangleF(CSng(x), CSng(y - height), CSng(width), CSng(height))
graphics.DrawImage(image, imageRect)
End Sub
End Class
End Namespace