From a0de2a3b2a5f4a99f280f5caebbca0d183ae109a Mon Sep 17 00:00:00 2001 From: tknall Date: Wed, 7 Feb 2007 10:08:21 +0000 Subject: Bugfix: Querformat, BKU 2.7.x, ... git-svn-id: https://joinup.ec.europa.eu/svn/pdf-as/trunk@35 7b5415b0-85f9-ee4d-85bd-d5d0c3b42d1c --- .../java/at/knowcenter/wag/egov/egiz/PdfAS.java | 245 ++++++++++++++++----- 1 file changed, 196 insertions(+), 49 deletions(-) (limited to 'src/main/java/at/knowcenter/wag/egov/egiz/PdfAS.java') diff --git a/src/main/java/at/knowcenter/wag/egov/egiz/PdfAS.java b/src/main/java/at/knowcenter/wag/egov/egiz/PdfAS.java index 0987b2d..f9ab652 100644 --- a/src/main/java/at/knowcenter/wag/egov/egiz/PdfAS.java +++ b/src/main/java/at/knowcenter/wag/egov/egiz/PdfAS.java @@ -46,6 +46,7 @@ import at.knowcenter.wag.egov.egiz.pdf.IncrementalUpdateInformation; import at.knowcenter.wag.egov.egiz.pdf.PDFSignatureCreation; import at.knowcenter.wag.egov.egiz.pdf.PDFSignatureObject; import at.knowcenter.wag.egov.egiz.pdf.PDFUtilities; +import at.knowcenter.wag.egov.egiz.pdf.PositioningInstruction; import at.knowcenter.wag.egov.egiz.pdf.SignatureHolder; import at.knowcenter.wag.egov.egiz.pdf.TextualSignatureHolder; import at.knowcenter.wag.egov.egiz.pdf.TablePos; @@ -57,6 +58,7 @@ import at.knowcenter.wag.egov.egiz.sig.SignatureResponse; import at.knowcenter.wag.egov.egiz.sig.SignatureTypeDefinition; import at.knowcenter.wag.egov.egiz.sig.SignatureTypes; import at.knowcenter.wag.egov.egiz.tools.Normalizer; +import at.knowcenter.wag.exactparser.ParseDocument; import at.knowcenter.wag.exactparser.parsing.PDFUtils; import at.knowcenter.wag.exactparser.parsing.results.HeaderParseResult; @@ -152,7 +154,8 @@ public abstract class PdfAS * is not 1.4. * *

- * If strict mode is deactivated, this does simply nothing. + * Furthermore (independently of strict mode) the PDF is checked for + * parsability. *

