|
| 1 | +package GroupDocs.Annotation.Examples.Java; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import java.io.FileInputStream; |
| 5 | +import java.io.FileNotFoundException; |
| 6 | +import java.io.FileOutputStream; |
| 7 | +import java.io.InputStream; |
| 8 | +import java.io.OutputStream; |
| 9 | +import java.util.ArrayList; |
| 10 | +import java.util.Date; |
| 11 | + |
| 12 | +import org.apache.commons.io.IOUtils; |
| 13 | + |
| 14 | +import com.groupdocs.annotation.domain.AnnotationInfo; |
| 15 | +import com.groupdocs.annotation.domain.AnnotationType; |
| 16 | +import com.groupdocs.annotation.domain.DocumentType; |
| 17 | +import com.groupdocs.annotation.domain.Rectangle; |
| 18 | +import com.groupdocs.annotation.domain.config.AnnotationConfig; |
| 19 | +import com.groupdocs.annotation.handler.AnnotationImageHandler; |
| 20 | + |
| 21 | +public class ImagesAnnotation { |
| 22 | + //ExStart:ImagesAnnotation |
| 23 | + /* |
| 24 | + * document name |
| 25 | + */ |
| 26 | + public static String fileName = "image.png"; |
| 27 | + public static void differentAnnotationsForImages() throws Throwable{ |
| 28 | + AnnotationConfig cfg = Utilities.getConfiguration(); |
| 29 | + AnnotationImageHandler annotator = new AnnotationImageHandler(cfg); |
| 30 | + //From version 17.1.0 you can work with images of JPG, TIFF, BMP and GIF(static without animation) format images |
| 31 | + FileInputStream cleanPresentation = new FileInputStream(Utilities.storagePath + File.separator + fileName); |
| 32 | + ArrayList<AnnotationInfo> annotations; |
| 33 | + annotations = new ArrayList<AnnotationInfo>(); |
| 34 | + |
| 35 | + AnnotationInfo textAnnotation = new AnnotationInfo(); |
| 36 | + textAnnotation.setBox(new Rectangle(50, 510.9f, 454.73f, 29.08f)); |
| 37 | + textAnnotation.setPageNumber(0); |
| 38 | + textAnnotation.setSvgPath("[{\"x\":200,\"y\":520.92},{\"x\":454.73,\"y\":520.92},{\"x\":200,\"y\":539.98},{\"x\":454.73,\"y\":539.98}]"); |
| 39 | + textAnnotation.setType(AnnotationType.Text); |
| 40 | + textAnnotation.setCreatorName("Anonym A."); |
| 41 | + |
| 42 | + AnnotationInfo textRedactionAnnotation = new AnnotationInfo(); |
| 43 | + textRedactionAnnotation.setBox(new Rectangle(50, 510.9f, 454.73f, 29.08f)); |
| 44 | + textRedactionAnnotation.setPageNumber(0); |
| 45 | + textRedactionAnnotation.setSvgPath("[{\"x\":200,\"y\":545.92},{\"x\":454.73,\"y\":545.92},{\"x\":200,\"y\":564.98},{\"x\":454.73,\"y\":564.98}]"); |
| 46 | + textRedactionAnnotation.setType(AnnotationType.TextRedaction); |
| 47 | + textRedactionAnnotation.setCreatorName("Anonym A."); |
| 48 | + |
| 49 | + AnnotationInfo strikeoutAnnotation = new AnnotationInfo(); |
| 50 | + strikeoutAnnotation.setBox(new Rectangle(0, 510.92f, 454.73f, 29.08f)); |
| 51 | + strikeoutAnnotation.setPageNumber(0); |
| 52 | + strikeoutAnnotation.setSvgPath("[{\"x\":120,\"y\":520.92},{\"x\":454.73,\"y\":520.92},{\"x\":120,\"y\":539.98},{\"x\":454.73,\"y\":539.98}]"); |
| 53 | + strikeoutAnnotation.setType(AnnotationType.TextStrikeout); |
| 54 | + strikeoutAnnotation.setCreatorName("Anonym A."); |
| 55 | + |
| 56 | + AnnotationInfo underlineAnnotation = new AnnotationInfo(); |
| 57 | + underlineAnnotation.setBox(new Rectangle(0, 510.92f, 454.73f, 29.08f)); |
| 58 | + underlineAnnotation.setPageNumber(0); |
| 59 | + underlineAnnotation.setSvgPath("[{\"x\":120,\"y\":520.92},{\"x\":454.73,\"y\":520.92},{\"x\":120,\"y\":539.98},{\"x\":454.73,\"y\":539.98}]"); |
| 60 | + underlineAnnotation.setType(AnnotationType.TextUnderline); |
| 61 | + underlineAnnotation.setCreatorName("Anonym A."); |
| 62 | + |
| 63 | + AnnotationInfo pointAnnotation = new AnnotationInfo(); |
| 64 | + pointAnnotation.setPageNumber(0); |
| 65 | + pointAnnotation.setType(AnnotationType.Point); |
| 66 | + pointAnnotation.setBox(new Rectangle(100, 100, 0, 0)); |
| 67 | + pointAnnotation.setFieldText("Hello!"); |
| 68 | + pointAnnotation.setCreatorName("John"); |
| 69 | + |
| 70 | + AnnotationInfo areaAnnotation = new AnnotationInfo(); |
| 71 | + areaAnnotation.setCreatedOn(new Date()); |
| 72 | + areaAnnotation.setType(AnnotationType.Area); |
| 73 | + areaAnnotation.setPageNumber(0); |
| 74 | + areaAnnotation.setBox(new Rectangle(200, 72.7f, 50, 50)); |
| 75 | + areaAnnotation.setBackgroundColor(-15988609); |
| 76 | + |
| 77 | + AnnotationInfo polylineAnnotation = new AnnotationInfo(); |
| 78 | + polylineAnnotation.setType(AnnotationType.Polyline); |
| 79 | + polylineAnnotation.setCreatedOn(new Date()); |
| 80 | + polylineAnnotation.setBox(new Rectangle((float)250.82807922363281, (float)35.632957458496094, (float)102.70676422119141, (float)12.576337814331055)); |
| 81 | + polylineAnnotation.setPageNumber(0); |
| 82 | + polylineAnnotation.setPenColor(65015); |
| 83 | + polylineAnnotation.setSvgPath( |
| 84 | + "M250.8280751173709,48.209295774647885l0.6986854460093896,0l0.6986854460093896, -1.3973708920187793l0.6986854460093896," |
| 85 | + + "0l0.6986854460093896,-1.3973708920187793l1.3973708920187793, -0.6986854460093896l0.6986854460093896,-0.6986854460093896l0.6986854460093896,0l2.096056338028169," |
| 86 | + + "-1.3973708920187793l3.493427230046948,-1.3973708920187793l0.6986854460093896,-0.6986854460093896l1.3973708920187793," |
| 87 | + + "-1.3973708920187793l0.6986854460093896,0l1.3973708920187793,-0.6986854460093896l0.6986854460093896,0l0.6986854460093896," |
| 88 | + + "-0.6986854460093896l0.6986854460093896,0l0.6986854460093896,0l0,-0.6986854460093896l0.6986854460093896,0l0.6986854460093896," |
| 89 | + + "0l1.3973708920187793,0l0,-0.6986854460093896l0.6986854460093896,0l1.3973708920187793,0l0.6986854460093896,0l1.3973708920187793," |
| 90 | + + "0l0.6986854460093896,0l2.096056338028169,-0.6986854460093896l1.3973708920187793,0l0.6986854460093896,0l0.6986854460093896," |
| 91 | + + "0l1.3973708920187793,0l1.3973708920187793,0l1.3973708920187793,0l2.096056338028169,0l5.589483568075117,0l1.3973708920187793," |
| 92 | + + "0l2.096056338028169,0l0.6986854460093896,0l1.3973708920187793,0l0.6986854460093896,0l1.3973708920187793,0l1.3973708920187793," |
| 93 | + + "0l0.6986854460093896,0.6986854460093896l1.3973708920187793,0l2.096056338028169,1.3973708920187793l0.6986854460093896,0l0.6986854460093896," |
| 94 | + + "0l0,0.6986854460093896l1.3973708920187793,0l0.6986854460093896,0.6986854460093896l1.3973708920187793,0.6986854460093896l0," |
| 95 | + + "0.6986854460093896l0.6986854460093896,0l1.3973708920187793,0.6986854460093896l1.3973708920187793,0.6986854460093896l3.493427230046948," |
| 96 | + + "0.6986854460093896l1.3973708920187793,0.6986854460093896l2.096056338028169,0.6986854460093896l1.3973708920187793," |
| 97 | + + "0.6986854460093896l1.3973708920187793,0l1.3973708920187793,0.6986854460093896l0.6986854460093896,0l0.6986854460093896," |
| 98 | + + "0.6986854460093896l1.3973708920187793,0l0.6986854460093896,0l0.6986854460093896,0l2.7947417840375586,0l1.3973708920187793," |
| 99 | + + "0l0.6986854460093896,0l1.3973708920187793,0l0.6986854460093896,0l0.6986854460093896,0l1.3973708920187793," |
| 100 | + + "0l0.6986854460093896,0l2.7947417840375586,0l0.6986854460093896,0l2.7947417840375586,0l1.3973708920187793," |
| 101 | + + "0l0.6986854460093896,0l0.6986854460093896,0l0.6986854460093896,0l0.6986854460093896,0l0.6986854460093896," |
| 102 | + + "0l0.6986854460093896,0l0.6986854460093896,-0.6986854460093896l0.6986854460093896,0" |
| 103 | + ); |
| 104 | + |
| 105 | + AnnotationInfo resourceRedactionAnnotation = new AnnotationInfo(); |
| 106 | + resourceRedactionAnnotation.setCreatedOn(new Date()); |
| 107 | + resourceRedactionAnnotation.setType(AnnotationType.ResourcesRedaction); |
| 108 | + resourceRedactionAnnotation.setPageNumber(0); |
| 109 | + resourceRedactionAnnotation.setBox(new Rectangle(100, 72.7f, 50, 50)); |
| 110 | + |
| 111 | + AnnotationInfo textFieldAnnotation = new AnnotationInfo(); |
| 112 | + textFieldAnnotation.setCreatedOn(new Date()); |
| 113 | + textFieldAnnotation.setType(AnnotationType.TextField); |
| 114 | + textFieldAnnotation.setPageNumber(0); |
| 115 | + textFieldAnnotation.setBox(new Rectangle(100, 72.7f, 50, 50)); |
| 116 | + textFieldAnnotation.setFontColor(-15988609); |
| 117 | + textFieldAnnotation.setFieldText("some text"); |
| 118 | + textFieldAnnotation.setFontFamily("times new roman"); |
| 119 | + |
| 120 | + AnnotationInfo watermarkAnnotation = new AnnotationInfo(); |
| 121 | + watermarkAnnotation.setCreatedOn(new Date()); |
| 122 | + watermarkAnnotation.setType(AnnotationType.Watermark); |
| 123 | + watermarkAnnotation.setFieldText("This is watermark"); |
| 124 | + watermarkAnnotation.setFontFamily("arial"); |
| 125 | + watermarkAnnotation.setBox(new Rectangle(200, 250, 50, 0)); |
| 126 | + watermarkAnnotation.setFontSize(25); |
| 127 | + watermarkAnnotation.setFontColor(15988609); |
| 128 | + |
| 129 | + AnnotationInfo distanceAnnotation = new AnnotationInfo(); |
| 130 | + distanceAnnotation.setCreatedOn(new Date()); |
| 131 | + distanceAnnotation.setType(AnnotationType.Distance); |
| 132 | + distanceAnnotation.setBox(new Rectangle(20, 20, 250, 250)); |
| 133 | + distanceAnnotation.setBackgroundColor(-15988609); |
| 134 | + distanceAnnotation.setText("500px"); |
| 135 | + distanceAnnotation.setFontFamily("Arial"); |
| 136 | + |
| 137 | + AnnotationInfo arrowAnnotation = new AnnotationInfo(); |
| 138 | + arrowAnnotation.setCreatedOn(new Date()); |
| 139 | + arrowAnnotation.setType(AnnotationType.Arrow); |
| 140 | + arrowAnnotation.setBox(new Rectangle(250, 72.7f, 0, 20)); |
| 141 | + arrowAnnotation.setBackgroundColor(-15988609); |
| 142 | + |
| 143 | + annotations.add(textAnnotation); |
| 144 | + annotations.add(pointAnnotation); |
| 145 | + annotations.add(areaAnnotation); |
| 146 | + annotations.add(textRedactionAnnotation); |
| 147 | + annotations.add(resourceRedactionAnnotation); |
| 148 | + annotations.add(polylineAnnotation); |
| 149 | + annotations.add(watermarkAnnotation); |
| 150 | + annotations.add(textFieldAnnotation); |
| 151 | + annotations.add(strikeoutAnnotation); |
| 152 | + annotations.add(underlineAnnotation); |
| 153 | + annotations.add(distanceAnnotation); |
| 154 | + annotations.add(arrowAnnotation); |
| 155 | + |
| 156 | + InputStream result = annotator.exportAnnotationsToDocument(cleanPresentation, annotations, DocumentType.Images); |
| 157 | + |
| 158 | + try (OutputStream fileStream = new FileOutputStream(Utilities.outputPath + File.separator + "Image-annotated.png")){ |
| 159 | + System.out.println(result.available()); |
| 160 | + IOUtils.copy(result, fileStream); |
| 161 | + } |
| 162 | + //ExEnd:ImagesAnnotation |
| 163 | + } |
| 164 | +} |
0 commit comments