/// <summary> /// Import and Export Annotations from Words document. /// </summary> /// Update CommonUtilities.filePath with path to word document files before using this function public static void ImportAndExportAnnotationsFromWords() { try { //ExStart:ImportAndExportAnnotationsFromWords // Create instance of annotator. AnnotationConfig cfg = CommonUtilities.GetConfiguration(); AnnotationImageHandler annotator = new AnnotationImageHandler(cfg); // Get input file stream Stream inputFile = new FileStream(CommonUtilities.MapSourceFilePath(CommonUtilities.filePath), FileMode.Open, FileAccess.ReadWrite); //importing annotations from Words document AnnotationInfo[] annotations = annotator.ImportAnnotations(inputFile, DocumentType.Words); //export imported annotation to another document (just for check) Stream clearDocument = new FileStream(CommonUtilities.MapDestinationFilePath("Clear.docx"), FileMode.Open, FileAccess.ReadWrite); Stream output = annotator.ExportAnnotationsToDocument(clearDocument, annotations.ToList(), DocumentType.Words); // Export annotation and save output file //save results after export using (FileStream fileStream = new FileStream(CommonUtilities.MapDestinationFilePath("AnnotationImportAndExportAnnotated.docx"), FileMode.Create)) { byte[] buffer = new byte[output.Length]; output.Seek(0L, SeekOrigin.Begin); output.Read(buffer, 0, buffer.Length); fileStream.Write(buffer, 0, buffer.Length); fileStream.Close(); } //ExEnd:ImportAndExportAnnotationsFromWords } catch (System.Exception exp) { Console.WriteLine(exp.Message); } }
//ExEnd:MapDestinationFilePath /// <summary> /// Saves output document /// </summary> public static void SaveOutputDocument(Stream inputFile, List <AnnotationInfo> annotations, DocumentType type) { try { //ExStart:SaveOutputDocument // Create instance of annotator. AnnotationConfig cfg = CommonUtilities.GetConfiguration(); AnnotationImageHandler annotator = new AnnotationImageHandler(cfg); IDocumentDataHandler documentRepository = annotator.GetDocumentDataHandler(); if (!Directory.Exists(cfg.StoragePath)) { Directory.CreateDirectory(cfg.StoragePath); } Stream result = annotator.ExportAnnotationsToDocument(inputFile, annotations, type); FileStream getFileStream = inputFile as FileStream; string extensionWithDot = Path.GetExtension(getFileStream.Name); // Save result stream to file. using (FileStream fileStream = new FileStream(MapDestinationFilePath("Annotated" + extensionWithDot), FileMode.Create)) { byte[] buffer = new byte[result.Length]; result.Seek(0, SeekOrigin.Begin); result.Read(buffer, 0, buffer.Length); fileStream.Write(buffer, 0, buffer.Length); fileStream.Close(); } //ExEnd:SaveOutputDocument } catch (System.Exception exp) { Console.WriteLine(exp.Message); } }
/// <summary> /// Adds distance annotation in Images document /// </summary> public static void AddDistanceAnnotation() { try { //ExStart:AddDistanceAnnotation // Get input file stream Stream inputFile = new FileStream(CommonUtilities.MapSourceFilePath(CommonUtilities.filePath), FileMode.Open, FileAccess.ReadWrite); // Initialize list of AnnotationInfo List <AnnotationInfo> annotations = new List <AnnotationInfo>(); // Distance annotation AnnotationInfo distanceAnnotation = new AnnotationInfo { AnnotationPosition = new Point(852.0, 287.0), Box = new Rectangle(248f, 287f, 115f, 25f), PageNumber = 0, PenColor = 1201033, PenStyle = 0, PenWidth = 1, SvgPath = "M248.73201877934272,295.5439436619718 l115.28309859154929,-4.192112676056338", Text = "\r\nAnonym A.: 115px", Type = AnnotationType.Distance, CreatorName = "Anonym A." }; // Add annotation to list annotations.Add(distanceAnnotation); // Export annotation and save output file CommonUtilities.SaveOutputDocument(inputFile, annotations, DocumentType.Images); //ExEnd:AddDistanceAnnotation } catch (System.Exception exp) { Console.WriteLine(exp.Message); } }
/// <summary> /// Shows how to get thumbnails of pages for PDF /// </summary> public static void GetThumbnailsOfPagesForPDF() { try { //ExStart:GetThumnailsOfPagesForPDF // Create instance of annotator. AnnotationConfig cfg = CommonUtilities.GetConfiguration(); AnnotationImageHandler annotator = new AnnotationImageHandler(cfg); // Get input file stream Stream inputFile = new FileStream(CommonUtilities.MapSourceFilePath(CommonUtilities.filePath), FileMode.Open, FileAccess.ReadWrite); List <PageImage> images = annotator.GetPages(inputFile, new ImageOptions { WithoutAnnotations = true }); //Then if we want get thumbnail we call GetThumbnail() method of PageImage item: foreach (PageImage pageImage in images) { Stream stream = pageImage.GetThumbnail(); // do something with stream } // Default image size was 300x180. If need specified image size, you can pass method parameters: // image thumbnails 100x100 foreach (PageImage pageImage in images) { Stream stream = pageImage.GetThumbnail(100, 100); // do something with stream } //ExEnd:GetThumnailsOfPagesForPDF } catch (System.Exception exp) { Console.WriteLine(exp.Message); } }
/// <summary> /// Adds arrow annotation in Images document /// </summary> public static void AddArrowAnnotation() { try { //ExStart:AddArrowAnnotation // Get input file stream Stream inputFile = new FileStream(CommonUtilities.MapSourceFilePath(CommonUtilities.filePath), FileMode.Open, FileAccess.ReadWrite); // Initialize list of AnnotationInfo List <AnnotationInfo> annotations = new List <AnnotationInfo>(); // Arrow annotation AnnotationInfo arrowAnnotation = new AnnotationInfo { AnnotationPosition = new Point(852.0, 252.0), Box = new Rectangle(279.4742f, 252.9241f, 129.9555f, -9.781596f), PageNumber = 0, PenColor = 1201033, PenStyle = 0, PenWidth = 1, SvgPath = "M279.47417840375584,252.92413145539905 L129.9554929577465,-9.781596244131455", Type = AnnotationType.Arrow, CreatorName = "Anonym A." }; // Add annotation to list annotations.Add(arrowAnnotation); // Export annotation and save output file CommonUtilities.SaveOutputDocument(inputFile, annotations, DocumentType.Images); //ExEnd:AddArrowAnnotation } catch (System.Exception exp) { Console.WriteLine(exp.Message); } }
/// <summary> /// Adds watermark annotation in Images document /// </summary> public static void AddWatermarkAnnotation() { try { //ExStart:AddWatermarkAnnotation // Get input file stream Stream inputFile = new FileStream(CommonUtilities.MapSourceFilePath(CommonUtilities.filePath), FileMode.Open, FileAccess.ReadWrite); // Initialize list of AnnotationInfo List <AnnotationInfo> annotations = new List <AnnotationInfo>(); // Watermark annotation AnnotationInfo watermarkAnnotation = new AnnotationInfo { AnnotationPosition = new Point(100.0, 300.0), FieldText = "TEXT STAMP", FontFamily = "Microsoft Sans Serif", FontSize = 10, FontColor = 2222222, Box = new Rectangle(430f, 272f, 66f, 51f), PageNumber = 0, Type = AnnotationType.Watermark, CreatorName = "Anonym A." }; // Add annotation to list annotations.Add(watermarkAnnotation); // Export annotation and save output file CommonUtilities.SaveOutputDocument(inputFile, annotations, DocumentType.Images); //ExEnd:AddWatermarkAnnotation } catch (System.Exception exp) { Console.WriteLine(exp.Message); } }
static void Main(string[] args) { CommonUtilities.StorageFolderPath = "../../../../Data/Samples/"; CommonUtilities.DestinationFolderPath = "../../../../Data/Output/"; CommonUtilities.LicenseFilePath = "E://lic/Groupdocs.Total.lic"; CommonUtilities.filePath = "Annotated.pdf"; /* Apply product license * Uncomment following function if you have product license * */ CommonUtilities.ApplyLicense(); #region Annotation Functions for PDF //CommonUtilities.filePath = "Annotated.pdf"; //////Add text annotation //// PDFAnnotation.AddTextAnnotation(); ////Add text annotation in Pdf ////PDFAnnotation.AddTextAnnotationInCells(); ////Add text annotation in slides ////PDFAnnotation.AddTextAnnotationInSlides(); //////Add area annotation with replies ////PDFAnnotation.AddAreaAnnotationWithReplies(); //////Add point annotation //// PDFAnnotation.AddPointAnnotation(); //////Add text strike out annotation ////PDFAnnotation.AddTextStrikeOutAnnotation(); //////Add polyline annotation ////PDFAnnotation.AddPolylineAnnotation(); //////Add text field annotation //////PDFAnnotation.AddTextFieldAnnotation(); //////Add watermark annotation ////PDFAnnotation.AddWatermarkAnnotation(); //////Add text replacement annotation ////PDFAnnotation.AddTextReplacementAnnotation(); //////Add arrow annotation ////PDFAnnotation.AddArrowAnnotation(); //////Add text redaction annotation //// PDFAnnotation.AddTextRedactionAnnotation(); //////Add underline annotation ////PDFAnnotation.AddUnderLineAnnotation(); //////Add distance annotation ////PDFAnnotation.AddDistanceAnnotation(); //////Add resource redaction annotation //// PDFAnnotation.AddResourceRedactionAnnotation(); //////Remove all annotations //// PDFAnnotation.RemoveAllAnnotationsFromDocument(); #endregion #region Annotation Functions for Words Document format //CommonUtilities.filePath = "Annotated.docx"; //////Add area annotation with replies for Words Document format //// WordAnnotation.AddAreaAnnotationWithRepliesforWords(); //////Add point annotation for Words Document format //// WordAnnotation.AddPointAnnotation(); //////Add text strike out annotation for Words Document format ////WordAnnotation.AddTextStrikeOutAnnotationforWords(); //////Add text field annotation for Words Document format ////WordAnnotation.AddTextFieldAnnotationforWords(); //////Add text replacement annotation for Words Document format ////WordAnnotation.AddTextReplacementAnnotationforWords(); //////Add arrow annotation for Words Document format ////WordAnnotation.AddArrowAnnotationforWords(); //////Add text redaction annotation for Words Document format //// WordAnnotation.AddTextRedactionAnnotationforWords(); //////Add underline annotation for Words Document format ////WordAnnotation.AddUnderLineAnnotationforWords(); //////Add resource redaction annotation for Words Document format //// WordAnnotation.AddResourceRedactionAnnotationforWords(); //////Import and Export Annotations from Words document. //WordAnnotation.ImportAndExportAnnotationsFromWords(); #endregion #region Annotation Functions for Slides CommonUtilities.filePath = "sample.pptx"; ////Add text annotation //SlidesAnnotation.AddTextAnnotation(); ////Add text annotation in slides //SlidesAnnotation.AddTextFieldAnnotation(); ////Add area annotation with replies //SlidesAnnotation.AddAreaAnnotationWithReplies(); ////Add point annotation // SlidesAnnotation.AddPointAnnotation(); ////Add text strike out annotation //SlidesAnnotation.AddTextStrikeOutAnnotation(); ////Add polyline annotation //SlidesAnnotation.AddPolylineAnnotation(); ////Add text field annotation //SlidesAnnotation.AddTextFieldAnnotation(); ////Add watermark annotation //SlidesAnnotation.AddWatermarkAnnotation(); ////Add text replacement annotation //SlidesAnnotation.AddTextReplacementAnnotation(); ////Add arrow annotation //SlidesAnnotation.AddArrowAnnotation(); ////Add text redaction annotation // SlidesAnnotation.AddTextRedactionAnnotation(); ////Add underline annotation //SlidesAnnotation.AddUnderLineAnnotation(); ////Add distance annotation //SlidesAnnotation.AddDistanceAnnotation(); ////Add resource redaction annotation // SlidesAnnotation.AddResourceRedactionAnnotation(); ////Remove all annotations // SlidesAnnotation.RemoveAllAnnotationsFromDocument(); #endregion #region Cells //CommonUtilities.filePath = "Annotated.xlsx"; ////Add text annotation in Cells ////CellsAnnotation.AddTextAnnotationInCells(); #endregion #region DataStorage Functions ////Create document ////DataStorage.CreateDocument(); //////Assign access rights ////DataStorage.AssignAccessRights(); //////Create and get annotation ////DataStorage.CreateAndGetAnnotation(); //////Get all annotation of a document ////DataStorage.GetAllDocumentAnnotation(); //////Resize annotation ////DataStorage.ResizeAnnotationResult(); //////Move an anotation ////DataStorage.MoveAnnotationResult(); //////Set background color ////DataStorage.SetBackgroundColorResult(); //////Edit annotation ////DataStorage.EditTextFieldAnnotation(); //////Remove annotation ////DataStorage.RemoveAnnotation(); //////Add annotation reply ////DataStorage.AddAnnotationReply(); //////Add document collaborator ////DataStorage.AddCollaborator(); //////Get document collaborator ////DataStorage.GetCollaborator(); //////Update document collaborator ////DataStorage.UpdateCollaborator(); //////Delete document collaborator ////DataStorage.DeleteCollaborator(); //////Delete document collaborator ////DataStorage.ManageCollaboratorRights(); //////Export annotation to document ////DataStorage.ExportAnnotationInFile(); //#endregion //#region Other Operations //////Get image representation of the document ////CommonUtilities.GetImageRepresentation("sample.pdf"); //////Get text coordinates in image representation of the document ////CommonUtilities.GetTextCoordinates("sample.pdf"); #endregion Console.ReadKey(); }
/// <summary> /// Manages collaborator rights /// </summary> public static void ManageCollaboratorRights() { try { //ExStart:ManageCollaboratorRights // Create instance of annotator. AnnotationConfig cfg = CommonUtilities.GetConfiguration(); //Create annotation handler AnnotationImageHandler annotator = new AnnotationImageHandler(cfg); IUserDataHandler userRepository = annotator.GetUserDataHandler(); IDocumentDataHandler documentRepository = annotator.GetDocumentDataHandler(); if (!Directory.Exists(cfg.StoragePath)) { Directory.CreateDirectory(cfg.StoragePath); } // Create owner. var johnOwner = userRepository.GetUserByEmail("*****@*****.**"); if (johnOwner == null) { userRepository.Add(new User { FirstName = "John", LastName = "Doe", Email = "*****@*****.**" }); johnOwner = userRepository.GetUserByEmail("*****@*****.**"); } // Create document data object in storage var document = documentRepository.GetDocument("Document.pdf"); long documentId = document != null ? document.Id : annotator.CreateDocument("Document.pdf", DocumentType.Pdf, johnOwner.Id); // Create reviewer. var reviewerInfo = new ReviewerInfo { PrimaryEmail = "*****@*****.**", FirstName = "Judy", LastName = "Doe", // Can only get view annotations AccessRights = AnnotationReviewerRights.CanView }; // Add collaboorator to the document. If user with Email equals to reviewers PrimaryEmail is absent it will be created. var addCollaboratorResult = annotator.AddCollaborator(documentId, reviewerInfo); // Get document collaborators var getCollaboratorsResult = annotator.GetCollaborators(documentId); var judy = userRepository.GetUserByEmail("*****@*****.**"); // 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 A." }; // John try to add annotations. User is owner of the document. var johnResult = annotator.CreateAnnotation(pointAnnotation, documentId, johnOwner.Id); // Judy try to add annotations try { var judyResult = annotator.CreateAnnotation(pointAnnotation, documentId, judy.Id); } //Get exceptions, because user can only view annotations catch (AnnotatorException e) { Console.Write(e.Message); Console.ReadKey(); } // Allow Judy create annotations. reviewerInfo.AccessRights = AnnotationReviewerRights.CanAnnotate; var updateCollaboratorResult = annotator.UpdateCollaborator(documentId, reviewerInfo); // Now user can add annotations var judyResultCanAnnotate = annotator.CreateAnnotation(pointAnnotation, documentId, judy.Id); //ExEnd:ManageCollaboratorRights } catch (System.Exception exp) { Console.WriteLine(exp.Message); } }
/// <summary> /// Deletes document collaborator /// </summary> public static void DeleteCollaborator() { try { //ExStart:DeleteCollaborator // Create instance of annotator. AnnotationConfig cfg = CommonUtilities.GetConfiguration(); //Create annotation handler AnnotationImageHandler annotator = new AnnotationImageHandler(cfg); IUserDataHandler userRepository = annotator.GetUserDataHandler(); IDocumentDataHandler documentRepository = annotator.GetDocumentDataHandler(); if (!Directory.Exists(cfg.StoragePath)) { Directory.CreateDirectory(cfg.StoragePath); } // Create a user that will be an owner. // Get user from the storage var owner = userRepository.GetUserByEmail("*****@*****.**"); // If user doesn’t exist in the storage then create it. if (owner == null) { userRepository.Add(new User { FirstName = "John", LastName = "Doe", Email = "*****@*****.**" }); owner = userRepository.GetUserByEmail("*****@*****.**"); } // Get document data object in the storage var document = documentRepository.GetDocument("Document.pdf"); // If document already created or it hasn’t owner then delete document if (document != null && document.OwnerId != owner.Id) { documentRepository.Remove(document); document = null; } // Get document id if document already created or create new document long documentId = document != null ? document.Id : annotator.CreateDocument("Document.pdf", DocumentType.Pdf, owner.Id); // Create reviewer. var reviewerInfo = new ReviewerInfo { PrimaryEmail = "*****@*****.**", //user email, unique identifier FirstName = "Judy", LastName = "Doe", AccessRights = AnnotationReviewerRights.All }; // Delete collaborator var deleteCollaboratorResult = annotator.DeleteCollaborator(documentId, reviewerInfo.PrimaryEmail); //ExEnd:DeleteCollaborator } catch (System.Exception exp) { Console.WriteLine(exp.Message); } }
/// <summary> /// Maps annotations and creates dcocument data object in the storage /// </summary> public static void CreateAndGetAnnotation() { try { //ExStart:CreateAndGetAnnotation // Create instance of annotator. AnnotationConfig cfg = CommonUtilities.GetConfiguration(); AnnotationImageHandler annotator = new AnnotationImageHandler(cfg); IDocumentDataHandler documentRepository = annotator.GetDocumentDataHandler(); if (!Directory.Exists(cfg.StoragePath)) { Directory.CreateDirectory(cfg.StoragePath); } // 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); //ExEnd:CreateAndGetAnnotation } catch (System.Exception exp) { Console.WriteLine(exp.Message); } }