Commit 1c6a66b7 authored by Hermann Owren Elton's avatar Hermann Owren Elton Committed by Olaf Rosendahl
Browse files

Make pdfcreator more readable and not as hardcoded

parent ab3bedce
......@@ -106,7 +106,7 @@ $('.navPadding').css('padding-top', $('.fixedNav').css("height"));
<li>java.lang.Object</li>
<li>
<ul class="inheritance">
<li>Components.PDFcreator</li>
<li>Components.PdfCreator</li>
</ul>
</li>
</ul>
......
package Components;
import Database.HibernateClasses.Photo;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
/**
* Class that is used to create a photo album in the form of a pdf file
*/
public final class PDFcreator {
/**
* Private constructor to hinder creation of utility class
*/
private PDFcreator() {
throw new IllegalStateException("Can not make instance of utility class");
}
/**
* Creates a pdf from a list of photos and saves them where the user wants them to be saved
* @param photos Is the list of photos
* @param saveLocation Is where the user wants the pdf saved
* @throws DocumentException Is thrown is a document can't be made or created
* @throws IOException Is thrown if there is a mistake in the save location url and or the image url is wrong
*/
public static void createPDF(List<Photo> photos, String saveLocation) throws DocumentException, IOException{
// Opens a new document and makes it a pdf
Document document = new Document(PageSize.A4, 0, 0, 0, 0);
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(saveLocation));
document.open();
PdfContentByte content = writer.getDirectContent();
int row = 0;
int photoCounter = 0;
int xPos;
int yPos;
for (Photo photo : photos) {
xPos = 20;
Image img = Image.getInstance(photo.getUrl());
// Sets up photos side by side
if (photoCounter == 1 || photoCounter == 3 || photoCounter == 5) {
xPos = xPos + 320;
}
// Switches row
if (photoCounter == 2 || photoCounter == 4) {
row++;
}
// Makes sure that a new page is created if 6 images are added to a page
if (photoCounter == 6) {
document.newPage();
row = 0;
photoCounter = 0;
}
// Begins writing text for the image titles
content.beginText();
BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA_BOLD, BaseFont.WINANSI, BaseFont.EMBEDDED);
content.setFontAndSize(bf, 14);
// If sentence that changes position of the image according to the image size
if (img.getWidth() > img.getHeight()) {
yPos = 600 - (row * 270);
content.showTextAligned(1, photo.getTitle(), (float)xPos + 80, (float)yPos - 20, 0);
content.endText();
img.setAbsolutePosition(xPos, yPos);
img.scaleToFit(200, 400);
content.addImage(img);
} else {
yPos = 600 - (row * 270);
content.showTextAligned(1, photo.getTitle(), (float)xPos + 80, (float)yPos - 20, 0);
content.endText();
img.setAbsolutePosition(xPos, yPos);
img.scaleToFit(400, 200);
content.addImage(img);
}
photoCounter++;
}
document.close();
}
}
package Components;
import Database.HibernateClasses.Photo;
import Database.HibernateClasses.User;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.List;
/**
* Class that is used to create a photo album in the form of a pdf file
*/
public final class PdfCreator {
private static Document document;
private static List<Photo> photos;
private static String albumName;
private static Font smallFont = new Font(Font.FontFamily.TIMES_ROMAN, 16, Font.BOLD);
private static Font headerFont = new Font(Font.FontFamily.TIMES_ROMAN, 18, Font.BOLD);
/**
* Private constructor to hinder creation of utility class
*/
private PdfCreator() {
throw new IllegalStateException("Can not make instance of utility class");
}
/**
* Creates a new pdf document with all the photos, author name, and album name.
* @param AlbumPhotos all the photos in the album that is getting made into a Pdf.
* @param saveLocation is where the Pdf is going to be saved.
* @param name is the name of the album.
* @throws IOException
* @throws DocumentException
*/
public static void createPdf(List<Photo> AlbumPhotos, String saveLocation, String name) throws IOException, DocumentException {
photos = AlbumPhotos;
albumName = name;
document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(saveLocation));
document.open();
addHeader();
addImagesContainer();
document.close();
}
/**
* Adds the header to the Pdf document.
* @throws DocumentException
*/
private static void addHeader() throws DocumentException {
Paragraph headline = new Paragraph();
addEmptyLineTo(headline, 1);
headline.add(new Paragraph(albumName, headerFont));
addEmptyLineTo(headline, 1);
headline.add(new Paragraph("Generated by: " + UserInfo.getUser().getUsername() + ", " + new Date(), smallFont));
addEmptyLineTo(headline, 2);
document.add(headline);
}
/**
* Adds the image container to the pdf document.
* @throws DocumentException
* @throws IOException
*/
private static void addImagesContainer() throws DocumentException, IOException {
Paragraph imagesContainer = new Paragraph();
addEmptyLineTo(imagesContainer, 1);
Paragraph images = new Paragraph("Images", smallFont);
addEmptyLineTo(images, 1);
imagesContainer.add(images);
document.add(imagesContainer);
addAllImages();
}
/**
* Adds all the images to the Pdf document.
* @throws DocumentException
* @throws IOException
*/
private static void addAllImages() throws DocumentException, IOException {
for (Photo photo: photos) {
Paragraph photoBox = new Paragraph();
Paragraph imageBox = new Paragraph();
Image image = Image.getInstance(photo.getUrl());
scaleImage(image);
imageBox.add(image);
Paragraph titleBox = new Paragraph(photo.getTitle());
photoBox.add(0,titleBox);
photoBox.add(1, imageBox);
addEmptyLineTo(photoBox, 2);
document.add(photoBox);
}
}
/**
* Scales the images according to the ratio between the height and the width of the image.
* @param image the image that is getting scaled.
*/
private static void scaleImage(Image image) {
if (image.getWidth() > image.getHeight()) {
image.scaleToFit(300, 600);
} else {
image.scaleToFit(600, 300);
}
}
/**
* Add an empty line to the Pdf document.
*
* @param paragraph the paragraph to insert an empty line into.
* @param number the number of empty lines desired.
*/
private static void addEmptyLineTo(Paragraph paragraph, int number) {
for (int i = 0; i < number; i++) {
paragraph.add(new Paragraph(" "));
}
}
}
......@@ -2,7 +2,7 @@ package Roots;
import Components.FileLogger;
import Components.PopupWindow;
import Components.PDFcreator;
import Components.PdfCreator;
import Components.PhotoContainer;
import Components.UserInfo;
import Css.Css;
......@@ -248,7 +248,7 @@ final class AlbumDetailsRoot extends SceneRoot {
photos.addAll(this.albumPhotoList);
String saveLink = saveLocation + "/" + albumName + ".pdf";
try {
PDFcreator.createPDF(photos, saveLink);
PdfCreator.createPdf(photos, saveLink, albumName);
File pdfFile = new File(saveLink);
if (pdfFile.exists() && Desktop.isDesktopSupported()) {
Desktop.getDesktop().open(pdfFile);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment