diff options
author | Andreas Fitzek <andreas.fitzek@iaik.tugraz.at> | 2014-03-13 08:52:46 +0100 |
---|---|---|
committer | Andreas Fitzek <andreas.fitzek@iaik.tugraz.at> | 2014-04-01 16:51:50 +0200 |
commit | 8d9cc698494694a57743c7443bf5e77d4e6c882b (patch) | |
tree | 6b7cf655ffdc0a8e92252adbf25ce5ae4fd41f1f /pdf-as-lib/src/test/java/at/gv/egiz/pdfas/lib | |
parent | 78c7b6b82267b766b573046d722a55638749a6cd (diff) | |
download | pdf-as-4-8d9cc698494694a57743c7443bf5e77d4e6c882b.tar.gz pdf-as-4-8d9cc698494694a57743c7443bf5e77d4e6c882b.tar.bz2 pdf-as-4-8d9cc698494694a57743c7443bf5e77d4e6c882b.zip |
WIP: pdfbox table to Sign AP
Diffstat (limited to 'pdf-as-lib/src/test/java/at/gv/egiz/pdfas/lib')
3 files changed, 233 insertions, 0 deletions
diff --git a/pdf-as-lib/src/test/java/at/gv/egiz/pdfas/lib/test/mains/CertificateHolderRequest.java b/pdf-as-lib/src/test/java/at/gv/egiz/pdfas/lib/test/mains/CertificateHolderRequest.java new file mode 100644 index 00000000..6d853f6e --- /dev/null +++ b/pdf-as-lib/src/test/java/at/gv/egiz/pdfas/lib/test/mains/CertificateHolderRequest.java @@ -0,0 +1,18 @@ +package at.gv.egiz.pdfas.lib.test.mains; +import iaik.x509.X509Certificate; +import at.gv.egiz.pdfas.lib.impl.status.ICertificateProvider; + + +public class CertificateHolderRequest implements ICertificateProvider { + + private X509Certificate cert; + + public CertificateHolderRequest(X509Certificate cert) { + this.cert = cert; + } + + public X509Certificate getCertificate() { + return cert; + } + +} diff --git a/pdf-as-lib/src/test/java/at/gv/egiz/pdfas/lib/test/mains/TestPDFBoxTable.java b/pdf-as-lib/src/test/java/at/gv/egiz/pdfas/lib/test/mains/TestPDFBoxTable.java new file mode 100644 index 00000000..cba9b927 --- /dev/null +++ b/pdf-as-lib/src/test/java/at/gv/egiz/pdfas/lib/test/mains/TestPDFBoxTable.java @@ -0,0 +1,207 @@ +package at.gv.egiz.pdfas.lib.test.mains; +import iaik.x509.X509Certificate; + +import java.awt.Graphics; +import java.awt.geom.AffineTransform; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.ArrayList; + +import org.apache.pdfbox.cos.COSName; +import org.apache.pdfbox.pdmodel.PDDocument; +import org.apache.pdfbox.pdmodel.PDPage; +import org.apache.pdfbox.pdmodel.common.PDRectangle; +import org.apache.pdfbox.pdmodel.common.PDStream; +import org.apache.pdfbox.pdmodel.edit.PDPageContentStream; +import org.apache.pdfbox.pdmodel.font.PDFont; +import org.apache.pdfbox.pdmodel.font.PDType1Font; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import at.gv.egiz.pdfas.common.settings.ISettings; +import at.gv.egiz.pdfas.common.settings.SignatureProfileSettings; +import at.gv.egiz.pdfas.lib.api.PdfAs; +import at.gv.egiz.pdfas.lib.api.PdfAsFactory; +import at.gv.egiz.pdfas.lib.impl.stamping.TableFactory; +import at.knowcenter.wag.egov.egiz.table.Entry; +import at.knowcenter.wag.egov.egiz.table.Style; +import at.knowcenter.wag.egov.egiz.table.Table; + +public class TestPDFBoxTable { + + private static final Logger logger = LoggerFactory + .getLogger(TestPDFBoxTable.class); + + private static void drawTable(PDPage page, PDPageContentStream contentStream, + float x, float y, Table abstractTable) throws IOException { + + final int rows = abstractTable.getRows().size(); + final int cols = abstractTable.getMaxCols(); + float[] colsSizes = abstractTable.getColsRelativeWith(); + int max_cols = abstractTable.getMaxCols(); + if (colsSizes == null) + { + colsSizes = new float[max_cols]; + // set the column ratio for all columns to 1 + for (int cols_idx = 0; cols_idx < colsSizes.length; cols_idx++) + { + colsSizes[cols_idx] = 1; + } + } + + logger.info("TOTAL Col: " + abstractTable.getWidth()); + + float total = 0; + + for (int cols_idx = 0; cols_idx < colsSizes.length; cols_idx++) + { + total += colsSizes[cols_idx]; + } + + for (int cols_idx = 0; cols_idx < colsSizes.length; cols_idx++) + { + colsSizes[cols_idx] = (colsSizes[cols_idx]/total) * abstractTable.getWidth(); + } + + for (int cols_idx = 0; cols_idx < colsSizes.length; cols_idx++) + { + logger.info("Col: " + cols_idx + " : " + colsSizes[cols_idx]); + } + + final float cellMargin=5f; + final float rowHeight = 12f + 2 * cellMargin; + final float tableWidth = abstractTable.getWidth(); + final float tableHeight = rowHeight * rows; + final float colWidth = tableWidth/(float)cols; + + //draw the rows + float nexty = y ; + for (int i = 0; i <= rows; i++) { + contentStream.drawLine(x, nexty, x+tableWidth, nexty); + nexty-= rowHeight; + } + + //draw the columns + float nextx = x; + for (int i = 0; i <= cols; i++) { + contentStream.drawLine(nextx, y, nextx, y-tableHeight); + if(i < colsSizes.length) { + nextx += (colsSizes != null) ? colsSizes[i] : colWidth; + } + } + + float textx = x+cellMargin; + float texty = y-15; + for(int i = 0; i < abstractTable.getRows().size(); i++){ + ArrayList row = (ArrayList) abstractTable.getRows().get(i); + for(int j = 0 ; j < row.size(); j++) { + Entry cell = (Entry) row.get(j); + String text = cell.toString(); + text = "Hallo"; + COSName name = COSName.getPDFName("ANDI_TAG!"); + contentStream.beginMarkedContentSequence(COSName.ALT, name); + contentStream.beginText(); + logger.info("Writing: " + textx + " : " + texty + " = " + text); + contentStream.moveTextPositionByAmount(textx,texty); + + if (text.contains("\n")) { + String[] lines = text.split("\n"); + contentStream.appendRawCommands(10 + " TL\n"); + for (int k = 0; k < lines.length; k++) { + contentStream.drawString(lines[k]); + if (k < lines.length - 1) { + contentStream.appendRawCommands("T*\n"); + } + } + } else { + contentStream.drawString(text); + } + contentStream.endText(); + contentStream.endMarkedContentSequence(); + textx += (colsSizes != null) ? colsSizes[j] : colWidth; + } + texty-= rowHeight; + textx = x+cellMargin; + } + } + + + private static void renderTable(Table abstractTable) { + + ArrayList rows = abstractTable.getRows(); + Style table_style = abstractTable.getStyle(); + for (int row_idx = 0; row_idx < rows.size(); row_idx++) + { + @SuppressWarnings("rawtypes") + ArrayList row = (ArrayList) rows.get(row_idx); + logger.info("## Row:" + row_idx + " ## of table:" + abstractTable.getName()); + for (int entry_idx = 0; entry_idx < row.size(); entry_idx++) + { + Entry cell = (Entry) row.get(entry_idx); + // 03.11.2010 changed by exthex - swapped the two params, was probably a bug + Style inherit_style = Style.doInherit(table_style, cell.getStyle()); + cell.setStyle(inherit_style); + logger.info(cell.toString()); + /*PdfPCell pdf_cell = renderCell(cell); + if (cell.getColSpan() > 1) + { + pdf_cell.setColspan(cell.getColSpan()); + } + if (cell.isNoWrap()) + { + pdf_cell.setNoWrap(true); + }*/ + // System.err.println("valign:" + pdf_cell.getVerticalAlignment() + " + // halign:" + + // pdf_cell.getHorizontalAlignment()); + //pdf_table.addCell(pdf_cell); + } + //pdf_table.completeRow(); + } + logger.info("render table:" + abstractTable.getName()); + } + + public static void main(String[] args) { + try { + PdfAs pdfAs = PdfAsFactory.createPdfAs(new File("/home/afitzek/.pdfas/")); + ISettings settings = (ISettings) pdfAs.getConfiguration(); + SignatureProfileSettings profileSettings = + TableFactory.createProfile("SIGNATURBLOCK_DE", settings); + + X509Certificate cert = new X509Certificate(new FileInputStream("/home/afitzek/qualified.cer")); + + CertificateHolderRequest request = new CertificateHolderRequest(cert); + + Table main = TableFactory.createSigTable(profileSettings, "main", settings, request); + + main.setWidth(400); + + renderTable(main); + + PDStream stream1; + + PDDocument document = new PDDocument(); + PDPage page = new PDPage(); + page.setMediaBox(new PDRectangle()); + PDPageContentStream stream = new PDPageContentStream(document, page); + stream.setFont(PDType1Font.HELVETICA_BOLD , 12); + drawTable(page, stream, 100, 300, main); + stream.close(); + + document.addPage(page); + + document.save("/tmp/test.pdf"); + + /* + FileOutputStream fos = new FileOutputStream("/tmp/buffer.bin"); + fos.write(page.getContents().getByteArray()); + fos.close(); + */ + + } catch(Throwable e) { + e.printStackTrace(); + } + } +} diff --git a/pdf-as-lib/src/test/java/at/gv/egiz/pdfas/lib/test/mains/package-info.java b/pdf-as-lib/src/test/java/at/gv/egiz/pdfas/lib/test/mains/package-info.java new file mode 100644 index 00000000..60f666c8 --- /dev/null +++ b/pdf-as-lib/src/test/java/at/gv/egiz/pdfas/lib/test/mains/package-info.java @@ -0,0 +1,8 @@ +/** + * + */ +/** + * @author afitzek + * + */ +package at.gv.egiz.pdfas.lib.test.mains;
\ No newline at end of file |