Page 1 of 1
Hiding Annotations
Posted: Tue May 03, 2016 10:52 am
by sw_developer
Hello.
We are using the Vintasoft.Imaging.Wpf.UI.WpfImageViewer from the Imaging .NET SDK (Standard, WPF, Reader) to display PDF Documents (without the Annotation .NET Plug-in). The viewer shows also all PDF annotations.
A new requirement is to toggle the visiblity of the annotations. The user should be able to hide and show the annotations.
How can we temporarily hide the annotations without modifying the PDF document itself?
Thanks in advance!
Kind regards,
Johannes
Re: Hiding Annotations
Posted: Tue May 03, 2016 4:41 pm
by Alex
Hello Johannes,
You need use the PDF content renderer if you want to control the rendering process of PDF page. Please read more info here:
http://www.vintasoft.com/docs/vsimaging ... ntent.html
Best regards, Alexander
Re: Hiding Annotations
Posted: Wed May 04, 2016 9:22 am
by Alex
Hello Johannes,
Here is a code snippet that shows how to show/hide annotation in PDF document:
Code: Select all
public void SetPdfAnnotationVisibility(WpfImageViewer viewer, int imageIndex, int annotationIndex, bool isVisible)
{
VintasoftImage image = imageViewer.Images[imageIndex];
PdfPage page = PdfDocumentController.GetPageAssociatedWithImage(image);
if (page != null)
{
if (page.Annotations != null)
{
PdfAnnotation annotation = page.Annotations[annotationIndex];
if (!isVisible)
annotation.Flags |= PdfAnnotationFlags.Hidden;
else
annotation.Flags &= PdfAnnotationFlags.Hidden ^ (PdfAnnotationFlags)int.MaxValue;
if (imageViewer.Image == image)
imageViewer.ReloadImage();
else
image.Reload(true);
}
}
}
IMPORTANT: Code above changes PDF document! Do not save PDF document if this is not necessary.
Best regards, Alexander
Re: Hiding Annotations
Posted: Wed May 04, 2016 11:03 am
by sw_developer
Hello Alex,
before I saw your second answer I had written a custom renderer which prevents to call
DrawFormXObject() of the base renderer if annotations should be hidden:
Code: Select all
public class CustomRenderer : PdfContentRenderer
{
public override void DrawFormXObject(PdfContentRenderingContext context, PdfFormXObjectResource formResource)
{
if (ShowAnnotations)
{
base.DrawFormXObject(context, formResource);
}
}
...
}
This seems to work with our test files and with a call to
Vintasoft.Imaging.VintasoftImage.Reload(true) I'm able to switch the visibility "at runtime".
But I'm not sure if there can be other XObjects in PDF documents that are not annotations. Theses XObjects should be shown even if the annotations are hidden. Can I filter for a certain ResourceType? What is your opinion?
Nevertheless I'll try your second solution. Thank you!
Kind regards, Johannes
Re: Hiding Annotations
Posted: Wed May 04, 2016 2:06 pm
by Alex
But I'm not sure if there can be other XObjects in PDF documents that are not annotations.
Yes, not all XObjects represent annotation appearances and you need to hide only XObjects associated with annotation appearances.
Here is a code snippet that allows to get dictionary with annotation appearances for specified PDF page:
Code: Select all
public Dictionary<PdfFormXObjectResource, PdfAnnotation> GetAnnotationToAppearanceFormTable(PdfPage page)
{
Dictionary<PdfFormXObjectResource, PdfAnnotation> result = new Dictionary<PdfFormXObjectResource, PdfAnnotation>();
if (page.Annotations != null)
{
foreach (PdfAnnotation annotation in page.Annotations)
{
if (annotation.Appearances != null)
{
PdfFormXObjectResource[] appearanceForms = annotation.Appearances.GetAllAppearances();
foreach (PdfFormXObjectResource appearanceForm in appearanceForms)
result[appearanceForm] = annotation;
}
}
}
return result;
}
Best regards, Alexander