GroupDocsGists
10/19/2017 - 11:30 AM

DataStorageClass.cs

// For complete examples and data files, please go to https://github.com/groupdocsannotation/GroupDocs_Annotation_NET
public class DataStorage
{
    // initialize file path
    private const string filePath = "sample.pdf";

    /// <summary>
    /// Picks the annotations from data storage and exports them to sample.pdf
    /// </summary>
    public static void ExportAnnotationInFile()
    {
        try
        {
            // Create instance of annotator.
            IRepositoryPathFinder pathFinder = new RepositoryPathFinder();
            IAnnotator annotator = new Annotator(
                new UserRepository(pathFinder),
                new DocumentRepository(pathFinder),
                new AnnotationRepository(pathFinder),
                new AnnotationReplyRepository(pathFinder),
                new AnnotationCollaboratorRepository(pathFinder));


            var documentRepository = new DocumentRepository(pathFinder);
            
            // Get file stream
            Stream manifestResourceStream = new FileStream(CommonUtilities.MapSourceFilePath(filePath), FileMode.Open, FileAccess.ReadWrite);
            List<AnnotationInfo> annotations = new List<AnnotationInfo>();
            
            // Get document
            var document = documentRepository.GetDocument("Document.pdf");

            Stream stream = annotator.ExportAnnotationsToDocument(document.Id, manifestResourceStream, DocumentType.Pdf);

            // Save result stream to file.
            using (FileStream fileStream = new FileStream(CommonUtilities.MapDestinationFilePath("Annotated.pdf"), FileMode.Create))
            {
                byte[] buffer = new byte[stream.Length];
                stream.Seek(0, SeekOrigin.Begin);
                stream.Read(buffer, 0, buffer.Length);
                fileStream.Write(buffer, 0, buffer.Length);
                fileStream.Close();
            }
        }
        catch (Exception exp)
        {
            Console.WriteLine(exp.Message);
        }
    }

    /// <summary>
    /// Creates a document data object in the storage
    /// </summary>
    public static void CreateDocument()
    {
        try
        {
            IRepositoryPathFinder pathFinder = new RepositoryPathFinder();
            var documentRepository = new DocumentRepository(pathFinder);

            // Create instance of annotator
            IAnnotator annotator = new Annotator(
                new UserRepository(pathFinder),
                new DocumentRepository(pathFinder),
                new AnnotationRepository(pathFinder),
                new AnnotationReplyRepository(pathFinder),
                new AnnotationCollaboratorRepository(pathFinder));

            // Create document data object in storage.
            var document = documentRepository.GetDocument("Document.pdf");
            long documentId = document != null ? document.Id : annotator.CreateDocument("Document.pdf");

            Console.WriteLine("Document ID : " + documentId);

        }
        catch (Exception exp)
        {
            Console.WriteLine(exp.Message);
        }
    }

    /// <summary>
    /// Assigns/sets document access rights
    /// </summary>
    public static void AssignAccessRights()
    {
        try
        {
            // Creat path finder 
            IRepositoryPathFinder pathFinder = new RepositoryPathFinder();
            var documentRepository = new DocumentRepository(pathFinder);

            // Create instance of annotator
            IAnnotator annotator = new Annotator(
                new UserRepository(pathFinder),
                new DocumentRepository(pathFinder),
                new AnnotationRepository(pathFinder),
                new AnnotationReplyRepository(pathFinder),
                new AnnotationCollaboratorRepository(pathFinder));


            // Create document data object in storage.
            var document = documentRepository.GetDocument("Document.pdf");
            long documentId = document != null ? document.Id : annotator.CreateDocument("Document.pdf");

            // Set document access rights    
            annotator.SetDocumentAccessRights(documentId, AnnotationReviewerRights.All);
        }
        catch (Exception exp)
        {
            Console.WriteLine(exp.Message);
        }
    }