* * @param pdf @@ -168,6 +171,15 @@ public abstract class PdfAS throw new PDFDocumentException(201, "StrictMode: The pdf version is not 1.4 or lower."); } } + try + { + ParseDocument.parseDocument(pdf); + } + catch (Exception e) + { + logger_.debug("Error while parsing Document.", e); + throw new PDFDocumentException(201, e); + } } /** @@ -232,7 +244,7 @@ public abstract class PdfAS * @throws SignatureException * F.e. * @throws SignatureTypesException - * @throws NormalizeException + * @throws NormalizeException */ public static List extractSignatureHoldersTextual(String raw_text, boolean old_style) throws PDFDocumentException, SignatureException, SignatureTypesException, NormalizeException @@ -267,7 +279,7 @@ public abstract class PdfAS * found. * @throws SignatureException * @throws SignatureTypesException - * @throws NormalizeException + * @throws NormalizeException */ public static SignatureHolder extractSignatureHolderTextual(String raw_text, boolean old_style) throws SignatureException, SignatureTypesException, NormalizeException @@ -343,7 +355,8 @@ public abstract class PdfAS end_index = cur_key.getStartIndex(); } - // this normalization is required to get rid of possible trailing newlines. + // this normalization is required to get rid of possible trailing + // newlines. String normalized_text = normalizeText(signed_text); SignatureHolder holder = new TextualSignatureHolder(normalized_text, signatureObject_); return holder; @@ -368,8 +381,6 @@ public abstract class PdfAS public static List findBlockInText(String text, SignatureTypeDefinition sig_type_def, boolean old_style) { - // FIXME[tknall]: AbsoluteTextSignature.java, method findEndOfValue(...) does not work properly for landscape documents because start_index of FoundKeys are not set correctly. - // Hint: Captions and values of landscape documents are separated with " \n" and not only with " ". Vector keys = sig_type_def.getRevertSortedKeys(); Vector captions = sig_type_def.getRevertSortedCaptions(); @@ -389,11 +400,12 @@ public abstract class PdfAS String caption = (String) captions.get(key_idx); - //int found_index = text.lastIndexOf(caption); -// we're searching for captions that start at the beginning of the line. - int found_index = text.lastIndexOf("\n" + caption) + 1; // the +1 compensates the \n - - + // int found_index = text.lastIndexOf(caption); + // we're searching for captions that start at the beginning of the line. + int found_index = text.lastIndexOf("\n" + caption) + 1; // the +1 + // compensates the + // \n + if (key.equals(SignatureTypes.SIG_ID)) { if (found_index < 0 || found_index >= last_index) @@ -453,7 +465,7 @@ public abstract class PdfAS } }); } - + /** * Sorts the FoundKeys List ascendingly according to the start indices of the * found keys (the first found key in the list will have the lowest start @@ -475,7 +487,7 @@ public abstract class PdfAS return fk0.start_index - fk1.start_index; } }); - } + } /** * Checks that the found keys are in correct order regarding SIG_ID as @@ -763,7 +775,7 @@ public abstract class PdfAS String connector) throws NormalizeException, PDFDocumentException, SignatureException { String text_to_be_verified = signature_holder.getSignedText(); - + SignatureObject so_to_be_verified = signature_holder.getSignatureObject(); if (text_to_be_verified == null) @@ -774,7 +786,7 @@ public abstract class PdfAS { throw new SignatureException(311, "Document can not be verified because the length of the text to be verified is 0. (length = " + text_to_be_verified.length() + ")"); } - + if (so_to_be_verified == null) { throw new SignatureException(312, "Document can not be verified because no signature object are set."); @@ -816,8 +828,9 @@ public abstract class PdfAS final String user_name, final String user_password) throws SignatureException, PDFDocumentException { logger_.info("User signed a document: " + user_name); - - if (text_to_sign == null) { + + if (text_to_sign == null) + { throw new SignatureException(301, "Signature can not be produced. Text is null."); } if (text_to_sign.length() <= 0) @@ -907,10 +920,10 @@ public abstract class PdfAS */ public static String extractNormalizedTextTextual(final byte[] pdf) throws PresentableException { -// ByteArrayInputStream bais = new ByteArrayInputStream(pdf); -// String raw_document_text = TextualSignature.extractTextTextual(bais); -// -// String document_text = normalizeText(raw_document_text); + // ByteArrayInputStream bais = new ByteArrayInputStream(pdf); + // String raw_document_text = TextualSignature.extractTextTextual(bais); + // + // String document_text = normalizeText(raw_document_text); return extractNormalizedTextTextual(pdf, pdf.length); } @@ -927,7 +940,8 @@ public abstract class PdfAS * @throws PresentableException * F.e. */ - public static String extractNormalizedTextTextual(final byte[] pdf, final int length) throws PresentableException + public static String extractNormalizedTextTextual(final byte[] pdf, + final int length) throws PresentableException { ByteArrayInputStream bais = new ByteArrayInputStream(pdf, 0, length); String raw_document_text = TextualSignature.extractTextTextual(bais); @@ -974,6 +988,68 @@ public abstract class PdfAS return pdf_table; } + /** + * Evalutates absolute positioning and prepares the PositioningInstruction for + * placing the table. + * + * @param pos + * The absolute positioning parameter. If null it is sought in the + * profile definition. + * @param signature_type + * The profile definition of the table to be written. + * @param pdf + * The pdf. + * @param pdf_table + * The pdf table to be written. + * @return Returns the PositioningInformation. + * @throws PDFDocumentException + * F.e. + * @throws SettingsException + * F.e. + */ + public static PositioningInstruction determineTablePositioning(TablePos pos, + String signature_type, byte[] pdf, PdfPTable pdf_table) throws PDFDocumentException, SettingsException + { + if (pos == null) + { + String pos_string = SettingsReader.getInstance().getSetting(SignatureTypes.SIG_OBJ + signature_type + ".pos", null); + if (pos_string != null) + { + pos = PdfAS.parsePositionFromPosString(pos_string); + } + } + + if (pos == null) + { + // The automatic algorithm. + return PdfAS.adjustTableAndCalculatePosition(pdf, pdf_table); + } + + if (pos.page == -2) + { + // The automatic algorithm regarding the footer. + return PdfAS.adjustTableAndCalculatePositionRegardingFooter(pdf, pdf_table, pos.footer_line); + } + + boolean make_new_page = false; + int page = pos.page; + + if (pos.page == -1) + { + // Absolute positioning on a new page. + make_new_page = true; + PdfReader reader = PdfAS.readInPdfDocument(pdf); + page = reader.getNumberOfPages() + 1; + reader = null; + } + + pdf_table.setTotalWidth(pos.width); + pdf_table.setLockedWidth(true); + + return new PositioningInstruction(make_new_page, page, pos.pos_x, pos.pos_y); + + } + /** * Sets the width of the table according to the layout of the document and * calculates the y position where the PDFPTable should be placed. @@ -986,36 +1062,88 @@ public abstract class PdfAS * @throws PDFDocumentException * F.e. */ - public static TablePos adjustTableAndCalculatePosition(final byte[] pdf, - PdfPTable pdf_table) throws PDFDocumentException + public static PositioningInstruction adjustTableAndCalculatePosition( + final byte[] pdf, PdfPTable pdf_table) throws PDFDocumentException { - TablePos pos = new TablePos(); + boolean make_new_page = false; PdfReader reader = readInPdfDocument(pdf); - Rectangle psize = reader.getPageSizeWithRotation(reader.getNumberOfPages()); + int page = reader.getNumberOfPages(); + Rectangle psize = reader.getPageSizeWithRotation(page); float page_width = psize.width(); float page_height = psize.height(); - pos.width = page_width - SIGNATURE_BORDER; - pdf_table.setTotalWidth(pos.width); + final float width = page_width - SIGNATURE_BORDER; + pdf_table.setTotalWidth(width); + pdf_table.setLockedWidth(true); + + final float pos_x = SIGNATURE_BORDER / 2; + + final float table_height = pdf_table.getTotalHeight(); + final float page_length = PDFUtilities.calculateLastPageLength(pdf, page_height); + float pos_y = page_height - page_length - SIGNATURE_MARGIN; + + if (pos_y <= table_height) + { + make_new_page = true; + page++; + + pos_y = page_height - SIGNATURE_BORDER / 2; + } + + return new PositioningInstruction(make_new_page, page, pos_x, pos_y); + } + + /** + * Sets the width of the table according to the layout of the document and + * calculates the y position where the PDFPTable should be placed. + * + *

