Hiding Annotations

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

Moderator: Alex

Post Reply
sw_developer
Posts: 2
Joined: Tue May 03, 2016 10:41 am

Hiding Annotations

Post by sw_developer » Tue May 03, 2016 10:52 am

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

Alex
Site Admin
Posts: 1464
Joined: Thu Jul 10, 2008 2:21 pm

Re: Hiding Annotations

Post by Alex » Tue May 03, 2016 4:41 pm

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

Alex
Site Admin
Posts: 1464
Joined: Thu Jul 10, 2008 2:21 pm

Re: Hiding Annotations

Post by Alex » Wed May 04, 2016 9:22 am

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

sw_developer
Posts: 2
Joined: Tue May 03, 2016 10:41 am

Re: Hiding Annotations

Post by sw_developer » Wed May 04, 2016 11:03 am

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

Alex
Site Admin
Posts: 1464
Joined: Thu Jul 10, 2008 2:21 pm

Re: Hiding Annotations

Post by Alex » Wed May 04, 2016 2:06 pm

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

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests