VintaSoft PDF .NET Plug-in Discussions

Questions, comments and suggestions concerning VintaSoft PDF .NET Plug-in.

Board index < VintaSoft Imaging < VintaSoft PDF .NET Plug-in Discussions

We are migrating to new forums engine, no new registration or posting currently available. TIA for your patience.

Hiding Annotations



Hiding Annotations

Post 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

Post 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: https://www.vintasoft.com/docs/vsimaging ... ntent.html

Best regards, Alexander


Re: Hiding Annotations

Post by Alex »

Hello Johannes,

Here is a code snippet that shows how to show/hide annotation in PDF document:
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

Post 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:
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

Post 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:
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


Page 1 from 1: 1