Skip to content

Commit

Permalink
Added image appendix on the end of exported PDF
Browse files Browse the repository at this point in the history
  • Loading branch information
Nemanja Tisma committed Sep 10, 2019
1 parent e545649 commit 807d96d
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -71,6 +73,7 @@ public ByteArrayInputStream createPdf(java.util.List<NotebookEntryState> noteboo
Rectangle pageSize = new Rectangle(PageSize.A5);
Document document = new Document(pageSize);
ByteArrayOutputStream out = new ByteArrayOutputStream();
HashMap<String, ImageComponent> appendixImages = new HashMap<>();

pdfWriter = PdfWriter.getInstance(document, out);
document.open();
Expand All @@ -84,7 +87,13 @@ public ByteArrayInputStream createPdf(java.util.List<NotebookEntryState> noteboo
Component component = notebookContent.getComponent();

if (component instanceof ImageComponent) {
addImageComponent(component, document);
ImageComponent imageComponent = (ImageComponent) component;

if (imageComponent.isZoomable()) {
appendixImages.put(notebookEntryState.getTitle(), imageComponent);
}

addImageComponent(imageComponent, document);
} else if (component instanceof VideoComponent) {
//addVideoComponent(component, document);
} else if (component instanceof TextComponent) {
Expand Down Expand Up @@ -112,6 +121,8 @@ public ByteArrayInputStream createPdf(java.util.List<NotebookEntryState> noteboo
document.add(Chunk.NEXTPAGE);
}

// appendix of images
addAppendix(appendixImages, document);
document.close();

return new ByteArrayInputStream(out.toByteArray());
Expand All @@ -121,6 +132,15 @@ public ByteArrayInputStream createPdf(java.util.List<NotebookEntryState> noteboo

}

private void addAppendix(HashMap<String, ImageComponent> appendixImages, Document document) throws DocumentException, IOException {
addText("Appendix of images", document);
for (Map.Entry<String, ImageComponent> entry : appendixImages.entrySet()) {
addText(entry.getKey(), document);
addAppendixImage(entry.getValue(), document);
document.add(Chunk.NEXTPAGE);
}
}

/**
* Pdf Link component
*
Expand Down Expand Up @@ -181,13 +201,12 @@ private void addListExercise(List<ExerciseOption> exerciseOptionList, NotebookCo
/**
* Pdf Image component
*
* @param component
* @param imageComponent
* @param document
* @throws IOException
* @throws DocumentException
*/
private void addImageComponent(Component component, Document document) throws IOException, DocumentException {
ImageComponent imageComponent = (ImageComponent) component;
private void addImageComponent(ImageComponent imageComponent, Document document) throws IOException, DocumentException {
for (ImageResource imageResource : imageComponent.getImages()) {
Path path = Paths.get(resourceStaticDirectory + File.separator + imageResource.getPath());
document.add(PdfStylingService.getImageStyle(path.toFile().getAbsolutePath()));
Expand All @@ -197,6 +216,16 @@ private void addImageComponent(Component component, Document document) throws IO
document.add(Chunk.NEWLINE);
}

private void addAppendixImage(ImageComponent imageComponent, Document document) throws IOException, DocumentException {
for (ImageResource imageResource : imageComponent.getImages()) {
Path path = Paths.get(resourceStaticDirectory + File.separator + imageResource.getPath());
document.add(PdfStylingService.getAppendixImageStyle(path.toFile().getAbsolutePath()));
}

document.add(PdfStylingService.getCaptionStyle(imageComponent.getCaption()));
document.add(Chunk.NEWLINE);
}

/**
* Pdf Text component
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public class PdfStylingService {
public static final int captionFontSize = 8;
public static final int IMAGE_WIDTH = 350;
public static final int IMAGE_HEIGHT = 150;
public static final int IMAGE_APPENDIX_WIDTH = 480;
public static final int IMAGE_APPENDIX_HEIGHT = 205;
public static final int IMAGE_ROTATION = 3;
public static final BaseColor titleFontColor = BaseColor.DARK_GRAY;
public static final BaseColor textFontColor = BaseColor.DARK_GRAY;
Expand Down Expand Up @@ -54,6 +56,14 @@ public static Image getImageStyle (String imagePath) throws BadElementException,
return image;
}

public static Image getAppendixImageStyle (String imagePath) throws BadElementException, IOException {
Image image = Image.getInstance(imagePath);
image.scaleToFit(IMAGE_APPENDIX_WIDTH, IMAGE_APPENDIX_HEIGHT);
image.setAlignment(Element.ALIGN_CENTER);

return image;
}

public static Paragraph getCaptionStyle(String text) {
return new Paragraph(text, FontFactory.getFont("LibreItalic", captionFontSize, captionFontColor));
}
Expand Down

0 comments on commit 807d96d

Please sign in to comment.