    /// <summary>
    /// Maps annotations and creates dcocument data object in the storage 
    /// </summary>
    public static void CreateAndGetAnnotation()
    {
        try
        {
            // Create path finder 
            IRepositoryPathFinder pathFinder = new RepositoryPathFinder();
            var documentRepository = new DocumentRepository(pathFinder);

            // Create instance of annotator
            IAnnotator annotator = new Annotator(
                new UserRepository(pathFinder),
                new DocumentRepository(pathFinder),
                new AnnotationRepository(pathFinder),
                new AnnotationReplyRepository(pathFinder),
                new AnnotationCollaboratorRepository(pathFinder));

            // Create document data object in storage.
            var document = documentRepository.GetDocument("Document.pdf");
            long documentId = document != null ? document.Id : annotator.CreateDocument("Document.pdf");

            // Create annotation object
            AnnotationInfo pointAnnotation = new AnnotationInfo
            {
                AnnotationPosition = new Point(852.0, 81.0),
                Box = new Rectangle(212f, 81f, 142f, 0.0f),
                Type = AnnotationType.Point,
                PageNumber = 0,
                CreatorName = "Anonym",
                DocumentGuid = documentId
            };

            // Add annotation to storage
            CreateAnnotationResult createPointAnnotationResult = annotator.CreateAnnotation(pointAnnotation);

            //=============================================================================
            // Create annotation object
            AnnotationInfo textFieldAnnotation = new AnnotationInfo
            {
                AnnotationPosition = new Point(852.0, 201.0),
                FieldText = "text in the box",
                FontFamily = "Arial",
                FontSize = 10,
                Box = new Rectangle(66f, 201f, 64f, 37f),
                PageNumber = 0,
                Type = AnnotationType.TextField,
                CreatorName = "Anonym",
                DocumentGuid = documentId
            };

            //Add annotation to storage
            CreateAnnotationResult createTextFieldAnnotationResult = annotator.CreateAnnotation(textFieldAnnotation);

            // Get annotation from storage
            GetAnnotationResult result = annotator.GetAnnotation(createPointAnnotationResult.Guid);
        }
        catch (Exception exp)
        {
            Console.WriteLine(exp.Message);
        }
    }

    /// <summary>
    /// Gets annotations from the storage file
    /// </summary>
    /// <returns>Returns a list of annotations</returns>
    public static ListAnnotationsResult GetAllDocumentAnnotation()
    {
        try
        {
            // Create path finder 
            IRepositoryPathFinder pathFinder = new RepositoryPathFinder();
            var documentRepository = new DocumentRepository(pathFinder);

            // Create instance of annotator
            IAnnotator annotator = new Annotator(
                new UserRepository(pathFinder),
                new DocumentRepository(pathFinder),
                new AnnotationRepository(pathFinder),
                new AnnotationReplyRepository(pathFinder),
                new AnnotationCollaboratorRepository(pathFinder));

            // Create document data object in storage.
            var document = documentRepository.GetDocument("Document.pdf");
            long documentId = document != null ? document.Id : annotator.CreateDocument("Document.pdf");

            // Get annotation from storage
            ListAnnotationsResult result = annotator.GetAnnotations(documentId);

            return result;
        }
        catch (Exception exp)
        {
            Console.WriteLine(exp.Message);
            return null;
        }
    }

    /// <summary>
    /// Resizes the existing annotations
    /// </summary>
    public static void ResizeAnnotationResult()
    {
        try
        {
            // Create path finder 
            IRepositoryPathFinder pathFinder = new RepositoryPathFinder();
            var documentRepository = new DocumentRepository(pathFinder);

            // Create instance of annotator
            IAnnotator annotator = new Annotator(
                new UserRepository(pathFinder),
                new DocumentRepository(pathFinder),
                new AnnotationRepository(pathFinder),
                new AnnotationReplyRepository(pathFinder),
                new AnnotationCollaboratorRepository(pathFinder));

            // Create document data object in storage.
            var document = documentRepository.GetDocument("Document.pdf");
            long documentId = document != null ? document.Id : annotator.CreateDocument("Document.pdf");

            // Create annotation object
            AnnotationInfo areaAnnotation = new AnnotationInfo
            {
                AnnotationPosition = new Point(852.0, 271.7),
                BackgroundColor = 3355443,
                Box = new Rectangle(466f, 271f, 69f, 62f),
                PageNumber = 0,
                PenColor = 3355443,
                Type = AnnotationType.Area,
                CreatorName = "Anonym",
                DocumentGuid = documentId
            };

            //Add annotation to storage
            CreateAnnotationResult createAreaAnnotationResult = annotator.CreateAnnotation(areaAnnotation);

            //Resize annotation
            ResizeAnnotationResult resizeResult = annotator.ResizeAnnotation(createAreaAnnotationResult.Id, new AnnotationSizeInfo
            {
                Height = 80,
                Width = 60
            });
        }
        catch (Exception exp)
        {
            Console.WriteLine(exp.Message);
        }
    }

