package AlMaGe.export;
import java.io.IOException;
import java.net.MalformedURLException;
import javax.swing.JOptionPane;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfPageEventHelper;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
public class PDFHeaderFooterWriter extends PdfPageEventHelper
{
private static Image image;
private static Font headerBigFont = new Font(Font.FontFamily.TIMES_ROMAN, 22,
Font.BOLD, new BaseColor(0, 51, 102));
private static Font headerSmallFont = new Font(Font.FontFamily.TIMES_ROMAN, 16,
Font.BOLD, new BaseColor(0, 51, 102));
private static Font headerAdressFont = new Font(Font.FontFamily.TIMES_ROMAN, 8,
Font.NORMAL);
@Override
public void onStartPage(PdfWriter writer, Document document) {
Rectangle page = document.getPageSize();
float[] relSize = {7, 4};
PdfPTable head = new PdfPTable(relSize);
PdfPTable innerHead = new PdfPTable(1);
//Image
try {
image = Image.getInstance(PDFHeaderFooterWriter.class.getResource("/images/logo.jpg"));
} catch (BadElementException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(
null,
e.getMessage()
+ System.getProperty("line.separator")
+ "Dies ist ein schwerwiegender Fehler."
+ System.getProperty("line.separator")
+ "Das Programm wird beendet.",
"Fehler", JOptionPane.WARNING_MESSAGE);
System.exit(1);
} catch (MalformedURLException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(
null,
e.getMessage()
+ System.getProperty("line.separator")
+ "Dies ist ein schwerwiegender Fehler."
+ System.getProperty("line.separator")
+ "Das Programm wird beendet.",
"Fehler", JOptionPane.WARNING_MESSAGE);
System.exit(1);
} catch (IOException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(
null,
e.getMessage()
+ System.getProperty("line.separator")
+ "Dies ist ein schwerwiegender Fehler."
+ System.getProperty("line.separator")
+ "Das Programm wird beendet.",
"Fehler", JOptionPane.WARNING_MESSAGE);
System.exit(1);
}
image.scalePercent(35);
// image.setAlignment(Image.RIGHT | Image.TEXTWRAP);//Code 2
Paragraph paraImgHead = new Paragraph();
paraImgHead.add(new Chunk(image, 0, 0));
paraImgHead.add(new Phrase(""));
PdfPCell tmp = new PdfPCell(innerHead);
tmp.setBorder(Rectangle.NO_BORDER);
tmp.setBorderWidth(0);
tmp.setPadding(0);
tmp.setUseBorderPadding(false);
tmp.setHorizontalAlignment(Element.ALIGN_CENTER);
head.addCell(tmp);
tmp = new PdfPCell(paraImgHead);
tmp.setBorder(Rectangle.NO_BORDER);
tmp.setBorderWidth(0);
tmp.setPadding(0);
tmp.setUseBorderPadding(false);
tmp.setHorizontalAlignment(Element.ALIGN_CENTER);
head.addCell(tmp);
try {
image = Image.getInstance(PDFHeaderFooterWriter.class.getResource("/images/aufzaehl.jpg"));
} catch (BadElementException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(
null,
e.getMessage()
+ System.getProperty("line.separator")
+ "Dies ist ein schwerwiegender Fehler."
+ System.getProperty("line.separator")
+ "Das Programm wird beendet.",
"Fehler", JOptionPane.WARNING_MESSAGE);
System.exit(1);
} catch (MalformedURLException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(
null,
e.getMessage()
+ System.getProperty("line.separator")
+ "Dies ist ein schwerwiegender Fehler."
+ System.getProperty("line.separator")
+ "Das Programm wird beendet.",
"Fehler", JOptionPane.WARNING_MESSAGE);
System.exit(1);
} catch (IOException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(
null,
e.getMessage()
+ System.getProperty("line.separator")
+ "Dies ist ein schwerwiegender Fehler."
+ System.getProperty("line.separator")
+ "Das Programm wird beendet.",
"Fehler", JOptionPane.WARNING_MESSAGE);
System.exit(1);
}
image.scalePercent(5);
Paragraph paraHead0 = new Paragraph();
paraHead0.add(new Chunk(image, 0, 0));
paraHead0.add(new Paragraph(" Vor-Ort-Service ", headerBigFont));
tmp = new PdfPCell(paraHead0);
tmp.setBorder(Rectangle.NO_BORDER);
tmp.setBorderWidth(0);
tmp.setPadding(0);
tmp.setUseBorderPadding(false);
tmp.setHorizontalAlignment(Element.ALIGN_CENTER);
innerHead.addCell(tmp);
try {
image = Image.getInstance(PDFHeaderFooterWriter.class.getResource("/images/aufzaehl.jpg"));
} catch (BadElementException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(
null,
e.getMessage()
+ System.getProperty("line.separator")
+ "Dies ist ein schwerwiegender Fehler."
+ System.getProperty("line.separator")
+ "Das Programm wird beendet.",
"Fehler", JOptionPane.WARNING_MESSAGE);
System.exit(1);
} catch (MalformedURLException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(
null,
e.getMessage()
+ System.getProperty("line.separator")
+ "Dies ist ein schwerwiegender Fehler."
+ System.getProperty("line.separator")
+ "Das Programm wird beendet.",
"Fehler", JOptionPane.WARNING_MESSAGE);
System.exit(1);
} catch (IOException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(
null,
e.getMessage()
+ System.getProperty("line.separator")
+ "Dies ist ein schwerwiegender Fehler."
+ System.getProperty("line.separator")
+ "Das Programm wird beendet.",
"Fehler", JOptionPane.WARNING_MESSAGE);
System.exit(1);
}
image.scalePercent(3);
Paragraph paraHead1 = new Paragraph();
paraHead1.add(new Chunk(image, 0, 0));
paraHead1.add(new Phrase(" Software ", headerSmallFont));
paraHead1.add(new Chunk(image, 0, 0));
paraHead1.add(new Phrase(" Computersysteme", headerSmallFont));
tmp = new PdfPCell(paraHead1);
tmp.setBorder(Rectangle.NO_BORDER);
tmp.setBorderWidth(0);
tmp.setPadding(0);
tmp.setUseBorderPadding(false);
tmp.setHorizontalAlignment(Element.ALIGN_CENTER);
innerHead.addCell(tmp);
Paragraph paraHead2 = new Paragraph();
paraHead2.add(new Phrase(" "));
paraHead2.add(new Chunk(image, 0, 0));
paraHead2.add(new Phrase(" Zubeh�r ", headerSmallFont));
paraHead2.add(new Chunk(image, 0, 0));
paraHead2.add(new Phrase(" Drucker", headerSmallFont));
tmp = new PdfPCell(paraHead2);
tmp.setBorder(Rectangle.NO_BORDER);
tmp.setBorderWidth(0);
tmp.setPadding(0);
tmp.setUseBorderPadding(false);
tmp.setHorizontalAlignment(Element.ALIGN_LEFT);
innerHead.addCell(tmp);
tmp = new PdfPCell(new Paragraph(" ", headerAdressFont));
tmp.setBorder(Rectangle.NO_BORDER);
tmp.setBorderWidth(0);
tmp.setPadding(0);
tmp.setUseBorderPadding(false);
tmp.setHorizontalAlignment(Element.ALIGN_CENTER);
innerHead.addCell(tmp);
head.setTotalWidth(page.getWidth() - document.leftMargin() - document.rightMargin());
head.writeSelectedRows(0, -1, document.leftMargin(), document.top(),
writer.getDirectContent());
}
public void onEndPage(PdfWriter writer, Document document)
{
try {
Rectangle page = document.getPageSize();
PdfPTable head = new PdfPTable(4);
head.getDefaultCell().setBorder(Rectangle.NO_BORDER);
PdfPCell tmp = new PdfPCell(new Phrase("Bis zur vollst�ndigen Bezahlung der Ware bleibt diese Eigentum von Matthias M�ller Computerservice.", new Font(Font.FontFamily.TIMES_ROMAN, 8, Font.BOLD)));
tmp.setBorder(Rectangle.NO_BORDER);
tmp.setBorderWidth(0);
tmp.setPadding(0);
tmp.setUseBorderPadding(false);
tmp.setColspan(4);
tmp.setHorizontalAlignment(Element.ALIGN_CENTER);
head.addCell(tmp);
tmp = new PdfPCell(new Phrase("Wir machen gebrauch von der Kleinunternehmerregelung nach � 19 UStG und sind daher nicht gezwungen die Umsatzsteuer auszuweisen.", new Font(Font.FontFamily.TIMES_ROMAN, 8, Font.NORMAL)));
tmp.setBorder(Rectangle.NO_BORDER);
tmp.setBorderWidth(0);
tmp.setPadding(0);
tmp.setUseBorderPadding(false);
tmp.setColspan(4);
tmp.setHorizontalAlignment(Element.ALIGN_CENTER);
head.addCell(tmp);
tmp = new PdfPCell(new Phrase(" ", new Font(Font.FontFamily.TIMES_ROMAN, 8, Font.NORMAL)));
tmp.setBorder(Rectangle.BOTTOM);
tmp.setPadding(0);
tmp.setUseBorderPadding(false);
tmp.setColspan(4);
tmp.setHorizontalAlignment(Element.ALIGN_CENTER);
head.addCell(tmp);
tmp = new PdfPCell(new Phrase("Anschrift:", new Font(Font.FontFamily.TIMES_ROMAN, 8, Font.NORMAL)));
tmp.setBorder(Rectangle.NO_BORDER);
tmp.setBorderWidth(0);
tmp.setPadding(0);
tmp.setUseBorderPadding(false);
tmp.setHorizontalAlignment(Element.ALIGN_LEFT);
head.addCell(tmp);
tmp = new PdfPCell(new Phrase("Internet:", new Font(Font.FontFamily.TIMES_ROMAN, 8, Font.NORMAL)));
tmp.setBorder(Rectangle.NO_BORDER);
tmp.setBorderWidth(0);
tmp.setPadding(0);
tmp.setUseBorderPadding(false);
tmp.setHorizontalAlignment(Element.ALIGN_LEFT);
head.addCell(tmp);
tmp = new PdfPCell(new Phrase("Telefon:", new Font(Font.FontFamily.TIMES_ROMAN, 8, Font.NORMAL)));
tmp.setBorder(Rectangle.NO_BORDER);
tmp.setBorderWidth(0);
tmp.setPadding(0);
tmp.setUseBorderPadding(false);
tmp.setHorizontalAlignment(Element.ALIGN_LEFT);
head.addCell(tmp);
tmp = new PdfPCell(new Phrase("Bankverbindung:", new Font(Font.FontFamily.TIMES_ROMAN, 8, Font.NORMAL)));
tmp.setBorder(Rectangle.NO_BORDER);
tmp.setBorderWidth(0);
tmp.setPadding(0);
tmp.setUseBorderPadding(false);
tmp.setHorizontalAlignment(Element.ALIGN_LEFT);
head.addCell(tmp);
tmp = new PdfPCell(new Phrase("Matthias M�ller", new Font(Font.FontFamily.TIMES_ROMAN, 8, Font.NORMAL)));
tmp.setBorder(Rectangle.NO_BORDER);
tmp.setBorderWidth(0);
tmp.setPadding(0);
tmp.setUseBorderPadding(false);
tmp.setHorizontalAlignment(Element.ALIGN_LEFT);
head.addCell(tmp);
tmp = new PdfPCell(new Phrase("http://www.muellerpc.de", new Font(Font.FontFamily.TIMES_ROMAN, 8, Font.NORMAL)));
tmp.setBorder(Rectangle.NO_BORDER);
tmp.setBorderWidth(0);
tmp.setPadding(0);
tmp.setUseBorderPadding(false);
tmp.setHorizontalAlignment(Element.ALIGN_LEFT);
head.addCell(tmp);
tmp = new PdfPCell(new Phrase("0 67 41 / 20 74 06", new Font(Font.FontFamily.TIMES_ROMAN, 8, Font.NORMAL)));
tmp.setBorder(Rectangle.NO_BORDER);
tmp.setBorderWidth(0);
tmp.setPadding(0);
tmp.setUseBorderPadding(false);
tmp.setHorizontalAlignment(Element.ALIGN_LEFT);
head.addCell(tmp);
tmp = new PdfPCell(new Phrase("Volksbank RNH eG", new Font(Font.FontFamily.TIMES_ROMAN, 8, Font.NORMAL)));
tmp.setBorder(Rectangle.NO_BORDER);
tmp.setBorderWidth(0);
tmp.setPadding(0);
tmp.setUseBorderPadding(false);
tmp.setHorizontalAlignment(Element.ALIGN_LEFT);
head.addCell(tmp);
tmp = new PdfPCell(new Phrase("Rheingoldstr. 38a", new Font(Font.FontFamily.TIMES_ROMAN, 8, Font.NORMAL)));
tmp.setBorder(Rectangle.NO_BORDER);
tmp.setBorderWidth(0);
tmp.setPadding(0);
tmp.setUseBorderPadding(false);
tmp.setHorizontalAlignment(Element.ALIGN_LEFT);
head.addCell(tmp);
tmp = new PdfPCell(new Phrase("E-Mail:", new Font(Font.FontFamily.TIMES_ROMAN, 8, Font.NORMAL)));
tmp.setBorder(Rectangle.NO_BORDER);
tmp.setBorderWidth(0);
tmp.setPadding(0);
tmp.setUseBorderPadding(false);
tmp.setHorizontalAlignment(Element.ALIGN_LEFT);
head.addCell(tmp);
tmp = new PdfPCell(new Phrase("Mobil:", new Font(Font.FontFamily.TIMES_ROMAN, 8, Font.NORMAL)));
tmp.setBorder(Rectangle.NO_BORDER);
tmp.setBorderWidth(0);
tmp.setPadding(0);
tmp.setUseBorderPadding(false);
tmp.setHorizontalAlignment(Element.ALIGN_LEFT);
head.addCell(tmp);
tmp = new PdfPCell(new Phrase("BLZ 560 900 00", new Font(Font.FontFamily.TIMES_ROMAN, 8, Font.NORMAL)));
tmp.setBorder(Rectangle.NO_BORDER);
tmp.setBorderWidth(0);
tmp.setPadding(0);
tmp.setUseBorderPadding(false);
tmp.setHorizontalAlignment(Element.ALIGN_LEFT);
head.addCell(tmp);
tmp = new PdfPCell(new Phrase("56329 St. Goar", new Font(Font.FontFamily.TIMES_ROMAN, 8, Font.NORMAL)));
tmp.setBorder(Rectangle.NO_BORDER);
tmp.setBorderWidth(0);
tmp.setPadding(0);
tmp.setUseBorderPadding(false);
tmp.setHorizontalAlignment(Element.ALIGN_LEFT);
head.addCell(tmp);
tmp = new PdfPCell(new Phrase("kontakt@muellerpc.de", new Font(Font.FontFamily.TIMES_ROMAN, 8, Font.NORMAL)));
tmp.setBorder(Rectangle.NO_BORDER);
tmp.setBorderWidth(0);
tmp.setPadding(0);
tmp.setUseBorderPadding(false);
tmp.setHorizontalAlignment(Element.ALIGN_LEFT);
head.addCell(tmp);
tmp = new PdfPCell(new Phrase("01 52 / 09 80 91 92", new Font(Font.FontFamily.TIMES_ROMAN, 8, Font.NORMAL)));
tmp.setBorder(Rectangle.NO_BORDER);
tmp.setBorderWidth(0);
tmp.setPadding(0);
tmp.setUseBorderPadding(false);
tmp.setHorizontalAlignment(Element.ALIGN_LEFT);
head.addCell(tmp);
tmp = new PdfPCell(new Phrase("Konto-Nr. 1402917", new Font(Font.FontFamily.TIMES_ROMAN, 8, Font.NORMAL)));
tmp.setBorder(Rectangle.NO_BORDER);
tmp.setBorderWidth(0);
tmp.setPadding(0);
tmp.setUseBorderPadding(false);
tmp.setHorizontalAlignment(Element.ALIGN_LEFT);
head.addCell(tmp);
head.setTotalWidth(page.getWidth() - document.leftMargin() - document.rightMargin());
head.writeSelectedRows(0, -1, document.leftMargin(), document.bottomMargin()+50,
writer.getDirectContent());
}
catch (Exception e) {
throw new ExceptionConverter(e);
}
}
}