aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/knowcenter/wag/egov/egiz/test/BinSig.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/at/knowcenter/wag/egov/egiz/test/BinSig.java')
-rw-r--r--src/main/java/at/knowcenter/wag/egov/egiz/test/BinSig.java345
1 files changed, 345 insertions, 0 deletions
diff --git a/src/main/java/at/knowcenter/wag/egov/egiz/test/BinSig.java b/src/main/java/at/knowcenter/wag/egov/egiz/test/BinSig.java
new file mode 100644
index 0000000..612ada1
--- /dev/null
+++ b/src/main/java/at/knowcenter/wag/egov/egiz/test/BinSig.java
@@ -0,0 +1,345 @@
+/**
+ * <copyright> Copyright (c) 2006 by Know-Center, Graz, Austria </copyright>
+ *
+ * This software is the confidential and proprietary information of Know-Center,
+ * Graz, Austria. You shall not disclose such Confidential Information and shall
+ * use it only in accordance with the terms of the license agreement you entered
+ * into with Know-Center.
+ *
+ * KNOW-CENTER MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
+ * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
+ * NON-INFRINGEMENT. KNOW-CENTER SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY
+ * LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
+ * DERIVATIVES.
+ *
+ * $Id: BinSig.java,v 1.1 2006/10/31 08:19:52 wprinz Exp $
+ */
+package at.knowcenter.wag.egov.egiz.test;
+
+import java.awt.Color;
+import java.io.ByteArrayOutputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+import at.knowcenter.wag.egov.egiz.exceptions.PDFDocumentException;
+import at.knowcenter.wag.egov.egiz.pdf.Placeholder;
+import at.knowcenter.wag.egov.egiz.pdf.SplitStrings;
+import at.knowcenter.wag.egov.egiz.pdf.StringInfo;
+
+import com.lowagie.text.BadElementException;
+import com.lowagie.text.Chunk;
+import com.lowagie.text.Document;
+import com.lowagie.text.DocumentException;
+import com.lowagie.text.Font;
+import com.lowagie.text.FontFactory;
+import com.lowagie.text.Paragraph;
+import com.lowagie.text.Phrase;
+import com.lowagie.text.Rectangle;
+import com.lowagie.text.pdf.BaseFont;
+import com.lowagie.text.pdf.PdfContentByte;
+import com.lowagie.text.pdf.PdfIndirectReference;
+import com.lowagie.text.pdf.PdfPCell;
+import com.lowagie.text.pdf.PdfPTable;
+import com.lowagie.text.pdf.PdfTemplate;
+import com.lowagie.text.pdf.PdfWriter;
+
+/**
+ * @author wprinz
+ */
+public class BinSig
+{
+
+ public static final String KEY_DATE = "Datum ÖÄÜ:";
+
+ public static final String KEY_SN = "Seriennummer öäü:";
+
+ public static final String KEY_SIGVAL = "Signaturwert ß:";
+
+ public static final String KEY_ISSUER = "Aussteller .,!?:";
+
+ /**
+ * @param args
+ * @throws DocumentException
+ * @throws IOException
+ * @throws PDFDocumentException
+ */
+ public static void main(String[] args) throws DocumentException, IOException, PDFDocumentException
+ {
+ BinSig bs = new BinSig();
+ bs.action();
+ }
+
+ public void action() throws IOException, DocumentException, PDFDocumentException
+ {
+ Document.compress = false;
+
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+
+ Document document = new Document();
+ PdfWriter writer = PdfWriter.getInstance(document, baos);
+
+ document.open();
+
+ Paragraph p1 = new Paragraph(new Chunk("This is my first paragraph. ", FontFactory.getFont(FontFactory.HELVETICA, 10)));
+ p1.add("The leading of this paragraph is calculated automagically. ");
+ p1.add("The default leading is 1.5 times the fontsize. ");
+ p1.add(new Chunk("You can add chunks "));
+ p1.add(new Phrase("or you can add phrases. "));
+ p1.add(new Phrase("Unless you change the leading with the method setLeading, the leading doesn't change if you add text with another leading. This can lead to some problems.", FontFactory.getFont(FontFactory.HELVETICA, 18)));
+ document.add(p1);
+ Paragraph p2 = new Paragraph(new Phrase("This is my second paragraph. ", FontFactory.getFont(FontFactory.HELVETICA, 12)));
+ p2.add("As you can see, it started on a new line.");
+ document.add(p2);
+ Paragraph p3 = new Paragraph("This is my third paragraph.", FontFactory.getFont(FontFactory.HELVETICA, 12));
+ document.add(p3);
+
+ PdfContentByte cb = writer.getDirectContent();
+
+ // Font tableFont = FontFactory.getFont(FontFactory.COURIER,
+ // Font.DEFAULTSIZE, Font.NORMAL);
+ // Font tableFont = FontFactory.getFont(FontFactory.HELVETICA,
+ // Font.DEFAULTSIZE, Font.NORMAL);
+
+ BaseFont bf = BaseFont.createFont(BaseFont.COURIER, BaseFont.WINANSI, false);
+ Font tableFont = new Font(bf, 12);
+
+ float padding = 0f;
+ Rectangle border = new Rectangle(0f, 0f);
+ border.setBorder(Rectangle.BOX);
+ border.setBorderWidth(1);
+ border.setBorderColor(Color.BLACK);
+ border.setBackgroundColor(new Color(.75f, .75f, 1f));
+ PdfPTable table = makeTestPage(tableFont, border, padding, true, true);
+
+ float width = 400;
+ table.setTotalWidth(width);
+ float height = table.getTotalHeight();
+
+ PdfTemplate template = cb.createTemplate(width, height);
+
+ // we add some graphics
+ // template.moveTo(0, 200);
+ // template.lineTo(500, 0);
+ // template.rectangle(0, 0, width, height);
+ // template.setLineWidth(3);
+ // template.stroke();
+
+ table.writeSelectedRows(0, -1, 0, table.getTotalHeight(), template);
+
+ // we add some text
+ // template.beginText();
+ // template.setFontAndSize(BaseFont.createFont(BaseFont.HELVETICA,
+ // BaseFont.CP1252, BaseFont.NOT_EMBEDDED), 12);
+ // template.setTextMatrix(0, 0);
+ // template.showText("Text at the position 0,0 (relative to the
+ // template!)");
+ // template.endText();
+
+ // we add the template on different positions
+ cb.addTemplate(template, 100, 500);
+
+ PdfIndirectReference template_ref = template.getIndirectReference();
+ System.out.println("template = " + template_ref.getNumber() + " " + template_ref.getGeneration());
+
+ document.close();
+
+ baos.close();
+ byte[] pdf = baos.toByteArray();
+
+ // not the best solution to parse the stream, but work's for now.
+ String pdf_string = new String(pdf, "ISO-8859-1");
+ int obj_start = pdf_string.lastIndexOf(template_ref.getNumber() + " " + template_ref.getGeneration() + " obj");
+ int stream_start = pdf_string.indexOf("stream\n", obj_start) + 7;
+ int stream_end = pdf_string.indexOf("endstream\nendobj", stream_start);
+ int stream_len = stream_end - stream_start;
+ byte[] stream = new byte[stream_len];
+ System.arraycopy(pdf, stream_start, stream, 0, stream_len);
+
+ String stream_string = new String(stream, "ISO-8859-1");
+ System.out.println(stream_string);
+
+ List strings = Placeholder.parseStrings(pdf, stream_start, stream_end);
+
+ List date = new ArrayList();
+ List sn = new ArrayList();
+ List sigval = new ArrayList();
+ List issuer = new ArrayList();
+
+ List[] partitions = new List[] { date, sn, sigval, issuer };
+
+ Iterator it = strings.iterator();
+ int cur_list = -1;
+ while (it.hasNext())
+ {
+ StringInfo si = (StringInfo) it.next();
+
+ String str = si.getString("ISO-8859-1");
+
+ if (str.equals(KEY_DATE))
+ {
+ cur_list = 0;
+ continue;
+ }
+ if (str.equals(KEY_SN))
+ {
+ cur_list = 1;
+ continue;
+ }
+ if (str.equals(KEY_SIGVAL))
+ {
+ cur_list = 2;
+ continue;
+ }
+ if (str.equals(KEY_ISSUER))
+ {
+ cur_list = 3;
+ continue;
+ }
+
+ partitions[cur_list].add(si);
+ }
+
+ String date_orig = "The Date doesn't make problems.";
+ String sn_orig = "0123456789abcdefghijklmnopqrstuvwxyz0123456789";
+ String sigval_orig = "ashdjklsbfkabsflkbadslkfblasdkfhajösdhfkjlasdhfkljasdbfljkasdhfljkasdhfljkasdhfasdfhj.";
+ String issuer_orig = "CN=Republik Österreich, \\\\O=Bund irgendwas, C=ÖÄÜsterreich, N=ein paar \\worte ohne (sinnn und) verstand #+ßÜ,ÄÖÜ;äöü";
+ BinSig.replacePlaceholder(new SplitStrings(pdf, date), date_orig.getBytes("ISO-8859-1"));
+ BinSig.replacePlaceholder(new SplitStrings(pdf, sn), sn_orig.getBytes("ISO-8859-1"));
+ BinSig.replacePlaceholder(new SplitStrings(pdf, sigval), sigval_orig.getBytes("ISO-8859-1"));
+ Placeholder.replacePlaceholderWithTolerance(pdf, issuer, issuer_orig.getBytes("ISO-8859-1"), 10);
+ // replacePlaceholders(pdf, "CN=Seppl Maximilian Hötürälß, ÖÜÄß", (byte)
+ // 'Y');
+
+ byte [] enc = { 'w', 'i', 'n' };
+ String date_rec = Placeholder.reconstructStringFromPartition(pdf, date, enc);
+ String sn_rec = Placeholder.reconstructStringFromPartition(pdf, sn, enc);
+ String sigval_rec = Placeholder.reconstructStringFromPartition(pdf, sigval, enc);
+ String issuer_rec = Placeholder.reconstructStringFromPartition(pdf, issuer, enc);
+
+ System.out.println("date ok = " + date_orig.equals(date_rec));
+ System.out.println("sn ok = " + sn_orig.equals(sn_rec));
+ System.out.println("sigval ok = " + sigval_orig.equals(sigval_rec));
+ System.out.println("issuer ok = " + issuer_orig.equals(issuer_rec));
+
+ FileOutputStream fos = new FileOutputStream("C:\\test.pdf");
+ fos.write(pdf);
+ fos.close();
+
+ System.out.println("finished");
+
+ }
+
+ private static PdfPTable makeTestPage(Font tableFont, Rectangle borders,
+ float padding, boolean ascender, boolean descender) throws BadElementException, DocumentException, IOException
+ {
+ // document.newPage();
+ PdfPTable table = null;
+ table = new PdfPTable(2);
+ table.setWidthPercentage(100f);
+
+ table.setWidths(new float[] { 30, 70 });
+
+ table.addCell(makeNormalCell(KEY_DATE));
+ table.addCell(makeValueCell("DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD"));
+
+ table.addCell(makeNormalCell(KEY_SN));
+ table.addCell(makeValueCell("SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS"));
+
+ table.addCell(makeNormalCell(KEY_SIGVAL));
+ table.addCell(makeValueCell("WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW"));
+
+ table.addCell(makeNormalCell(KEY_ISSUER));
+ table.addCell(makeValueCell("IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII"));
+
+ return table;
+ }
+
+ protected static PdfPCell makeCell(String text, int vAlignment,
+ int hAlignment, Font font, float leading, float padding,
+ Rectangle borders, boolean ascender, boolean descender)
+ {
+ Paragraph p = new Paragraph(text, font);
+ p.setLeading(leading);
+
+ PdfPCell cell = new PdfPCell(p);
+ cell.setLeading(leading, 0);
+ cell.setVerticalAlignment(vAlignment);
+ cell.setHorizontalAlignment(hAlignment);
+ cell.cloneNonPositionParameters(borders);
+ cell.setUseAscender(ascender);
+ cell.setUseDescender(descender);
+ cell.setUseBorderPadding(true);
+ cell.setPadding(padding);
+ return cell;
+ }
+
+ private static PdfPCell makeNormalCell(String text)
+ {
+
+ Paragraph p = new Paragraph(text);
+
+ PdfPCell cell = new PdfPCell(p);
+ cell.setBorder(PdfPCell.BOX);
+ cell.setBorderColor(Color.BLUE);
+ cell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
+
+ return cell;
+ }
+
+ private static PdfPCell makeValueCell(String placeholder) throws DocumentException, IOException
+ {
+ BaseFont bf = BaseFont.createFont(BaseFont.COURIER, BaseFont.WINANSI, false);
+ Font ff = new Font(bf, 12);
+
+ Paragraph p = new Paragraph(placeholder, ff);
+
+ PdfPCell cell = new PdfPCell(p);
+ cell.setBorder(PdfPCell.BOX);
+ cell.setBorderColor(Color.RED);
+
+ return cell;
+ }
+
+ protected boolean isPlaceholder(byte[] pdf, StringInfo si, byte placeholder)
+ {
+ for (int i = si.string_start; i < si.string_start + si.string_length; i++)
+ {
+ if (pdf[i] != placeholder)
+ {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ /**
+ * Replaces the given placeholders with the given bytes.
+ * @param ss The SplitStrings placeholders.
+ * @param replace_bytes The bytes to be filled in.
+ */
+ public static void replacePlaceholder(SplitStrings ss, byte[] replace_bytes)
+ {
+
+ for (int i = 0; i < replace_bytes.length; i++)
+ {
+ byte[] data = Placeholder.escapeByte(replace_bytes[i]);
+ if (!ss.fits(data))
+ {
+ ss.newline();
+ }
+ ss.write(data);
+ }
+ ss.fillRest();
+
+ // if (replace_read < replace_bytes.length)
+ // {
+ // System.err.println("The replace string was longer than the reserved placeholder.");
+ // throw new PlaceholderException(null, replace_bytes.length - replace_read);
+ // }
+ }
+
+}