Skip to content
This repository was archived by the owner on Jul 26, 2021. It is now read-only.

Commit 440370c

Browse files
authored
Merge pull request #25 from groupdocs-total/print-annotated
print annotated file without saving
2 parents 2747b72 + 07fa89e commit 440370c

File tree

7 files changed

+102
-102
lines changed

7 files changed

+102
-102
lines changed

src/main/java/com/groupdocs/ui/annotation/annotator/TextAnnotator.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@ public AnnotationInfo annotateCells() throws ParseException {
4343
// init possible types of annotations
4444
AnnotationInfo textAnnotation = super.initAnnotationInfo();
4545
textAnnotation.setPageNumber(annotationData.getPageNumber());
46-
// we use such calculation since the GroupDocs.Annotation library takes text line position from the bottom of the page
47-
double topPosition = pageData.getHeight() - annotationData.getTop();
4846
textAnnotation.setAnnotationPosition(new Point(1, 1));
4947
textAnnotation.setBox(new Rectangle(0, 0, 0, 0));
5048
return textAnnotation;

src/main/java/com/groupdocs/ui/annotation/entity/request/AnnotateDocumentRequest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ public class AnnotateDocumentRequest extends LoadDocumentRequest {
1717
* Document type
1818
*/
1919
private String documentType;
20+
/**
21+
* For print annotated file
22+
*/
23+
private Boolean print;
2024

2125
public AnnotationDataEntity[] getAnnotationsData() {
2226
return annotationsData;
@@ -33,4 +37,12 @@ public String getDocumentType() {
3337
public void setDocumentType(String documentType) {
3438
this.documentType = documentType;
3539
}
40+
41+
public Boolean getPrint() {
42+
return print;
43+
}
44+
45+
public void setPrint(Boolean print) {
46+
this.print = print;
47+
}
3648
}

src/main/java/com/groupdocs/ui/annotation/entity/request/TextCoordinatesRequest.java

Lines changed: 0 additions & 45 deletions
This file was deleted.

src/main/java/com/groupdocs/ui/annotation/service/AnnotationServiceImpl.java

Lines changed: 51 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.groupdocs.ui.annotation.service;
22

3-
import com.google.common.collect.Lists;
43
import com.groupdocs.annotation.common.exception.AnnotatorException;
54
import com.groupdocs.annotation.common.license.License;
65
import com.groupdocs.annotation.domain.AnnotationInfo;
@@ -43,16 +42,14 @@
4342
import java.util.Base64;
4443
import java.util.List;
4544

45+
import static com.groupdocs.ui.annotation.util.DocumentTypesConverter.checkedDocumentType;
4646
import static com.groupdocs.ui.annotation.util.DocumentTypesConverter.getDocumentType;
4747
import static com.groupdocs.ui.annotation.util.PathConstants.OUTPUT_FOLDER;
4848

4949
@Service
5050
public class AnnotationServiceImpl implements AnnotationService {
5151
private static final Logger logger = LoggerFactory.getLogger(AnnotationServiceImpl.class);
5252

53-
private static final List<String> supportedImageFormats = Lists.newArrayList("bmp", "jpeg", "jpg", "tiff", "tif", "png", "gif", "emf", "wmf", "dwg", "dicom", "djvu");
54-
private static final List<String> supportedDiagramFormats = Lists.newArrayList(".vsd", ".vdx", ".vss", ".vsx", ".vst", ".vtx", ".vsdx", ".vdw", ".vstx", ".vssx");
55-
5653
@Autowired
5754
private GlobalConfiguration globalConfiguration;
5855

@@ -83,11 +80,11 @@ public void init() {
8380
}
8481

8582
private void initOutputDirectory() {
86-
if(StringUtils.isEmpty(annotationConfiguration.getOutputDirectory())) {
83+
if (StringUtils.isEmpty(annotationConfiguration.getOutputDirectory())) {
8784
String outputDirectory = String.format("%s%s", annotationConfiguration.getFilesDirectory(), OUTPUT_FOLDER);
8885
annotationConfiguration.setOutputDirectory(outputDirectory);
8986
}
90-
if(!new File(annotationConfiguration.getOutputDirectory()).exists()) {
87+
if (!new File(annotationConfiguration.getOutputDirectory()).exists()) {
9188
new File(annotationConfiguration.getOutputDirectory()).mkdirs();
9289
}
9390
}
@@ -110,7 +107,7 @@ public List<FileDescriptionEntity> getFileList(FileTreeRequest fileTreeRequest)
110107
// get temp directory name
111108
String tempDirectoryName = new AnnotationConfig().getTempFolderName();
112109
try {
113-
FileTreeContainer fileListContainer = getAnnotationImageHandler().loadFileTree(fileListOptions);
110+
FileTreeContainer fileListContainer = annotationHandler.loadFileTree(fileListOptions);
114111

115112
List<FileDescriptionEntity> fileList = new ArrayList<>();
116113
// parse files/folders list
@@ -153,24 +150,17 @@ public AnnotatedDocumentEntity getDocumentDescription(LoadDocumentRequest loadDo
153150
}
154151
// get document info container
155152
String fileName = new File(documentGuid).getName();
156-
DocumentInfoContainer documentDescription = getAnnotationImageHandler().getDocumentInfo(fileName, password);
153+
DocumentInfoContainer documentDescription = annotationHandler.getDocumentInfo(fileName, password);
157154

158-
String documentType = documentDescription.getDocumentType();
159-
String fileExtension = parseFileExtension(documentGuid);
160-
// check if document type is image
161-
if (supportedImageFormats.contains(fileExtension)) {
162-
documentType = "image";
163-
} else if (supportedDiagramFormats.contains(fileExtension)) {
164-
documentType = "diagram";
165-
}
155+
String documentType = checkedDocumentType(documentGuid, documentDescription.getDocumentType());
166156
// check if document contains annotations
167157
AnnotationInfo[] annotations = getAnnotations(documentGuid, documentType);
168158
// get info about each document page
169159
List<PageImage> pageImages = null;
170160
List<PageData> pages = documentDescription.getPages();
171161
// TODO: remove once perf. issue is fixed
172-
if(annotationConfiguration.getPreloadPageCount() == 0){
173-
pageImages = getAnnotationImageHandler().getPages(fileName, imageOptions);
162+
if (annotationConfiguration.getPreloadPageCount() == 0) {
163+
pageImages = annotationHandler.getPages(fileName, imageOptions);
174164
}
175165
String[] supportedAnnotations = SupportedAnnotations.getSupportedAnnotations(documentType);
176166
// initiate custom Document description object
@@ -191,7 +181,7 @@ public AnnotatedDocumentEntity getDocumentDescription(LoadDocumentRequest loadDo
191181
page.setAnnotations(AnnotationMapper.instance.mapForPage(annotations, page.getNumber()));
192182
}
193183
// TODO: remove once perf. issue is fixed
194-
if(pageImages != null) {
184+
if (pageImages != null) {
195185
byte[] bytes = IOUtils.toByteArray(pageImages.get(i).getStream());
196186
String encodedImage = Base64.getEncoder().encodeToString(bytes);
197187
page.setData(encodedImage);
@@ -223,7 +213,7 @@ public LoadedPageEntity getDocumentPage(LoadDocumentPageRequest loadDocumentPage
223213
}
224214
// get page image
225215
String fileName = new File(documentGuid).getName();
226-
List<PageImage> images = getAnnotationImageHandler().getPages(fileName, imageOptions);
216+
List<PageImage> images = annotationHandler.getPages(fileName, imageOptions);
227217

228218
byte[] bytes = IOUtils.toByteArray(images.get(pageNumber - 1).getStream());
229219
// encode ByteArray into String
@@ -238,15 +228,6 @@ public LoadedPageEntity getDocumentPage(LoadDocumentPageRequest loadDocumentPage
238228
}
239229
}
240230

241-
/**
242-
* Create new instance for AnnotationImageHandler
243-
*
244-
* @return AnnotationImageHandler
245-
*/
246-
private AnnotationImageHandler getAnnotationImageHandler() {
247-
return annotationHandler;
248-
}
249-
250231
@Override
251232
public AnnotatedDocumentEntity annotate(AnnotateDocumentRequest annotateDocumentRequest) {
252233
AnnotatedDocumentEntity annotatedDocument = new AnnotatedDocumentEntity();
@@ -255,55 +236,76 @@ public AnnotatedDocumentEntity annotate(AnnotateDocumentRequest annotateDocument
255236
String documentGuid = annotateDocumentRequest.getGuid();
256237
String password = annotateDocumentRequest.getPassword();
257238
AnnotationDataEntity[] annotationsData = annotateDocumentRequest.getAnnotationsData();
258-
String documentType = annotateDocumentRequest.getDocumentType();
239+
String documentType = checkedDocumentType(documentGuid, annotateDocumentRequest.getDocumentType());
259240
// initiate AnnotatedDocument object
260241
// get document info - required to get document page height and calculate annotation top position
261-
DocumentInfoContainer documentInfo = getAnnotationImageHandler().getDocumentInfo(new File(documentGuid).getName(), password);
262-
// check if document type is image
263-
String fileExtension = parseFileExtension(documentGuid);
264-
if (supportedImageFormats.contains(fileExtension)) {
265-
documentType = "image";
266-
}
242+
DocumentInfoContainer documentInfo = annotationHandler.getDocumentInfo(new File(documentGuid).getName(), password);
267243
// initiate list of annotations to add
268244
List<AnnotationInfo> annotations = new ArrayList<>();
269245
InputStream file = new FileInputStream(documentGuid);
270-
file = getAnnotationImageHandler().removeAnnotationStream(file);
271-
Throwable exc = null;
246+
file = annotationHandler.removeAnnotationStream(file);
272247
for (AnnotationDataEntity annotationData : annotationsData) {
273248
// create annotator
274249
PageData pageData = documentInfo.getPages().get(annotationData.getPageNumber() - 1);
275250
// add annotation, if current annotation type isn't supported by the current document type it will be ignored
276251
try {
277252
annotations.add(AnnotatorFactory.createAnnotator(annotationData, pageData).getAnnotationInfo(documentType));
278-
} catch (UnsupportedOperationException ex) {
279-
exc = ex;
280253
} catch (Exception ex) {
281254
throw new TotalGroupDocsException(ex.getMessage(), ex);
282255
}
283256
}
284-
String fileName = new File(documentGuid).getName();
257+
String forPrint = annotateDocumentRequest.getPrint() ? "Temp" : "";
258+
String fileName = FilenameUtils.getBaseName(documentGuid) + forPrint + "." + FilenameUtils.getExtension(documentGuid);
285259
String path = annotationConfiguration.getOutputDirectory() + File.separator + fileName;
286260
// check if annotations array contains at least one annotation to add
287261
if (annotations.size() > 0) {
288262
// Add annotation to the document
289263
int type = getDocumentType(documentType);
290264
// Save result stream to file.
291-
file = getAnnotationImageHandler().exportAnnotationsToDocument(file, annotations, type);
265+
file = annotationHandler.exportAnnotationsToDocument(file, annotations, type);
292266
}
293267
(new File(path)).delete();
294-
try (OutputStream fileStream = new FileOutputStream(path)) {
295-
IOUtils.copyLarge(file, fileStream);
296-
annotatedDocument.setGuid(path);
268+
if (annotateDocumentRequest.getPrint()) {
269+
List<PageDataDescriptionEntity> annotatedPages = getAnnotatedPages(password, file);
270+
annotatedDocument.setPages(annotatedPages);
271+
(new File(path)).delete();
272+
} else {
273+
try (OutputStream fileStream = new FileOutputStream(path)) {
274+
IOUtils.copyLarge(file, fileStream);
275+
annotatedDocument.setGuid(path);
276+
}
297277
}
298278
} catch (Exception ex) {
299279
throw new TotalGroupDocsException(ex.getMessage(), ex);
300280
}
301281
return annotatedDocument;
302282
}
303283

304-
public String parseFileExtension(String documentGuid) {
305-
String extension = FilenameUtils.getExtension(documentGuid);
306-
return extension == null ? null : extension.toLowerCase();
284+
/**
285+
* Get pages images of annotated file
286+
*
287+
* @param password password for the file
288+
* @param inputStream stream of annotated file
289+
* @return list of pages
290+
* @throws IOException
291+
*/
292+
private List<PageDataDescriptionEntity> getAnnotatedPages(String password, InputStream inputStream) throws IOException {
293+
ImageOptions imageOptions = new ImageOptions();
294+
// set password for protected document
295+
if (!password.isEmpty()) {
296+
imageOptions.setPassword(password);
297+
}
298+
List<PageImage> pages = annotationHandler.getPages(inputStream, imageOptions);
299+
List<PageDataDescriptionEntity> pagesDescriptions = new ArrayList<>(pages.size());
300+
for (PageImage pageImage : pages) {
301+
byte[] bytes = IOUtils.toByteArray(pageImage.getStream());
302+
String encodedImage = Base64.getEncoder().encodeToString(bytes);
303+
PageDataDescriptionEntity page = new PageDataDescriptionEntity();
304+
page.setData(encodedImage);
305+
306+
pagesDescriptions.add(page);
307+
}
308+
return pagesDescriptions;
307309
}
308310

309311
/**
@@ -315,9 +317,8 @@ public String parseFileExtension(String documentGuid) {
315317
*/
316318
private AnnotationInfo[] getAnnotations(String documentGuid, String documentType) {
317319
try (InputStream documentStream = new FileInputStream(documentGuid)) {
318-
AnnotationImageHandler annotationImageHandler = getAnnotationImageHandler();
319320
int docType = getDocumentType(documentType);
320-
return new Importer(documentStream, annotationImageHandler).importAnnotations(docType);
321+
return new Importer(documentStream, annotationHandler).importAnnotations(docType);
321322
} catch (AnnotatorException annotatorException) {
322323
logger.error("Exception while extract annotations from file {}: {}", FilenameUtils.getName(documentGuid), annotatorException.getCause().getLocalizedMessage());
323324
return new AnnotationInfo[0];

src/main/java/com/groupdocs/ui/annotation/util/DocumentTypesConverter.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
11
package com.groupdocs.ui.annotation.util;
22

3+
import com.google.common.collect.Lists;
34
import com.groupdocs.annotation.domain.DocumentType;
45

6+
import java.util.List;
7+
8+
import static com.groupdocs.ui.util.Utils.parseFileExtension;
9+
510
/**
611
* Converter for document types
712
*/
813
public class DocumentTypesConverter {
14+
15+
private static final List<String> supportedImageFormats = Lists.newArrayList("bmp", "jpeg", "jpg", "tiff", "tif", "png", "gif", "emf", "wmf", "dwg", "dicom", "djvu");
16+
private static final List<String> supportedDiagramFormats = Lists.newArrayList(".vsd", ".vdx", ".vss", ".vsx", ".vst", ".vtx", ".vsdx", ".vdw", ".vstx", ".vssx");
17+
918
/**
1019
* Convert document type from string into int
1120
*
@@ -35,4 +44,22 @@ public static final int getDocumentType(String documentType) {
3544
return DocumentType.Undefined;
3645
}
3746
}
47+
48+
/**
49+
* Check image and diagram document types
50+
*
51+
* @param documentGuid document name
52+
* @param documentType string value of document type
53+
* @return correct document type
54+
*/
55+
public static String checkedDocumentType(String documentGuid, String documentType) {
56+
String fileExtension = parseFileExtension(documentGuid);
57+
// check if document type is image
58+
if (supportedImageFormats.contains(fileExtension)) {
59+
documentType = "image";
60+
} else if (supportedDiagramFormats.contains(fileExtension)) {
61+
documentType = "diagram";
62+
}
63+
return documentType;
64+
}
3865
}

src/main/java/com/groupdocs/ui/config/ServerConfiguration.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
package com.groupdocs.ui.config;
22

3-
import org.springframework.beans.factory.annotation.Value;
43
import org.slf4j.Logger;
54
import org.slf4j.LoggerFactory;
5+
import org.springframework.beans.factory.annotation.Value;
66
import org.springframework.stereotype.Component;
77

8-
import javax.annotation.PostConstruct;
9-
import java.net.InetAddress;
10-
import java.net.UnknownHostException;
11-
128
@Component
139
public class ServerConfiguration {
1410
private static final Logger logger = LoggerFactory.getLogger(ServerConfiguration.class);

src/main/java/com/groupdocs/ui/util/Utils.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,17 @@ public class Utils {
3030
public static final FileNameComparator FILE_NAME_COMPARATOR = new FileNameComparator();
3131
public static final FileTypeComparator FILE_TYPE_COMPARATOR = new FileTypeComparator();
3232

33+
/**
34+
* Parse extension of the file's name
35+
*
36+
* @param documentGuid path to file
37+
* @return extension of the file's name
38+
*/
39+
public static String parseFileExtension(String documentGuid) {
40+
String extension = FilenameUtils.getExtension(documentGuid);
41+
return extension == null ? null : extension.toLowerCase();
42+
}
43+
3344
/**
3445
* Fill header HTTP response with file data
3546
*/

0 commit comments

Comments
 (0)