    /// <summary>
    /// Moves annotation marker
    /// </summary>
    public static void MoveAnnotationResult()
    {
        try
        {
            // Create path finder 
            IRepositoryPathFinder pathFinder = new RepositoryPathFinder();
            var documentRepository = new DocumentRepository(pathFinder);

            // Create instance of annotator
            IAnnotator annotator = new Annotator(
                new UserRepository(pathFinder),
                new DocumentRepository(pathFinder),
                new AnnotationRepository(pathFinder),
                new AnnotationReplyRepository(pathFinder),
                new AnnotationCollaboratorRepository(pathFinder));

            // Create document data object in storage.
            var document = documentRepository.GetDocument("Document.pdf");
            long documentId = document != null ? document.Id : annotator.CreateDocument("Document.pdf");

            // Create annotation object
            AnnotationInfo areaAnnotation = new AnnotationInfo
            {
                AnnotationPosition = new Point(852.0, 271.7),
                BackgroundColor = 3355443,
                Box = new Rectangle(466f, 271f, 69f, 62f),
                PageNumber = 0,
                PenColor = 3355443,
                Type = AnnotationType.Area,
                CreatorName = "Anonym",
                DocumentGuid = documentId
            };

            //Add annotation to storage
            CreateAnnotationResult createAreaAnnotationResult = annotator.CreateAnnotation(areaAnnotation);

            //Move annotation marker
            MoveAnnotationResult moveAnnotationResult = annotator.MoveAnnotationMarker(createAreaAnnotationResult.Id,
                                                    new Point(200, 200),/*NewPageNumber*/1);
        }
        catch (Exception exp)
        {
            Console.WriteLine(exp.Message);
        }
    }

