From 38b9dbc224d7be54dba23d04529df07f6fd0db51 Mon Sep 17 00:00:00 2001 From: Christian Maierhofer Date: Thu, 23 Apr 2015 09:50:54 +0200 Subject: error handling --- .../lib/impl/signing/pdfbox/PADESPDFBOXSigner.java | 153 +++++++++++---------- 1 file changed, 77 insertions(+), 76 deletions(-) (limited to 'pdf-as-pdfbox/src/main/java/at/gv/egiz') diff --git a/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/signing/pdfbox/PADESPDFBOXSigner.java b/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/signing/pdfbox/PADESPDFBOXSigner.java index fcea384a..250d1242 100644 --- a/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/signing/pdfbox/PADESPDFBOXSigner.java +++ b/pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib/impl/signing/pdfbox/PADESPDFBOXSigner.java @@ -477,95 +477,96 @@ public class PADESPDFBOXSigner implements IPdfSigner, IConfigurationConstants { } // PDF-UA - if (signatureProfileSettings.isPDFUA()==true) { - logger.info("Adding pdf/ua content."); - try{ - PDDocumentCatalog root = doc.getDocumentCatalog(); - PDStructureTreeRoot treeRoot = root.getStructureTreeRoot(); + logger.info("Adding pdf/ua content."); + try{ + PDDocumentCatalog root = doc.getDocumentCatalog(); + PDStructureTreeRoot treeRoot = root.getStructureTreeRoot(); - if (treeRoot != null) { //check if structure tree available in pdf document - logger.info("Tree Root: {}", treeRoot.toString()); - List kids = treeRoot.getKids(); + logger.info("Tree Root: {}", treeRoot.toString()); + List kids = treeRoot.getKids(); - if(kids==null){ - logger.info("No kid-elements in structure tree Root, maybe not PDF/UA document"); - } + if(kids==null){ + logger.info("No kid-elements in structure tree Root, maybe not PDF/UA document"); + } - PDStructureElement docElement = null; - for(Object k: kids){ - if(k instanceof PDStructureElement){ - docElement=(PDStructureElement) k; - break; - // if(((PDStructureElement) k).getStructureType().equals("Document")){ - // docElement=(PDStructureElement) k; - // } - } - } + PDStructureElement docElement = null; + for(Object k: kids){ + if(k instanceof PDStructureElement){ + docElement=(PDStructureElement) k; + break; + // if(((PDStructureElement) k).getStructureType().equals("Document")){ + // docElement=(PDStructureElement) k; + // } + } + } - PDStructureElement sigBlock = new PDStructureElement( - "Form", docElement); + PDStructureElement sigBlock = new PDStructureElement( + "Form", docElement); - //create object dictionary and add as child element - COSDictionary objectdic= new COSDictionary(); - objectdic.setName("Type", "OBJR"); - objectdic.setItem("Pg", signatureField.getWidget().getPage()); - objectdic.setItem("Obj", signatureField.getWidget()); + //create object dictionary and add as child element + COSDictionary objectdic= new COSDictionary(); + objectdic.setName("Type", "OBJR"); + objectdic.setItem("Pg", signatureField.getWidget().getPage()); + objectdic.setItem("Obj", signatureField.getWidget()); - List l = new ArrayList(); - l.add(objectdic); - sigBlock.setKids(l); + List l = new ArrayList(); + l.add(objectdic); + sigBlock.setKids(l); - sigBlock.setTitle("Signature Table"); - sigBlock.setParent(docElement); - docElement.appendKid(sigBlock); + sigBlock.setTitle("Signature Table"); + sigBlock.setParent(docElement); + docElement.appendKid(sigBlock); - //Create and add Attribute dictionary to mitigate PAC warning - COSDictionary sigBlockDic = (COSDictionary) sigBlock.getCOSObject(); - COSDictionary sub = new COSDictionary(); + //Create and add Attribute dictionary to mitigate PAC warning + COSDictionary sigBlockDic = (COSDictionary) sigBlock.getCOSObject(); + COSDictionary sub = new COSDictionary(); - sub.setName("O", "Layout"); - sub.setName("Placement", "Block"); - sigBlockDic.setItem(COSName.A, sub); - sigBlockDic.setNeedToBeUpdate(true); + sub.setName("O", "Layout"); + sub.setName("Placement", "Block"); + sigBlockDic.setItem(COSName.A, sub); + sigBlockDic.setNeedToBeUpdate(true); - //Modify number tree - PDNumberTreeNode ntn = treeRoot.getParentTree(); - if(ntn==null){ - ntn = new PDNumberTreeNode(objectdic, null); - logger.info("No number-tree-node found!"); - } + //Modify number tree + PDNumberTreeNode ntn = treeRoot.getParentTree(); + if(ntn==null){ + ntn = new PDNumberTreeNode(objectdic, null); + logger.info("No number-tree-node found!"); + } - COSDictionary ntndic=ntn.getCOSDictionary(); - COSArray ntndicnumbersarray = (COSArray)ntndic.getDictionaryObject(COSName.NUMS); - int arrindex = ntndicnumbersarray.size(); - int treeindex = arrindex/2; - - ntndicnumbersarray.add(arrindex, COSInteger.get(treeindex)); - ntndicnumbersarray.add(arrindex+1, sigBlock.getCOSObject()); - - treeRoot.setParentTree(ntn); - treeRoot.setParentTreeNextKey(treeindex+1); - - //setStructureParent - PDAnnotationWidget widg=signatureField.getWidget(); - widg.setStructParent(treeindex); - - //add the Tabs /S Element for Tabbing through annots - PDPage p = signatureField.getWidget().getPage(); - p.getCOSDictionary().setName("Tabs", "S"); - p.getCOSObject().setNeedToBeUpdate(true); - - ntndic.setNeedToBeUpdate(true); - sigBlock.getCOSObject().setNeedToBeUpdate(true); - treeRoot.getCOSObject().setNeedToBeUpdate(true); - objectdic.getCOSObject().setNeedToBeUpdate(true); - docElement.getCOSObject().setNeedToBeUpdate(true); - - } - }catch(Exception e){ + COSDictionary ntndic=ntn.getCOSDictionary(); + COSArray ntndicnumbersarray = (COSArray)ntndic.getDictionaryObject(COSName.NUMS); + int arrindex = ntndicnumbersarray.size(); + int treeindex = arrindex/2; + + ntndicnumbersarray.add(arrindex, COSInteger.get(treeindex)); + ntndicnumbersarray.add(arrindex+1, sigBlock.getCOSObject()); + + treeRoot.setParentTree(ntn); + treeRoot.setParentTreeNextKey(treeindex+1); + + //setStructureParent + PDAnnotationWidget widg=signatureField.getWidget(); + widg.setStructParent(treeindex); + + //add the Tabs /S Element for Tabbing through annots + PDPage p = signatureField.getWidget().getPage(); + p.getCOSDictionary().setName("Tabs", "S"); + p.getCOSObject().setNeedToBeUpdate(true); + + ntndic.setNeedToBeUpdate(true); + sigBlock.getCOSObject().setNeedToBeUpdate(true); + treeRoot.getCOSObject().setNeedToBeUpdate(true); + objectdic.getCOSObject().setNeedToBeUpdate(true); + docElement.getCOSObject().setNeedToBeUpdate(true); + + + }catch(Throwable e){ + if (signatureProfileSettings.isPDFUA()==true){ logger.error("Could not create PDF-UA conform document!"); - logger.error(e.getMessage()); + throw new PdfAsException("error.pdf.sig.pdfua.1", e); + }else{ + logger.info("Could not create PDF-UA conform signature"); } } -- cgit v1.2.3