Reputation: 11
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.
Can anyone suggest a better way of rendering the PDF so that I can move rectangles around on it.
Upvotes: 1
Views: 3270
Reputation: 6375
If you want to continue using the WebBrowser Control you can use a transparent form that moves and resizes with the underlying form.
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.
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
In Form2 you use an API call to let you click through Form2 (ripped from VB.net Click through form ).
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.
Upvotes: 2
Reputation: 2876
This solution may work in the following 2 scenarios:
1- If you are generating the PDF file yourself and you are willing to switch over to PDFSharp for generating the file.
2- If you are not generating input files but you are ok with displaying a modified version that contains the rectangles that you want to be displayed.
I use PDFsharp. Is a open source .NET library for processing PDF
http://www.pdfsharp.com/PDFsharp/
Added: Graphics The graphical objects follow the design pattern of the .Net framework. With one set of functions you can draw on a PDF page as well as on a System.Drawing.Graphics object. Your application can render its output in a window, on the printer or in a PDF document.
Lines, polylines, arcs, Bézier splines, canonical splines Rectangles, rounded rectangles, ellipses, polygons, pies, closed splines, paths
PDFsharp is the Open Source library that easily creates PDF documents from any .NET language. The same drawing routines can be used to create PDF documents, draw on the screen, or send output to any printer.
PDFsharp Highlights
Creates PDF documents on the fly from any .Net language
Easy to understand object model to compose documents
One source code for drawing on a PDF page as well as in a window or on the printer
Modify, merge, and split existing PDF files
Images with transparency (color mask, monochrome mask, alpha mask)
Newly designed from scratch and written entirely in C#
PDFsharp Features
Key Features
Creates PDF documents on the fly from any .Net language
Easy to understand object model to compose documents
One source code for drawing on a PDF page as well as in a window or on the printer
Modify, merge, and split existing PDF files
Images with transparency (color mask, monochrome mask, alpha mask)
Newly designed from scratch and written entirely in C#
The graphical classes go well with .Net
Can use either GDI+ or WPF
Upvotes: -2