From ec7f8a2bfb3acfb2b0f9ec16ad7003e719a179f2 Mon Sep 17 00:00:00 2001 From: knowcenter Date: Fri, 2 Mar 2007 08:55:34 +0000 Subject: new positioning of signature table git-svn-id: https://joinup.ec.europa.eu/svn/pdf-as/trunk@45 7b5415b0-85f9-ee4d-85bd-d5d0c3b42d1c --- .../java/at/knowcenter/wag/egov/egiz/PdfAS.java | 175 ++++++++++++--------- 1 file changed, 104 insertions(+), 71 deletions(-) (limited to 'src/main/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 f9ab652..59a4987 100644 --- a/src/main/java/at/knowcenter/wag/egov/egiz/PdfAS.java +++ b/src/main/java/at/knowcenter/wag/egov/egiz/PdfAS.java @@ -1018,38 +1018,118 @@ public abstract class PdfAS pos = PdfAS.parsePositionFromPosString(pos_string); } } - if (pos == null) { - // The automatic algorithm. - return PdfAS.adjustTableAndCalculatePosition(pdf, pdf_table); + // The default algorithm. x,y,w =auto ,p=lastpage, f:ignored because y:auto + pos = new TablePos(); } - - if (pos.page == -2) + //System.out.println("Tablepos="+pos); + return PdfAS.adjustSignatureTableandCalculatePosition(pdf, pdf_table, pos); + } + /** + * Sets the width of the table according to the layout of the document and + * calculates the y position where the PDFPTable should be placed. + * + * @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 adjustSignatureTableandCalculatePosition( + final byte[] pdf, PdfPTable pdf_table, TablePos pos) throws PDFDocumentException + { + //first check pageinstruction in TablePos-object + //new,auto,absolut + PdfReader reader = readInPdfDocument(pdf); + //get pages of currentdocument + int doc_pages = reader.getNumberOfPages(); + int page = doc_pages; + if(!(pos.isNewPage() || pos.isPauto())) + { + //we should posit signaturtable on this page + page = pos.getPage(); + if (page > doc_pages) + { + throw new PDFDocumentException(227, "Page number is to big(=" + page+ ") cannot be parsed."); + } + } + boolean make_new_page = pos.isNewPage(); + //getPagedimensions + Rectangle psize = reader.getPageSizeWithRotation(page); + float page_width = psize.width(); + float page_height = psize.height(); + //now we can calculate x-position + float pre_pos_x = SIGNATURE_BORDER / 2; + if (!pos.isXauto()) + { + //we do have absolute x + pre_pos_x = pos.getPosX(); + } + final float pos_x = pre_pos_x; + //calculate width + float pre_width = page_width - pos_x - SIGNATURE_BORDER / 2; + if (!pos.isWauto()) { - // The automatic algorithm regarding the footer. - return PdfAS.adjustTableAndCalculatePositionRegardingFooter(pdf, pdf_table, pos.footer_line); + //we do have absolute width + pre_width = pos.getWidth(); + } + final float width = pre_width; + //Signatur table dimensions are complete + pdf_table.setTotalWidth(width); + pdf_table.setLockedWidth(true); + final float table_height = pdf_table.getTotalHeight(); + //now check pos_y + float pos_y = pos.getPosY(); + if (!pos.isYauto()) + { + //we do have y-position too --> all parameters but page ok + if (make_new_page) + { + page++; + } + return new PositioningInstruction(make_new_page, page, pos_x, pos_y); } - - boolean make_new_page = false; - int page = pos.page; - - if (pos.page == -1) + //pos_y is auto + if (make_new_page) + { + //ignore footer in new page + page++; + pos_y = page_height - SIGNATURE_BORDER / 2; + return new PositioningInstruction(make_new_page, page, pos_x, pos_y); + } + //up to here no checks have to be made if Tablesize and Pagesize are fit + //Now we have to getfreespace in page and reguard footerline + float footer_line = pos.getFooterLine(); + float pre_page_length = PDFUtilities.calculatePageLength(pdf, page-1, page_height-footer_line); + if (pre_page_length == Float.NEGATIVE_INFINITY) + { + //we do have an empty page + //pre_page_length = page_height; + pos_y = page_height - SIGNATURE_BORDER / 2; + return new PositioningInstruction(make_new_page, page, pos_x, pos_y); + } + final float page_length = pre_page_length; + pos_y = page_height - page_length - SIGNATURE_MARGIN; + if (pos_y - footer_line <= table_height) { - // Absolute positioning on a new page. make_new_page = true; - PdfReader reader = PdfAS.readInPdfDocument(pdf); - page = reader.getNumberOfPages() + 1; - reader = null; + if (!pos.isPauto()) + { + page = reader.getNumberOfPages(); + + } + page++; + pos_y = page_height - SIGNATURE_BORDER / 2; } - - pdf_table.setTotalWidth(pos.width); - pdf_table.setLockedWidth(true); - - return new PositioningInstruction(make_new_page, page, pos.pos_x, pos.pos_y); - + 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. @@ -1175,61 +1255,14 @@ public abstract class PdfAS * Parses the TablePos object from a given String with the appropriate format. * * @param pos_string - * The pos string. e.g. 1;40.0;600.0;300.0 + * The pos string. e.g. x:40.0;y:auto;w:auto;p:1;f:300.0 * @return Returns the parsed TablePos object. * @throws PDFDocumentException * Thrown, if the String doesn't have the proper format. */ public static TablePos parsePositionFromPosString(String pos_string) throws PDFDocumentException { - String[] strs = pos_string.split(";"); - - try - { - TablePos pos = new TablePos(); - pos.page = Integer.parseInt(strs[0]); - - if (pos.page < 1 && pos.page != -1 && pos.page != -2) - { - throw new PDFDocumentException(225, "Page (=" + pos.page + ") must not be lower than -2 and must not be 0."); - } - - if (pos.page == -2) - { - 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."); - } - } - + TablePos pos = new TablePos(pos_string); return pos; - } - catch (NumberFormatException e) - { - throw new PDFDocumentException(224, "Pos string (=" + pos_string + ") cannot be parsed."); - } } } -- cgit v1.2.3