如何在PDF页面上绘制矩形? [英] How can I draw a rectangle over a PDF page?
问题描述
我想在Winforms控件中呈现PDF页面,然后在PDF上四处移动矩形以标识用户选择的文本字符串.我正在尝试使用WebBrowser控件渲染PDF,但WebBrowser似乎不支持GDI.
I want to render a PDF page in a control in winforms and then move rectangles around over the PDF to identify user selected text strings. I'm trying to render the PDF using a WebBrowser control but WebBrowser doesn't seem to support GDI.
谁能建议一种更好的渲染PDF的方法,以便我可以在其上移动矩形.
Can anyone suggest a better way of rendering the PDF so that I can move rectangles around on it.
推荐答案
如果要继续使用WebBrowser控件,可以使用透明的表单,该表单可以随基础表单移动并调整其大小.
If you want to continue using the WebBrowser Control you can use a transparent form that moves and resizes with the underlying form.
创建主窗体Form1并向其中添加一个Webbrowser控件.对于此示例,将.Dock设置为All. 添加第二个窗体Form2,上面没有任何内容.
Create your mainform Form1 and add a Webbrowsercontrol to it. For this example set .Dock to All. Add a second form, Form2 with nothing on it.
在Form1中,您将显示Form2,并在Form移动或调整大小时将其移动.
In Form1 you show Form2 and move it if the Form moves or resizes.
Public Class Form1
Private Sub MoveForm2()
Dim crpos As Point = Me.PointToClient(Me.DesktopLocation)
With Form2
.DesktopLocation = New Point(Me.DesktopLocation.X - crpos.X, Me.DesktopLocation.Y - crpos.Y)
.WindowState = Me.WindowState
.Size = Me.ClientSize
End With
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
WebBrowser1.Navigate("www.google.com")
MoveForm2()
Form2.Show(Me)
End Sub
Private Sub Form1_Resize(sender As Object, e As EventArgs) Handles MyBase.Resize
MoveForm2()
End Sub
Private Sub Form1_Move(sender As Object, e As EventArgs) Handles MyBase.Move
MoveForm2()
End Sub
Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles MyBase.Shown
MoveForm2()
End Sub
End Class
在Form2中,您可以使用API调用来单击Form2(从 VB.net翻录点击表单).
In Form2 you use an API call to let you click through Form2 (ripped from VB.net Click through form ).
在这里,您还可以直接绘制到表单上.使用TransparencyKey和BackColor使其透明.
Here you also draw directly onto the form. Use TransparencyKey and BackColor to make it transparent.
Imports System.Runtime.InteropServices
Public Class Form2
<DllImport("user32.dll", EntryPoint:="GetWindowLong")> Public Shared Function GetWindowLong(ByVal hWnd As IntPtr, ByVal nIndex As Integer) As Integer
End Function
<DllImport("user32.dll", EntryPoint:="SetWindowLong")> Public Shared Function SetWindowLong(ByVal hWnd As IntPtr, ByVal nIndex As Integer, ByVal dwNewLong As Integer) As Integer
End Function
Private Sub Form2_Paint(sender As Object, e As PaintEventArgs) Handles MyBase.Paint
'Draw rectangles here
Using g As Graphics = Me.CreateGraphics
g.DrawRectangle(Pens.Red, 100, 100, 100, 100)
End Using
End Sub
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.BackColor = Color.Pink
Me.TransparencyKey = Color.Pink
Me.FormBorderStyle = Windows.Forms.FormBorderStyle.None
Dim InitialStyle As Integer
InitialStyle = GetWindowLong(Me.Handle, -20)
SetWindowLong(Me.Handle, -20, InitialStyle Or &H80000 Or &H20) 'Makes the window "click-throughable"
End Sub
End Class
这当然是一个相当肮脏的技巧,但是如果您只想自己移动矩形,它应该可以很好地工作.当然,您必须使这个示例适应您的需求.
This is a rather dirty hack of course but if you just want to move the rectangles yourself it should work quite well. You have to adapt this example to your needs of course.
这篇关于如何在PDF页面上绘制矩形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!