Package de.stiebernet.tools.jpg2pdf

Source Code of de.stiebernet.tools.jpg2pdf.ImagePrinter

package de.stiebernet.tools.jpg2pdf;
import java.io.FileOutputStream;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.Map;

import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfPageEvent;
import com.itextpdf.text.pdf.PdfWriter;


public class ImagePrinter implements PdfPageEvent {


  public enum TitlePositions {
    TopLeft,
    TopCenter,
    TopRight,
    BottomLeft,
    BottomCenter,
    BottomRight
  }
 
  private String filename = "images.pdf";
  private boolean contentTable = false;
  private boolean pageNumbers = false;
  private int currentPageNumber = 1;
  private int columns = 2;
  private int rows = 3;
  private TitlePositions titlePosition = TitlePositions.BottomLeft;
 
  private Document doc;
  private PdfWriter pwriter;
  private float width;
  private float height;
  private PdfPTable imageTable = null;
  private int imageCount = 0;
  private boolean mustAddTable = false;
 
  private Map<String, String> contentsMap = new LinkedHashMap<String, String>();
 
  public ImagePrinter() throws Exception {
    initDocument();
  }

  public ImagePrinter(String filename, boolean pageNumbers, boolean contentTable, int columns,
      int rows, TitlePositions titlePosition) throws Exception {
    super();
    this.filename = filename;
    this.pageNumbers = pageNumbers;
    this.contentTable = contentTable;
    this.columns = columns;
    this.rows = rows;
    this.titlePosition = titlePosition;
    initDocument();
  }

  private void initDocument() throws Exception {
    doc = new Document();
    pwriter = PdfWriter.getInstance(doc, new FileOutputStream(filename));
    pwriter.setPageEvent(this);
    width = doc.getPageSize().getWidth() - doc.leftMargin() - doc.rightMargin();
    height = doc.getPageSize().getHeight() - doc.topMargin() - doc.bottomMargin();
    doc.open();
    initImageTable();
  }
 
  private void initImageTable() {
    if(mustAddTable) {
      try {
        doc.add(imageTable);
        doc.add(Chunk.NEXTPAGE);
      } catch (DocumentException e) {
        e.printStackTrace();
      }
    }
    float[] columnWidths = new float[columns];
    Arrays.fill(columnWidths, (width / columns) / width * 100);
    imageTable = new PdfPTable(columnWidths);
    imageTable.setWidthPercentage(100F);
    imageTable.getDefaultCell().setBorder(0);
    imageCount = 0;
    mustAddTable = false;
  }
 
  public void addImage(String filename) throws Exception {
    /* image */
    Image img = Image.getInstance(filename);
    float h;
    if(rows > 0) {
      h = height / rows - 50;
    } else {
      h = height - 50;
    }
    img.scaleToFit((width / columns) - 10, h);
    PdfPCell imgCell = new PdfPCell(img);
    imgCell.setBorder(0);
   
    /* title */
    Paragraph titleParagraph = new Paragraph(new Chunk(filename.substring(0, filename.indexOf('.'))));
    PdfPCell titleCell = new PdfPCell(titleParagraph);
    titleCell.setPaddingBottom(5F);
    titleCell.setBorder(0);
    titleCell.setFixedHeight(30F);
    switch(titlePosition) {
    case TopLeft:
    case BottomLeft:
      titleCell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
      break;
    case TopCenter:
    case BottomCenter:
      titleCell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
      break;
    case TopRight:
    case BottomRight:
      titleCell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
      break;
    }
   
    /* compose */
    PdfPTable t = new PdfPTable(new float[] {100F});
    switch(titlePosition) {
    case TopLeft:
    case TopCenter:
    case TopRight:
      titleCell.setVerticalAlignment(PdfPCell.ALIGN_BOTTOM);
      t.addCell(titleCell);
      t.addCell(imgCell);
      break;
    case BottomLeft:
    case BottomCenter:
    case BottomRight:
      titleCell.setVerticalAlignment(PdfPCell.ALIGN_TOP);
      t.addCell(imgCell);
      t.addCell(titleCell);
      break;
    }
   
    /* add */
    PdfPCell tCell = new PdfPCell(t);
    tCell.setVerticalAlignment(PdfPCell.BOTTOM);
    tCell.setBorder(0);
    tCell.setPadding(5F);
    imageTable.addCell(tCell);
    mustAddTable = true;
    contentsMap.put(filename, String.valueOf(currentPageNumber));

    /* table finished ? */
    if(rows > 0 && ++imageCount >= rows * columns) {
      initImageTable();
    }
  }
 
  public void close() throws DocumentException {
    if(mustAddTable) {
      imageTable.completeRow();
      doc.add(imageTable);
    }
    if(contentTable) {
      doc.add(Chunk.NEXTPAGE);
      PdfPTable toc = new PdfPTable(new float[] {80F, 20F});
      for(String fn : contentsMap.keySet()) {
        toc.addCell(new Paragraph(fn));
        toc.addCell(new Paragraph(contentsMap.get(fn)));
      }
      doc.add(toc);
    }
    doc.close();
  }
 
  public void onEndPage(PdfWriter writer, Document document) {
    if(pageNumbers) {
      currentPageNumber = writer.getPageNumber();
      PdfContentByte cb = pwriter.getDirectContent();
      PdfPTable pageTable = new PdfPTable(new float[] {100F});
      pageTable.setTotalWidth(100F);
      PdfPCell c = new PdfPCell(new Paragraph("Seite " + writer.getPageNumber()));
      c.setBorder(0);
      c.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
      pageTable.addCell(c);
      pageTable.writeSelectedRows(0, -1, 480F, 30F, cb);
     
    }
  }

  public void onChapter(PdfWriter arg0, Document arg1, float arg2, Paragraph arg3) {}
  public void onChapterEnd(PdfWriter arg0, Document arg1, float arg2) {}
  public void onCloseDocument(PdfWriter arg0, Document arg1) {}
  public void onGenericTag(PdfWriter arg0, Document arg1, Rectangle arg2, String arg3) {}
  public void onOpenDocument(PdfWriter arg0, Document arg1) {}
  public void onParagraph(PdfWriter arg0, Document arg1, float arg2) {}
  public void onParagraphEnd(PdfWriter arg0, Document arg1, float arg2) {}
  public void onSection(PdfWriter arg0, Document arg1, float arg2, int arg3, Paragraph arg4) {}
  public void onSectionEnd(PdfWriter arg0, Document arg1, float arg2) {}
  public void onStartPage(PdfWriter arg0, Document arg1) {}
}
TOP

Related Classes of de.stiebernet.tools.jpg2pdf.ImagePrinter

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.