From bf3beb28c1ec92ca6110f8de0bf9eeb0b598a6de Mon Sep 17 00:00:00 2001
From: Andreas Fitzek <andreas.fitzek@iaik.tugraz.at>
Date: Fri, 6 Jun 2014 11:04:24 +0200
Subject: Signature Position merge, Table Borders offset fix

---
 .../lib/impl/signing/pdfbox/PADESPDFBOXSigner.java |  24 +-
 .../pdfbox/PDFAsVisualSignatureBuilder.java        |  23 +-
 .../pdfbox/PDFAsVisualSignatureProperties.java     |   2 +-
 .../lib/impl/stamping/pdfbox/PDFBoxTable.java      |   2 -
 .../at/knowcenter/wag/egov/egiz/pdf/TablePos.java  | 271 ++++++++++++---------
 5 files changed, 193 insertions(+), 129 deletions(-)

(limited to 'pdf-as-lib/src/main/java')

diff --git a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/signing/pdfbox/PADESPDFBOXSigner.java b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/signing/pdfbox/PADESPDFBOXSigner.java
index b583e48e..1e33a3cb 100644
--- a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/signing/pdfbox/PADESPDFBOXSigner.java
+++ b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/signing/pdfbox/PADESPDFBOXSigner.java
@@ -171,17 +171,29 @@ public class PADESPDFBOXSigner implements IPdfSigner, IConfigurationConstants {
 					String posString = pdfObject.getStatus().getSignParamter()
 							.getSignaturePosition();
 
-					if (posString == null) {
-						posString = signatureProfileConfiguration
+					TablePos signaturePos = null;
+					
+					String signaturePosString = signatureProfileConfiguration
 								.getDefaultPositioning();
-					}
 
+					if(signaturePosString != null) {
+						logger.debug("using signature Positioning: " + signaturePos);
+						signaturePos = new TablePos(signaturePosString);
+					}
+					
 					logger.debug("using Positioning: " + posString);
 
-					if (posString == null) {
-						tablePos = new TablePos();
+					if (posString != null) {
+						// Merge Signature Position
+						tablePos = new TablePos(posString, signaturePos);
 					} else {
-						tablePos = new TablePos(posString);
+						// Fallback to signature Position!
+						tablePos = signaturePos;
+					}
+					
+					if(tablePos == null) {
+						// Last Fallback default position
+						tablePos = new TablePos();
 					}
 				}
 				boolean legacy32Position = signatureProfileConfiguration
diff --git a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/PDFAsVisualSignatureBuilder.java b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/PDFAsVisualSignatureBuilder.java
index d2049f24..e64d735a 100644
--- a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/PDFAsVisualSignatureBuilder.java
+++ b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/PDFAsVisualSignatureBuilder.java
@@ -52,7 +52,7 @@ public class PDFAsVisualSignatureBuilder extends PDVisibleSigBuilder {
 			.getLogger(PDFAsVisualSignatureBuilder.class);
 
 	private void drawTable(PDPage page, PDPageContentStream contentStream,
-			float x, float y, PDFBoxTable abstractTable, PDDocument doc,
+			float x, float y, float width, float height, PDFBoxTable abstractTable, PDDocument doc,
 			boolean subtable) throws IOException, PdfAsException {
 
 		final int rows = abstractTable.getRowCount();
@@ -109,9 +109,9 @@ public class PDFAsVisualSignatureBuilder extends PDVisibleSigBuilder {
 			for (int i = 0; i < rows; i++) {
 				ArrayList<Entry> row = abstractTable.getRow(i);
 				// Draw row border!
-				logger.debug("ROW LINE: {} {} {} {}", x, nexty, x + tableWidth,
+				logger.debug("ROW LINE: {} {} {} {}", x, nexty, x + width,
 						nexty);
-				contentStream.drawLine(x, nexty, x + tableWidth, nexty);
+				contentStream.drawLine(x, nexty, x + width, nexty);
 				lasty = nexty;
 				if (i < abstractTable.getRowHeights().length) {
 					nexty -= abstractTable.getRowHeights()[i] + padding * 2;
@@ -144,7 +144,9 @@ public class PDFAsVisualSignatureBuilder extends PDVisibleSigBuilder {
 						}
 					}
 				}
-				contentStream.drawLine(nextx, lasty, nextx, nexty);
+				if(!subtable) {
+					contentStream.drawLine(nextx, lasty, nextx, nexty);
+				}
 			}
 			
 			contentStream.drawLine(x, nexty, x + tableWidth, nexty);
@@ -359,8 +361,10 @@ public class PDFAsVisualSignatureBuilder extends PDVisibleSigBuilder {
 					tbl_value.table.setStyle(inherit_styletab);
 					
 					logger.debug("Table: " + tableX + " : " + tableY);
-					drawTable(page, contentStream, tableX, tableY, tbl_value,
-							doc, true);
+					drawTable(page, contentStream, tableX, tableY, 
+							(colsSizes != null) ? colsSizes[j] : colWidth, 
+							abstractTable.getRowHeights()[i] + padding * 2,
+							tbl_value, doc, true);
 				}
 				textx += (colsSizes != null) ? colsSizes[j] : colWidth;
 			}
@@ -379,6 +383,7 @@ public class PDFAsVisualSignatureBuilder extends PDVisibleSigBuilder {
 	}
 
 	private PDFAsVisualSignatureProperties properties;
+	private PDFAsVisualSignatureDesigner designer;
 	private ISettings settings;
 	private PDResources innerFormResources;
 	private Map<String, ImageObject> images = new HashMap<String, ImageObject>();
@@ -396,9 +401,11 @@ public class PDFAsVisualSignatureBuilder extends PDVisibleSigBuilder {
 	}
 
 	public PDFAsVisualSignatureBuilder(
-			PDFAsVisualSignatureProperties properties, ISettings settings) {
+			PDFAsVisualSignatureProperties properties, ISettings settings, 
+			PDFAsVisualSignatureDesigner designer) {
 		this.properties = properties;
 		this.settings = settings;
+		this.designer = designer;
 	}
 
 	@Override
@@ -541,8 +548,8 @@ public class PDFAsVisualSignatureBuilder extends PDVisibleSigBuilder {
 			PDPageContentStream stream = new PDPageContentStream(template,
 					getStructure().getPage());
 			// stream.setFont(PDType1Font.COURIER, 5);
-
 			drawTable(getStructure().getPage(), stream, 1, 1,
+					designer.getWidth(), designer.getHeight(),
 					properties.getMainTable(), template, false);
 			stream.close();
 			PDStream innterFormStream = getStructure().getPage().getContents();
diff --git a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/PDFAsVisualSignatureProperties.java b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/PDFAsVisualSignatureProperties.java
index 1b3cf688..6ed57703 100644
--- a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/PDFAsVisualSignatureProperties.java
+++ b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/PDFAsVisualSignatureProperties.java
@@ -50,7 +50,7 @@ public class PDFAsVisualSignatureProperties extends PDVisibleSigProperties {
 
 	@Override
 	public void buildSignature() throws IOException {
-		PDFAsVisualSignatureBuilder builder = new PDFAsVisualSignatureBuilder(this, this.settings);
+		PDFAsVisualSignatureBuilder builder = new PDFAsVisualSignatureBuilder(this, this.settings, designer);
 		PDFAsTemplateCreator creator = new PDFAsTemplateCreator(builder);
 		setVisibleSignature(creator.buildPDF(designer));
 	}
diff --git a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/PDFBoxTable.java b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/PDFBoxTable.java
index 1bbfbe9a..da228dae 100644
--- a/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/PDFBoxTable.java
+++ b/pdf-as-lib/src/main/java/at/gv/egiz/pdfas/lib/impl/stamping/pdfbox/PDFBoxTable.java
@@ -11,8 +11,6 @@ import org.apache.pdfbox.pdmodel.font.PDType1Font;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import com.sun.org.apache.bcel.internal.generic.Type;
-
 import at.gv.egiz.pdfas.common.exceptions.PdfAsException;
 import at.gv.egiz.pdfas.common.exceptions.PdfAsWrappedIOException;
 import at.gv.egiz.pdfas.common.settings.ISettings;
diff --git a/pdf-as-lib/src/main/java/at/knowcenter/wag/egov/egiz/pdf/TablePos.java b/pdf-as-lib/src/main/java/at/knowcenter/wag/egov/egiz/pdf/TablePos.java
index 2eb3b341..420a8f0a 100644
--- a/pdf-as-lib/src/main/java/at/knowcenter/wag/egov/egiz/pdf/TablePos.java
+++ b/pdf-as-lib/src/main/java/at/knowcenter/wag/egov/egiz/pdf/TablePos.java
@@ -158,6 +158,138 @@ public class TablePos implements Serializable
     //nothing to do --> default
   } 
   
+  private void parsePosString(String pos_string) throws PdfAsException {
+	//parse posstring and throw exception
+		//[x:x_algo];[y:y_algo];[w:w_algo][p:p_algo];[f:f_algo]
+		
+		String[] strs = pos_string.split(";");
+		try
+		{
+		  for (int cmds = 0;cmds<strs.length;cmds++)
+		  {
+			 
+			 String cmd_kvstring = strs[cmds];
+			 String[] cmd_kv = cmd_kvstring.split(":");
+			 if (cmd_kv.length != 2)
+			 {
+				 throw new PdfAsException("Pos string (=" + pos_string + ") is invalid.");
+			 }
+			 String cmdstr =  cmd_kv[0];
+			 if (cmdstr.length() != 1)
+			 {
+				 throw new PdfAsException("Pos string (=" + pos_string + ") is invalid.");
+			 }		 
+			 char command = cmdstr.charAt(0);
+		     String commandval= cmd_kv[1];
+		     switch (command)
+		     {
+		     	case 'x': {
+		     		         if (!commandval.equalsIgnoreCase("auto"))
+		     		         {  
+		     		        	float xval= Float.parseFloat(commandval);
+		     		            if (xval<0)
+		     		            {
+		     		            	throw new PdfAsException("Pos string (x:" + xval + ") is invalid.");
+		     		            }	     		          
+		     		        	this.pos_x = xval;
+		     		        	this.autoX = false; 
+		     		         }	     		         
+		     		         break;
+		     			  }	
+		     	case 'y': {
+			         		if (!commandval.equalsIgnoreCase("auto"))
+			         		{
+			         			float yval= Float.parseFloat(commandval);
+		     		            if (yval<0)
+		     		            {
+		     		            	throw new PdfAsException("Pos string (y:" + yval + ") is invalid.");
+		     		            }			         			
+			         			this.pos_y = yval;
+			         			this.autoY = false; 
+			         		}	     		         
+			         		break;
+		     			  }		
+		     	case 'w': { 
+	         				if (!commandval.equalsIgnoreCase("auto"))
+	         				{    
+			         			float wval= Float.parseFloat(commandval);
+		     		            if (wval<=0)
+		     		            {
+		     		            	throw new PdfAsException("pos.width (w:" + wval + ") must not be lower or equal 0.");
+		     		            }        					
+	         					this.width = wval;
+	         					this.autoW = false; 
+	         				}	     		         
+	         				break;
+	      				  }
+		     	case 'p': {
+	 						if (!commandval.equalsIgnoreCase("auto"))
+	 						{ 
+	 							if (commandval.equalsIgnoreCase("new"))
+	 							{ 								
+	 								this.newpage = true;
+	 							}
+	 							else
+	 							{
+	 								int pval = Integer.parseInt(commandval);
+	 								if (pval<1)
+	 								{
+	 									throw new PdfAsException("Page (p:" + pval + ") must not be lower than 1.");
+	 								}
+	 								this.page = pval;
+	 								this.autoP = false;
+	 							}
+	 						}						     		       
+	 						break;
+	      				  }
+		     	case 'f': {
+		     		        float flval=Float.parseFloat(commandval);
+	     		            if (flval<0)
+	     		            {
+	     		            	throw new PdfAsException("Pos string (=" + pos_string + ") is invalid.");
+	     		            } 	     		        
+		     				this.footer_line = flval;
+		     				break;
+		     			  }
+		     	case 'r': {
+	 		        		float flval=Float.parseFloat(commandval);
+	 		        		// TODO: check rotation to be only multiples of 90 degrees
+	 		        		if (flval<0)
+	 		        		{
+	 		        			throw new PdfAsException("Pos string (=" + pos_string + ") is invalid.");
+	 		        		} 	     		        
+	 		        		this.rotation = flval;
+	 		        		break;
+	 			  }
+		     	default : {
+			                throw new PdfAsException("Pos string (=" + pos_string + ") is invalid.");
+		                  }
+		     }
+		  }
+		  this.myposstring=pos_string;
+	    }
+	    catch (NumberFormatException e)
+	    {
+	      throw new PdfAsException("Pos string (=" + pos_string + ") cannot be parsed.");
+	    }
+  }
+  
+  private void readFromPos(TablePos base) {
+	  this.autoP = base.autoP;
+	  this.autoW = base.autoW;
+	  this.autoX = base.autoX;
+	  this.autoY = base.autoY;
+	  
+	  this.footer_line = base.footer_line;
+	  this.myposstring = base.myposstring;
+	  this.newpage = base.newpage;
+	  this.page = base.page;
+	  this.pos_x = base.pos_x;
+	  this.pos_y = base.pos_y;
+	  this.rotation  = base.rotation;
+	  this.width = base.width;
+  }
+  
   /**
    * Constructor.
    * 
@@ -178,120 +310,35 @@ public class TablePos implements Serializable
    */
   public TablePos(String pos_string) throws PdfAsException
   {
-    //parse posstring and throw exception
-	//[x:x_algo];[y:y_algo];[w:w_algo][p:p_algo];[f:f_algo]
-	
-	String[] strs = pos_string.split(";");
-	try
-	{
-	  for (int cmds = 0;cmds<strs.length;cmds++)
-	  {
-		 
-		 String cmd_kvstring = strs[cmds];
-		 String[] cmd_kv = cmd_kvstring.split(":");
-		 if (cmd_kv.length != 2)
-		 {
-			 throw new PdfAsException("Pos string (=" + pos_string + ") is invalid.");
-		 }
-		 String cmdstr =  cmd_kv[0];
-		 if (cmdstr.length() != 1)
-		 {
-			 throw new PdfAsException("Pos string (=" + pos_string + ") is invalid.");
-		 }		 
-		 char command = cmdstr.charAt(0);
-	     String commandval= cmd_kv[1];
-	     switch (command)
-	     {
-	     	case 'x': {
-	     		         if (!commandval.equalsIgnoreCase("auto"))
-	     		         {  
-	     		        	float xval= Float.parseFloat(commandval);
-	     		            if (xval<0)
-	     		            {
-	     		            	throw new PdfAsException("Pos string (x:" + xval + ") is invalid.");
-	     		            }	     		          
-	     		        	this.pos_x = xval;
-	     		        	this.autoX = false; 
-	     		         }	     		         
-	     		         break;
-	     			  }	
-	     	case 'y': {
-		         		if (!commandval.equalsIgnoreCase("auto"))
-		         		{
-		         			float yval= Float.parseFloat(commandval);
-	     		            if (yval<0)
-	     		            {
-	     		            	throw new PdfAsException("Pos string (y:" + yval + ") is invalid.");
-	     		            }			         			
-		         			this.pos_y = yval;
-		         			this.autoY = false; 
-		         		}	     		         
-		         		break;
-	     			  }		
-	     	case 'w': { 
-         				if (!commandval.equalsIgnoreCase("auto"))
-         				{    
-		         			float wval= Float.parseFloat(commandval);
-	     		            if (wval<=0)
-	     		            {
-	     		            	throw new PdfAsException("pos.width (w:" + wval + ") must not be lower or equal 0.");
-	     		            }        					
-         					this.width = wval;
-         					this.autoW = false; 
-         				}	     		         
-         				break;
-      				  }
-	     	case 'p': {
- 						if (!commandval.equalsIgnoreCase("auto"))
- 						{ 
- 							if (commandval.equalsIgnoreCase("new"))
- 							{ 								
- 								this.newpage = true;
- 							}
- 							else
- 							{
- 								int pval = Integer.parseInt(commandval);
- 								if (pval<1)
- 								{
- 									throw new PdfAsException("Page (p:" + pval + ") must not be lower than 1.");
- 								}
- 								this.page = pval;
- 								this.autoP = false;
- 							}
- 						}						     		       
- 						break;
-      				  }
-	     	case 'f': {
-	     		        float flval=Float.parseFloat(commandval);
-     		            if (flval<0)
-     		            {
-     		            	throw new PdfAsException("Pos string (=" + pos_string + ") is invalid.");
-     		            } 	     		        
-	     				this.footer_line = flval;
-	     				break;
-	     			  }
-	     	case 'r': {
- 		        		float flval=Float.parseFloat(commandval);
- 		        		// TODO: check rotation to be only multiples of 90 degrees
- 		        		if (flval<0)
- 		        		{
- 		        			throw new PdfAsException("Pos string (=" + pos_string + ") is invalid.");
- 		        		} 	     		        
- 		        		this.rotation = flval;
- 		        		break;
- 			  }
-	     	default : {
-		                throw new PdfAsException("Pos string (=" + pos_string + ") is invalid.");
-	                  }
-	     }
+    parsePosString(pos_string);
+  }
+  
+  /**
+   * Constructor
+   * @param pos_string The pos instruction.
+   * 		format : [x:x_algo];[y:y_algo];[w:w_algo][p:p_algo];[f:f_algo];[r:r_algo]
+   *        x_algo:='auto'     ... automatic positioning x
+   *                floatvalue ... absolute x
+   *        y_algo:='auto'     ... automatic positioning y
+   *                floatvalue ... absolute y
+   *        w_algo:='auto'     ... automatic width
+   *                floatvalue ... absolute width    
+   *        p_algo:='auto'     ... automatic last page
+   *                'new'      ... new page  
+   *                intvalue   ... pagenumber
+   *        f_algo  floatvalue ... consider footerline (only if y_algo is auto and p_algo is not 'new')
+   *        r_algo  floatvalue ... rotate the table arround the lower left corner anti clockwise in degree
+   * @param basePosition The base Table Position these values are the base values
+   * @throws PdfAsException
+   */
+  public TablePos(String pos_string, TablePos basePosition) throws PdfAsException
+  {
+	  if(basePosition != null) {
+		  readFromPos(basePosition);
 	  }
-	  this.myposstring=pos_string;
-    }
-    catch (NumberFormatException e)
-    {
-      throw new PdfAsException("Pos string (=" + pos_string + ") cannot be parsed.");
-    }
+	  parsePosString(pos_string);
   }
+  
   public String toString()
   {  
 	 String thatsme = "cmd:"+this.myposstring+" pos_x:"+this.pos_x+" pos_y:"+this.pos_y+" page:"+this.page+" width:"+this.width+" footer:"+this.footer_line+" rotation:"+this.rotation+"\n "+" autoX:"+this.autoX+" autoY:"+this.autoY+" autoW:"+this.autoW+" Newpage:"+this.newpage+" autoP:"+this.autoP; 
-- 
cgit v1.2.3