    /// <summary>
    /// Sets background color of annotation
    /// </summary>
    public static void SetBackgroundColorResult()
    {
        try
        {
            // Create path finder 
            IRepositoryPathFinder pathFinder = new RepositoryPathFinder();
            var documentRepository = new DocumentRepository(pathFinder);

            // Create instance of annotator
            IAnnotator annotator = new Annotator(
                new UserRepository(pathFinder),
                new DocumentRepository(pathFinder),
                new AnnotationRepository(pathFinder),
                new AnnotationReplyRepository(pathFinder),
                new AnnotationCollaboratorRepository(pathFinder));

            // Create document data object in storage.
            var document = documentRepository.GetDocument("Document.pdf");
            long documentId = document != null ? document.Id : annotator.CreateDocument("Document.pdf");

            // Create annotation object
            AnnotationInfo textFieldAnnotation = new AnnotationInfo
            {
                AnnotationPosition = new Point(852.0, 201.0),
                FieldText = "text in the box",
                FontFamily = "Arial",
                FontSize = 10,
                Box = new Rectangle(66f, 201f, 64f, 37f),
                PageNumber = 0,
                Type = AnnotationType.TextField,
                CreatorName = "Anonym",
                DocumentGuid = documentId
            };

            //Add annotation to storage
            CreateAnnotationResult createTextFieldAnnotationResult = annotator.CreateAnnotation(textFieldAnnotation);

            // Set background color of annotation
            SaveAnnotationTextResult setBackgroundColorResult = annotator.SetAnnotationBackgroundColorcreateTextFieldAnnotationResult.Id, 16711680);
        }
        catch (Exception exp)
        {
            Console.WriteLine(exp.Message);
        }
    }

    /// <summary>
    /// Updates the text in the annotation
    /// </summary>
    public static void EditTextFieldAnnotation()
    {
        try
        {
            // Create path finder 
            IRepositoryPathFinder pathFinder = new RepositoryPathFinder();
            var documentRepository = new DocumentRepository(pathFinder);

            // Create instance of annotator
            IAnnotator annotator = new Annotator(
                new UserRepository(pathFinder),
                new DocumentRepository(pathFinder),
                new AnnotationRepository(pathFinder),
                new AnnotationReplyRepository(pathFinder),
                new AnnotationCollaboratorRepository(pathFinder));

            // Create document data object in storage.
            var document = documentRepository.GetDocument("Document.pdf");
            long documentId = document != null ? document.Id : annotator.CreateDocument("Document.pdf");

            // Create annotation object
            AnnotationInfo textFieldAnnotation = new AnnotationInfo
            {
                AnnotationPosition = new Point(852.0, 201.0),
                FieldText = "text in the box",
                FontFamily = "Arial",
                FontSize = 10,
                Box = new Rectangle(66f, 201f, 64f, 37f),
                PageNumber = 0,
                Type = AnnotationType.TextField,
                CreatorName = "Anonym",
                DocumentGuid = documentId
            };

            //Add annotation to storage
            CreateAnnotationResult createTextFieldAnnotationResult = annotator.CreateAnnotation(textFieldAnnotation);

            // Update text in the annotation
            SaveAnnotationTextResult saveTextFieldResult = annotator.SaveTextField(
                createTextFieldAnnotationResult.Id,
                new TextFieldInfo
                {
                    FieldText = "new text",
                    FontFamily = "Colibri",
                    FontSize = 12
                });


            // Set text field color
            SaveAnnotationTextResult saveTextFieldColorResult = annotator.SetTextFieldColor
                                    (createTextFieldAnnotationResult.Id, 16753920);
        }
        catch (Exception exp)
        {
            Console.WriteLine(exp.Message);
        }
    }

    /// <summary>
    /// Removes annotations
    /// </summary>
    public static void RemoveAnnotation()
    {
        try
        {
            // Create path finder 
            IRepositoryPathFinder pathFinder = new RepositoryPathFinder();
            var documentRepository = new DocumentRepository(pathFinder);

            // Create instance of annotator
            IAnnotator annotator = new Annotator(
                new UserRepository(pathFinder),
                new DocumentRepository(pathFinder),
                new AnnotationRepository(pathFinder),
                new AnnotationReplyRepository(pathFinder),
                new AnnotationCollaboratorRepository(pathFinder));

            // Create document data object in storage.
            var document = documentRepository.GetDocument("Document.pdf");
            long documentId = document != null ? document.Id : annotator.CreateDocument("Document.pdf");

            // Create annotation object
            AnnotationInfo pointAnnotation = new AnnotationInfo
            {
                AnnotationPosition = new Point(852.0, 81.0),
                Box = new Rectangle(212f, 81f, 142f, 0.0f),
                Type = AnnotationType.Point,
                PageNumber = 0,
                CreatorName = "Anonym",
                DocumentGuid = documentId
            };

            // Get all annotations from storage
            ListAnnotationsResult listAnnotationsResult = annotator.GetAnnotations(documentId);

            // Get annotation  
            var annotation = annotator.GetAnnotation(listAnnotationsResult.Annotations[0].Guid);


            // Delete single annotation
            var deleteAnnotationResult = annotator.DeleteAnnotation(annotation.Id);

            //Delete all annotations
            annotator.DeleteAnnotations(documentId);
        }
        catch (Exception exp)
        {
            Console.WriteLine(exp.Message);
        }
    }

    /// <summary>
    /// Adds reply to the annotation, edits reply, creates child reply
    /// </summary>
    public static void AddAnnotationReply()
    {
        try
        {
            IRepositoryPathFinder pathFinder = new RepositoryPathFinder();
            var documentRepository = new DocumentRepository(pathFinder);

            // Create instance of annotator
            IAnnotator annotator = new Annotator(
                new UserRepository(pathFinder),
                documentRepository,
                new AnnotationRepository(pathFinder),
                new AnnotationReplyRepository(pathFinder),
                new AnnotationCollaboratorRepository(pathFinder));

            // Create document data object in storage
            var document = documentRepository.GetDocument("Document.pdf");
            long documentId = document != null ? document.Id : annotator.CreateDocument("Document.pdf");

            // Create annotation object
            AnnotationInfo pointAnnotation = new AnnotationInfo
            {
                AnnotationPosition = new Point(852.0, 81.0),
                Box = new Rectangle(212f, 81f, 142f, 0.0f),
                Type = AnnotationType.Point,
                PageNumber = 0,
                CreatorName = "Anonym",
                DocumentGuid = documentId
            };

            // Add annotation to storage
            var createPointAnnotationResult = annotator.CreateAnnotation(pointAnnotation);

            // Add simple reply to created annotation
            var addSimpleReplyResult = annotator.CreateAnnotationReply(createPointAnnotationResult.Id, "first question");

            // Edit created reply
            var editReplyResult = annotator.EditAnnotationReply(addSimpleReplyResult.ReplyGuid, "changed question");

            // Create child reply. This reply will be linked to previously created reply.
            var addChildReplyResult = annotator.CreateAnnotationReply(createPointAnnotationResult.Id, "answer", addSimpleReplyResult.ReplyGuid);

            // Delete annotation reply by guid
            var deleteReplyResult = annotator.DeleteAnnotationReply(addChildReplyResult.ReplyGuid);

            // Delete all replies from annotation
            annotator.DeleteAnnotationReplies(createPointAnnotationResult.Id);

            // List of replies after deleting all replies
            var listRepliesResultAfterDeleteAll = annotator.ListAnnotationReplies(createPointAnnotationResult.Id);
        }
        catch (Exception exp)
        {
            Console.WriteLine(exp.Message);
        }
    }
}