diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/main/java/at/knowcenter/wag/egov/egiz/pdf/BinarySignature.java | 15 | ||||
-rw-r--r-- | src/main/java/at/knowcenter/wag/egov/egiz/pdf/StructContentHelper.java | 126 |
2 files changed, 111 insertions, 30 deletions
diff --git a/src/main/java/at/knowcenter/wag/egov/egiz/pdf/BinarySignature.java b/src/main/java/at/knowcenter/wag/egov/egiz/pdf/BinarySignature.java index 0465c5d..bc399f0 100644 --- a/src/main/java/at/knowcenter/wag/egov/egiz/pdf/BinarySignature.java +++ b/src/main/java/at/knowcenter/wag/egov/egiz/pdf/BinarySignature.java @@ -853,7 +853,7 @@ public abstract class BinarySignature // exthex
StructContentHelper structHelper = new StructContentHelper(stamper, content, pi.getPage());
- structHelper.buildMainStructData(table_template);
+ structHelper.prepareStructData(table_template);
pdf_table.writeSelectedRows(0, -1, 0, pdf_table.getTotalHeight(), table_template);
@@ -873,10 +873,7 @@ public abstract class BinarySignature content.addTemplate(table_template, pi.getX(), pi.getY() - pdf_table.getTotalHeight());
- structHelper.endSigBlockContent();
-
- structHelper.buildFigureStructData(so, table_template);
- structHelper.buildVerifyLinkStructData(table_template);
+ structHelper.endSigBlockContent();
ActualTablePos atp = new ActualTablePos();
@@ -886,7 +883,13 @@ public abstract class BinarySignature atp.width = pdf_table.getTotalWidth();
atp.height = pdf_table.getTotalHeight();
iui.actualTablePos = atp;
-
+
+ structHelper.buildFigureStructData(so, table_template);
+ structHelper.buildSigBlockStructData();
+ structHelper.finishMainStructData();
+
+ structHelper.buildVerifyLinkStructData(table_template, atp);
+
// For debugging print a 100x100 grid
// {
// Rectangle psize = reader.getPageSizeWithRotation(pos.page);
diff --git a/src/main/java/at/knowcenter/wag/egov/egiz/pdf/StructContentHelper.java b/src/main/java/at/knowcenter/wag/egov/egiz/pdf/StructContentHelper.java index e6ad646..220f9b7 100644 --- a/src/main/java/at/knowcenter/wag/egov/egiz/pdf/StructContentHelper.java +++ b/src/main/java/at/knowcenter/wag/egov/egiz/pdf/StructContentHelper.java @@ -8,6 +8,7 @@ import at.gv.egiz.pdfas.exceptions.ErrorCode; import at.knowcenter.wag.egov.egiz.exceptions.PresentableException;
import at.knowcenter.wag.egov.egiz.sig.SignatureObject;
+import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.PdfArray;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfDictionary;
@@ -65,6 +66,16 @@ public class StructContentHelper implements StructContentWriter { private PdfDictionary page;
private PdfNumber parentTreeNextKey = null;
private PdfNumber annotationParentTreeKey = null;
+
+ /**
+ * Temporary save a pos
+ */
+ private Rectangle tempMarkedPos = null;
+ /**
+ * Position of the signature verify link overlay
+ */
+ private Rectangle verifyLinkPos = null;
+
/**
* Kids array (K) of the <code>StructTreeRoot</code>
*/
@@ -99,11 +110,11 @@ public class StructContentHelper implements StructContentWriter { }
/**
- * Create and write structured content for signature block. This method initialized the whole StructTreeRoot stuff.
+ * Prepare structured content for signature block. This method initializes the whole StructTreeRoot stuff.
* @param sigBlockObj
* @throws PresentableException
*/
- void buildMainStructData(PdfTemplate sigBlockObj) throws PresentableException {
+ void prepareStructData(PdfTemplate sigBlockObj) throws PresentableException {
try {
checkTagging();
@@ -128,20 +139,49 @@ public class StructContentHelper implements StructContentWriter { this.structTreeRootKids = obtainStructTreeRootKids(structTreeRoot);
- PdfIndirectReference newStructRef = createStructElem(SIGBLOCK_STRUCT_TYPE, new PdfNumber(sigBlockMcid), structTreeRoot.getIndRef());
-
- // ADD everything at the end because nothing can be written afterwards
+ } catch (Exception ex) {
+ logger.error("error", ex);
+ throw new PresentableException(ErrorCode.CANNOT_WRITE_PDF,
+ "error writing structured signature content", ex);
+ }
+ }
+
+ /**
+ * Create struct data for main signature block
+ * @throws PresentableException
+ */
+ void buildSigBlockStructData() throws PresentableException {
+ if (!isTagged) return;
+ try {
+ PdfIndirectReference newStructRef = createStructElem(SIGBLOCK_STRUCT_TYPE, new PdfNumber(
+ sigBlockMcid), getStructTreeRoot().getIndRef());
+
+ // ADD everything at the end because nothing can be written afterwards
structTreeRootKids.add(newStructRef);
- mainParentTreeNumEntry.add(newStructRef);
+ mainParentTreeNumEntry.add(newStructRef);
stamperImp.markUsed(mainParentTreeNumEntry);
-// if (structParentsNr == null) {
-// parentTreeNums.add(stamper.getWriter().addToBody(mainParentTreeNumEntry).getIndirectReference());
-// stamperImp.markUsed(parentTreeNums);
-// stamperImp.markUsed(structTreeRoot.getAsDict(PdfName.PARENTTREE));
-// }
+ } catch (Exception ex) {
+ logger.error("error", ex);
+ throw new PresentableException(ErrorCode.CANNOT_WRITE_PDF,
+ "error writing structured signature content", ex);
+ }
+ }
+
+ /**
+ * Finish struct data for signblock and it's elements (NOT for the external link and annot!)
+ * @throws PresentableException
+ */
+ void finishMainStructData() throws PresentableException {
+ try {
+ if (isTagged && mainParentTreeNumEntry.getIndRef() == null) {
+ getParentTreeNums().add(
+ stamper.getWriter().addToBody(mainParentTreeNumEntry).getIndirectReference());
+ stamperImp.markUsed(getParentTreeNums());
+ stamperImp.markUsed(getStructTreeRoot().getAsDict(PdfName.PARENTTREE));
+ }
} catch (Exception ex) {
logger.error("error", ex);
throw new PresentableException(ErrorCode.CANNOT_WRITE_PDF,
@@ -174,11 +214,7 @@ public class StructContentHelper implements StructContentWriter { stamperImp.markUsed(mainParentTreeNumEntry);
}
- if (isTagged && mainParentTreeNumEntry.getIndRef() == null) {
- getParentTreeNums().add(stamper.getWriter().addToBody(mainParentTreeNumEntry).getIndirectReference());
- stamperImp.markUsed(getParentTreeNums());
- stamperImp.markUsed(getStructTreeRoot().getAsDict(PdfName.PARENTTREE));
- }
+
} catch (Exception ex) {
logger.error("error", ex);
throw new PresentableException(ErrorCode.CANNOT_WRITE_PDF,
@@ -191,16 +227,18 @@ public class StructContentHelper implements StructContentWriter { * Build the link annotation for the signature verification link and the structured content accordingly.<br>
* The tagging does NOT work if the link is placed in a binary signature replace cell (phlengh for this cell)!!
* @param sigBlockObj
+ * @param atp
* @throws PresentableException
*/
- void buildVerifyLinkStructData(PdfTemplate sigBlockObj) throws PresentableException {
+ void buildVerifyLinkStructData(PdfTemplate sigBlockObj, ActualTablePos atp) throws PresentableException {
if (!this.isTagged || !this.isLinkMarked()) return;
try {
PdfNumber parentTreeKey = getNewParentTreeKey();
PdfArray annots = obrainAnnotsFromPage();
- PdfIndirectReference linkAnnotRef = createLinkAnnot(parentTreeKey);
+
+ PdfIndirectReference linkAnnotRef = createLinkAnnot(parentTreeKey, atp);
annots.add(linkAnnotRef);
PdfIndirectReference objr = createObjrStructElem(linkAnnotRef);
@@ -357,9 +395,22 @@ public class StructContentHelper implements StructContentWriter { localContent.endMarkedContentSequence();
}
}
-
/**
+ * Implements {@link StructContentWriter#markPos(Rectangle)}
+ */
+ public void markPos(Rectangle pos) {
+ this.tempMarkedPos = pos;
+ }
+
+ /**
+ * Implements {@link StructContentWriter#storeCurrentPosAsLink()}
+ */
+ public void storeCurrentPosAsLink() {
+ this.verifyLinkPos = new Rectangle(this.tempMarkedPos);
+ }
+
+ /**
* set explicit annotation tab order if missing
*/
private void doAnnoTabOrder() {
@@ -379,17 +430,27 @@ public class StructContentHelper implements StructContentWriter { }
logger.debug("Input is tagged. Writing structure/WAI data.");
}
+
+
- private PdfIndirectReference createLinkAnnot(PdfNumber structParentNr) throws IOException {
- PdfDictionary linkAnnot = new PdfDictionary();
- PdfDictionary a = new PdfDictionary();
+ private PdfIndirectReference createLinkAnnot(PdfNumber structParentNr, ActualTablePos atp) throws IOException {
+ PdfDictionary linkAnnot = new PdfDictionary();
+
+ PdfDictionary a = new PdfDictionary();
a.put(PdfName.S, new PdfName("URI"));
a.put(PdfName.TYPE, PdfName.ACTION);
a.put(PdfName.URI, new PdfString(this.linkUrlString));
linkAnnot.put(PdfName.A, a);
+ PdfDictionary bs = new PdfDictionary();
+ bs.put(PdfName.W, new PdfNumber(0));
+ linkAnnot.put(PdfName.BS, bs);
+ linkAnnot.put(PdfName.F, new PdfNumber(4));
+
// iText "converts" 0.0f to an integer, therefore we cannot use 0, not nice...
- linkAnnot.put(PdfName.RECT, new PdfArray(new float[] {0.01f, 0.01f, 0.01f, 0.01f}));
+ //linkAnnot.put(PdfName.RECT, new PdfArray(new float[] {0.01f, 0.01f, 0.01f, 0.01f}));
+ // take cell pos as link pos
+ linkAnnot.put(PdfName.RECT, new PdfArray(createAbsPos(atp, this.verifyLinkPos)));
linkAnnot.put(PdfName.STRUCTPARENT, structParentNr);
linkAnnot.put(PdfName.SUBTYPE, PdfName.LINK);
@@ -397,6 +458,21 @@ public class StructContentHelper implements StructContentWriter { return stamper.getWriter().addToBody(linkAnnot).getIndirectReference();
}
+
+ private static PdfArray createAbsPos(ActualTablePos atp, Rectangle pos) {
+ PdfArray res = new PdfArray();
+ float y = atp.y - atp.height + pos.getTop();
+ res.add(new PdfNumber(atp.x + pos.getLeft()));
+ res.add(new PdfNumber(y));
+ res.add(new PdfNumber(atp.x + pos.getLeft() + pos.getWidth()));
+ res.add(new PdfNumber(y - pos.getHeight()));
+ return res;
+ }
+
+ protected static PdfArray createPdfArrayFromTablePos(ActualTablePos pos) {
+ return new PdfArray( new float[] {pos.x, pos.y, pos.x + pos.width, pos.y - pos.height});
+ }
+
private PdfArray obrainAnnotsFromPage() throws IOException {
PdfArray annots = this.page.getAsArray(PdfName.ANNOTS);
if (annots == null) {
@@ -567,6 +643,8 @@ public class StructContentHelper implements StructContentWriter { private static String getAltText(String sigProfile) {
return AdobeSignatureHelper.getDefaultableConfigProperty(sigProfile, ALT_TEXT_CONF_KEY, ALT_TEXT_DEFAULT);
- }
+ }
+
+
}
|