Drawing shapes or annotations in Codebehind

Hello,

I have the following problem:

I am hosting a pdfviewer control in a WinForms form

I would like to draw shapes (rectangle, ellipse, polygon) in the codebehind in a PDF document and be able to zoom in/out and move them in the document by mouse click and hold, like an annotation.

You must be able to rotate (rotate) the drawn shape in increments of 1 degree.

I've tried the following codes and I can't find any complete possibilities for all of them:

Text:

     Dim graphics As PdfGraphics = page.Graphics

     Dim font As Font = New Font(cFont, sFont_Size, FontStyle.Regular)

     Dim pdfFont As PdfTrueTypeFont = New PdfTrueTypeFont(font, False)

     sY = unitConvertor.ConvertFromPixels(sY / zoomfactor, PdfGraphicsUnit.Point)

     sX = unitConvertor.ConvertFromPixels(sX / zoomfactor,  PdfGraphicsUnit.Point)

     iRotationAngle = Convert.ToDouble(txtRotationAngle.Text)

     If iTurnAngle <> 0 Then

            Dim state As PdfGraphicsState = page.Graphics.Save()

            page.Graphics.TranslateTransform(sX, sY)

            page.Graphics.RotateTransform(iRotateAngle)

            graphics.DrawString(cStamp & " " & Now.ToString,

            pdfFont, colStamp, New PointF(0, 0))

         else

            graphics.DrawString(cStamp, pdfFont, colStamp, New

            PointF(0, 0))

            page.Graphics.Restore(state)

          End If

That works, also the rotation. However, the text cannot be moved later because it cannot be selected with a mouse click


Rectangle:

  Dim state As PdfGraphicsState = page.Graphics.Save()

  Dim brush As PdfSolidBrush = New

  PdfSolidBrush(Color.Green)

     'Set the bounds for rectangles

  Dim bounds As RectangleF = New RectangleF(10, 10, 100, 50)

        page.Graphics.DrawRectangle(brush, bounds)

        page.Graphics.RotateTransform(37)

        page.Graphics.Restore(state)


 Dim stream As MemoryStream = New MemoryStream()

        doc.Save(stream)

        pdfViewer.pdfViewerControl.Load(stream)


The drawing works, but can no longer be moved or resized later, since it cannot be selected with a mouse click. The rotation does not work.


Annotation:

 Dim doc As PdfLoadedDocument = pdfViewer.pdfViewerControl.LoadedDocument

 Dim inkPoints As List(Of Single) = New List(Of Single) From { 40, 300, 60, 100, 40, 50, 40, 300 }

 Dim rectangle As RectangleF = New RectangleF(0, 0, 300, 400)

 Dim inkAnnotation As PdfInkAnnotation = New PdfInkAnnotation(rectangle, inkPoints)

 doc.Pages(0).Annotations.Add(inkAnnotation)

  Dim stream As MemoryStream = New MemoryStream()

  doc.Save(stream)

  pdfViewer.pdfViewerControl.Load(stream)

Drawing works, but I don't get the relationship between the inkPoints list and the rectangle.

Can you show me a simple example of a rectangular annotation rotating a few degrees?


After these attempts, using an annotation seems to be the best way to achieve my goal.

Attached is a picture showing what I'm trying to achieve.

It took me 4 weeks to build a replacement solution.

I have overlaid the pdfviewer with a transparent shape.

I draw my elements on it.

It works quite well. The problem is that the transparent shape doesn't fire mouse events, you have to do everything from the keyboard, and when zooming, you have to do a tedious calculation of the positions of the elements. That's not very satisfying.

I think drawing directly into the PDF document would be a better solution.

I hope you understood me and can help me.

The solution with the annotations would be the most important to me.

Sorry, I wanted sent a pic (bmp or pdf), but it is not possible. Always: File Type is not allowed
Wich File type is allowed to sent?

Best regards

Helmut


4 Replies

KG Krithika Ganesan Syncfusion Team September 12, 2022 04:38 PM UTC

Hi Helmut,  

 

    1. Text added using graphics cannot be moved later because it cannot be selected with a mouse click

Text added using graphics gets flattened with the document. Hence, we can’t perform any interaction (mouse interaction or keyboard) in the drawn graphics.

    1. Draw rectangle using Graphics and rotate using RotateTransform with 37 degrees. rotation doesn't work.but can no longer be moved or resized later, since it cannot be selected with a mouse click

On further analyzing the provided code snippet we found that  Rotate Transform after drawing rectangle to the pdf document and thus the rotation is not properly updated to the pdf document. We must apply rotate transform and draw the rectangle to the pdf document.

 

Please follow the modified code snippet below, to achieve the requirement:

'Creates a New PDF document.

Dim document As New PdfDocument()

 

'Creates a new page

Dim page As PdfPage = document.Pages.Add()

Dim state As PdfGraphicsState = page.Graphics.Save()

Dim brush As PdfSolidBrush = New PdfSolidBrush(Color.Green)

'Set the bounds for rectangles

Dim bounds As RectangleF = New RectangleF(10, 10, 100, 50)

 

 

page.Graphics.RotateTransform(37)

page.Graphics.DrawRectangle(brush, bounds)

page.Graphics.Restore(state)

 

Dim stream As MemoryStream = New MemoryStream()

document.Save(stream)

 

    1. Adding ink annotation to the loaded page. Customer don't get the relationship between ink points list and the rectangle

The Ink Annotation will be drawn based on the ink points added to it and the changes in the Rectangle will not affect the ink annotation in any cause. We will take steps to update this in our documentation.

    1. Simple example of a rectangular annotation rotating a few degrees

We are checking on this and we will update further details on 13th September 2022.

 

Regards,

Krithika



HE Helmut September 13, 2022 09:55 AM UTC

Many Thanks Krithika,

In the meantime I came up with the wrong order in the lines of code for rotating the rectangle.

I was about to write it, but your answer was already here.

Thank you very much once more.

regards
Helmut




KG Krithika Ganesan Syncfusion Team September 13, 2022 03:14 PM UTC

Hi Helmut,  

 

Sorry for the inconvenience.

 

4.Simple example of a rectangular annotation rotating a few degrees

As per the PDF document specification, it is not possible to add a rotated annotation or support to rotate annotation with in the page. So it is not possible to rotate annotation using PdfViewer. If your requirement is to rotated shapes then it should be achieved by graphics. But it will not have user interaction as it will get flatten.

 

Please let us known if you have any concern and if you want any assist.

  

Regards,

Krithika



HE Helmut September 26, 2022 03:16 PM UTC

Hi Krithika,

many thanks for your efforts. In the meantime I've found an other way sto solve my problem.

Thank you very much once more.

regards
Helmut


Loader.
Up arrow icon