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