+ * This algorithm tries to position the table between the end of the text and + * the footer line. + *

+ * + * @param pdf + * The PDF document. + * @param pdf_table + * The PDFPTable to be placed. + * @return Returns the position where the PDFPTable should be placed. + * @throws PDFDocumentException + * F.e. + */ + public static PositioningInstruction adjustTableAndCalculatePositionRegardingFooter( + final byte[] pdf, PdfPTable pdf_table, float footer_line) throws PDFDocumentException + { + boolean make_new_page = false; + + PdfReader reader = readInPdfDocument(pdf); + + int page = reader.getNumberOfPages(); + Rectangle psize = reader.getPageSizeWithRotation(page); + float page_width = psize.width(); + float page_height = psize.height(); + + final float width = page_width - SIGNATURE_BORDER; + pdf_table.setTotalWidth(width); pdf_table.setLockedWidth(true); - pos.pos_x = SIGNATURE_BORDER / 2; + final float pos_x = SIGNATURE_BORDER / 2; + + final float table_height = pdf_table.getTotalHeight(); - float table_height = pdf_table.getTotalHeight(); - float page_length = PDFUtilities.calculateLastPageLength(pdf); - pos.pos_y = page_height - page_length - SIGNATURE_MARGIN; + final float page_length = PDFUtilities.calculateLastPageLength(pdf, page_height - footer_line); + float pos_y = page_height - page_length - SIGNATURE_MARGIN; - pos.page = reader.getNumberOfPages(); - if (pos.pos_y <= table_height) + if (pos_y - footer_line <= table_height) { - pos.page = -1; - // negative means to add a new page - pos.pos_y = page_height - SIGNATURE_BORDER / 2; + make_new_page = true; + page++; + + pos_y = page_height - SIGNATURE_BORDER / 2; } - return pos; + return new PositioningInstruction(make_new_page, page, pos_x, pos_y); } /** @@ -1055,27 +1183,46 @@ public abstract class PdfAS public static TablePos parsePositionFromPosString(String pos_string) throws PDFDocumentException { String[] strs = pos_string.split(";"); - if (strs.length != 4) - { - throw new PDFDocumentException(224, "Pos string (=" + pos_string + ") is invalid."); - } try { TablePos pos = new TablePos(); pos.page = Integer.parseInt(strs[0]); - pos.pos_x = Float.parseFloat(strs[1]); - pos.pos_y = Float.parseFloat(strs[2]); - pos.width = Float.parseFloat(strs[3]); - if (pos.page < 1 && pos.page != -1) + if (pos.page < 1 && pos.page != -1 && pos.page != -2) { - throw new PDFDocumentException(225, "pos.page (=" + pos.page + ") must not be lower than -1 and must not be 0."); + throw new PDFDocumentException(225, "Page (=" + pos.page + ") must not be lower than -2 and must not be 0."); } - if (pos.width <= 0.0f) + if (pos.page == -2) { - throw new PDFDocumentException(226, "pos.width (=" + pos.width + ") must not be lower or equal 0."); + if (strs.length != 2) + { + throw new PDFDocumentException(224, "Pos string (=" + pos_string + ") is invalid."); + } + + pos.footer_line = Float.parseFloat(strs[1]); + } + else + { + if (strs.length != 4) + { + throw new PDFDocumentException(224, "Pos string (=" + pos_string + ") is invalid."); + } + + pos.pos_x = Float.parseFloat(strs[1]); + pos.pos_y = Float.parseFloat(strs[2]); + pos.width = Float.parseFloat(strs[3]); + + if (pos.page < 1 && pos.page != -1) + { + throw new PDFDocumentException(225, "pos.page (=" + pos.page + ") must not be lower than -1 and must not be 0."); + } + + if (pos.width <= 0.0f) + { + throw new PDFDocumentException(226, "pos.width (=" + pos.width + ") must not be lower or equal 0."); + } } return pos; -- cgit v1.2.3