Stamp annotations need document reload to be shown?

Hi,
Following questions refer to PdfDocumentView control. I checked samples and online resource but could not find answers for following issues.
I managed to create customized stamp annotations with a plain bitmap and without any text by altering Appearance property of  PdfRubberStampAnnotation objects. These customized stamps should be placed by the user dynamically via mouse click on the PDF so I used the PageClicked event. In the click event code the custom stamp annotation should be created and inserted.

There are two strange things with PdfDocumentView when trying to implement this requirement:

1. When changing to PDFViewer.AnnotationMode = PdfDocumentView.PdfViewerAnnotationMode.Stamp nothing happens. I thought that this would enter a similar intuitive way of placing built-in or customized stamps like it works with text or ink annotations. I found no other way than using PageClicked event to allow users adding stamp annotations. Is this really the correct way?

2. In general, custom stamp annotations are added via PageClicked event but I need to close and reopen the LoadedDocument to get the user added stamps shown on screen. Just changing the AnnotationMode to Stamp and adding the stamp won't show anything until I close and reopen the LoadedDocument. How can I avoid reloading the document to get the same behaviour as Acrobat Reader which immediately shows custom and built-in stamps on screen?

3. I need following way to add further stamps after reloading the PDF containing previously added stamps:

PDFViewer.LoadedDocument.Pages[args.PageIndex].Annotations.Page.Annotations.Add(annotation);

I would expect that PDFViewer.LoadedDocument.Pages[args.PageIndex].Annotations.Add(annotation) would be the way to go but this works only once meaning you cannot add further stamp annotations to a PDF already containing stamps. Adding a new stamp won't change the Annotations list - it stays as it was before without any exception or error. But using the Page reference from Annotations list and adding the stamp to its Annotation list as shown in the code line above it seems to work.

private void PDFViewer_PageClicked(object sender, Syncfusion.Windows.PdfViewer.PageClickedEventArgs args)
{
     // get mouse position and convert to PDF position
     System.Windows.Point p = args.Position;
     PdfUnitConvertor uc = new PdfUnitConvertor();
     float x = uc.ConvertFromPixels((float)p.X, PdfGraphicsUnit.Point);
     float y = uc.ConvertFromPixels((float)p.Y, PdfGraphicsUnit.Point);

     // use stamp annotation mode and add customized stamp at mouse position
     PDFViewer.AnnotationMode = PdfDocumentView.PdfViewerAnnotationMode.Stamp;
     PdfRubberStampAnnotation annotation = new PdfRubberStampAnnotation(new System.Drawing.RectangleF(x, y, 32f, 32f));
     annotation.Appearance.Normal.Graphics.DrawImage(new PdfBitmap(_mybmp), new System.Drawing.PointF(0, 0));

     // add stamp annotation but nothing happens on screen
     PDFViewer.LoadedDocument.Pages[args.PageIndex].Annotations.Page.Annotations.Add(annotation);

     // disable stamp annotation mode
     PDFViewer.AnnotationMode = PdfDocumentView.PdfViewerAnnotationMode.None;

     // Save the document with the added stamp annotation to memory stream and reload to finally show the stamp which takes a lot of time...
     MemoryStream ms = new MemoryStream();
     PDFViewer.LoadedDocument.Save(ms);
     ms.Flush();
     ms.Position = 0;
     byte[] rawpdfdata = ms.ToArray();
     ms.Close();
     PDFViewer.LoadedDocument.Close(true);
     MemoryStream msnew = new MemoryStream(rawpdfdata);
     msnew.Position = 0;
     PDFViewer.Load(msnew);
}

Many thanks for your help.
Martin


3 Replies 1 reply marked as answer

DG Deepak Gunasekaran Syncfusion Team June 17, 2020 01:11 PM UTC

Hi Martin, 
 
At present, we could not add custom stamps at the specified page and a location using PdfDocumentView control without the reloading the document or without using the code snippet you have shared. However this can be simply achieved with `PdfViewerControl` using the API ToolbarSettings.StampAnnotations.  
Please follow the below steps to add the custom stamps in the pages using mouse down. 
Step 1: Set the PdfViewerControl’s annotation mode to Stamp. 
PdfViewerControl.AnnotationMode = PdfDocumentView.PdfViewerAnnotationMode.Stamp; 
 
 
Step 2: Create an image object from the local disk and add it to the StampAnnotation collection. The recently added stamp in the collection will be ready for the inclusion.  
  
System.Windows.Controls.Image image = new System.Windows.Controls.Image();   
  
image.Source = new BitmapImage(new Uri(@"..\..\Data\AdventureCycle.jpg", UriKind.RelativeOrAbsolute));   
  
pdfviewer.ToolbarSettings.StampAnnotations.Add(new Syncfusion.Windows.PdfViewer.PdfStampAnnotation(image));   
  
 
Step 3: Mouse down on the required page. This will include the stamp at the required page and at the required location. 
 
We have already published a custom toolbar sample for including the custom stamps, using which you can achieve the same. The sample can be downloaded from the below location.  
We have created a video that will demonstrate adding custom stamp from the local disk using the custom toolbar sample and it can be downloaded from the below location. 
 
Please try this and let us know whether it meets your requirement. 
 
Regards, 
Deepak G 


Marked as answer

MP Martin Pflueglmayer June 19, 2020 12:47 PM UTC

Hi Deepak,
Many thanks for your reply. In my case I use PdfDocumentView in a self-created WPF user control and just need a very small part of the whole functionality of the PdfViewerControl. However, I will follow your suggestions and check it out.
best regards,
Martin 


US Uthandaraja Selva Sundara Kani Syncfusion Team June 22, 2020 07:28 AM UTC

Hi Martin, 
 
Thanks for your update.  
 
As you mentioned, please try the provided suggestion and let us know if you need further assistance. As always, we will be happy to assist you. 
 
Regards, 
Uthandaraja S 


Loader.
Up arrow icon