From 843020504a70df4e13d51fdcc4f1986b5656b2f2 Mon Sep 17 00:00:00 2001 From: Andreas Fitzek Date: Tue, 9 Dec 2014 15:08:44 +0100 Subject: Tempfile Streams closing --- .../lib/impl/signing/pdfbox/PADESPDFBOXSigner.java | 681 +++++++++++---------- 1 file changed, 369 insertions(+), 312 deletions(-) (limited to 'pdf-as-pdfbox/src/main/java/at/gv/egiz/pdfas/lib') 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 c36ae653..a0d85219 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 @@ -124,365 +124,422 @@ public class PADESPDFBOXSigner implements IPdfSigner, IConfigurationConstants { try { fisTmpFile = helper.getStaticFilename(); - // write to temporary file - FileOutputStream fos = new FileOutputStream(new File(fisTmpFile)); - IOUtils.copy(pdfObject.getOriginalDocument().getInputStream(), fos); + FileOutputStream tmpOutputStream = null; + try { + // write to temporary file + tmpOutputStream = new FileOutputStream(new File(fisTmpFile)); + InputStream tmpis = null; + try { + tmpis = pdfObject.getOriginalDocument().getInputStream(); + IOUtils.copy(tmpis, tmpOutputStream); + tmpis.close(); + } finally { + IOUtils.closeQuietly(tmpis); + } - FileInputStream fis = new FileInputStream(new File(fisTmpFile)); + doc = pdfObject.getDocument(); - doc = pdfObject.getDocument(); + SignaturePlaceholderData signaturePlaceholderData = PlaceholderFilter + .checkPlaceholderSignature(pdfObject.getStatus(), + pdfObject.getStatus().getSettings()); - SignaturePlaceholderData signaturePlaceholderData = PlaceholderFilter - .checkPlaceholderSignature(pdfObject.getStatus(), pdfObject - .getStatus().getSettings()); + TablePos tablePos = null; - TablePos tablePos = null; + if (signaturePlaceholderData != null) { + // Placeholder found! + logger.info("Placeholder data found."); + if (signaturePlaceholderData.getProfile() != null) { + logger.debug("Placeholder Profile set to: " + + signaturePlaceholderData.getProfile()); + requestedSignature + .setSignatureProfileID(signaturePlaceholderData + .getProfile()); + } - if (signaturePlaceholderData != null) { - // Placeholder found! - logger.info("Placeholder data found."); - if (signaturePlaceholderData.getProfile() != null) { - logger.debug("Placeholder Profile set to: " + signaturePlaceholderData.getProfile()); - requestedSignature - .setSignatureProfileID(signaturePlaceholderData - .getProfile()); + tablePos = signaturePlaceholderData.getTablePos(); + if (tablePos != null) { + logger.debug("Placeholder Position set to: " + + tablePos.toString()); + } } - tablePos = signaturePlaceholderData.getTablePos(); - if(tablePos != null) { - logger.debug("Placeholder Position set to: " + tablePos.toString()); + PDSignature signature = new PDSignature(); + signature.setFilter(COSName.getPDFName(signer.getPDFFilter())); // default + // filter + signature.setSubFilter(COSName.getPDFName(signer + .getPDFSubFilter())); + + SignatureProfileSettings signatureProfileSettings = TableFactory + .createProfile( + requestedSignature.getSignatureProfileID(), + pdfObject.getStatus().getSettings()); + + ValueResolver resolver = new ValueResolver(requestedSignature, + pdfObject.getStatus()); + String signerName = resolver.resolve("SIG_SUBJECT", + signatureProfileSettings.getValue("SIG_SUBJECT"), + signatureProfileSettings); + + signature.setName(signerName); + signature.setSignDate(Calendar.getInstance()); + String signerReason = signatureProfileSettings + .getSigningReason(); + + if (signerReason == null) { + signerReason = "PAdES Signature"; } - } - - PDSignature signature = new PDSignature(); - signature.setFilter(COSName.getPDFName(signer.getPDFFilter())); // default - // filter - signature - .setSubFilter(COSName.getPDFName(signer.getPDFSubFilter())); - - SignatureProfileSettings signatureProfileSettings = TableFactory - .createProfile(requestedSignature.getSignatureProfileID(), - pdfObject.getStatus().getSettings()); - ValueResolver resolver = new ValueResolver(requestedSignature, - pdfObject.getStatus()); - String signerName = resolver.resolve("SIG_SUBJECT", - signatureProfileSettings.getValue("SIG_SUBJECT"), - signatureProfileSettings); + signature.setReason(signerReason); + logger.debug("Signing reason: " + signerReason); - signature.setName(signerName); - signature.setSignDate(Calendar.getInstance()); - String signerReason = signatureProfileSettings.getSigningReason(); + logger.debug("Signing @ " + + signer.getSigningDate().getTime().toString()); + // the signing date, needed for valid signature + // signature.setSignDate(signer.getSigningDate()); - if (signerReason == null) { - signerReason = "PAdES Signature"; - } + signer.setPDSignature(signature); + SignatureOptions options = new SignatureOptions(); + options.setPreferedSignatureSize(0x1000); - signature.setReason(signerReason); - logger.debug("Signing reason: " + signerReason); + // Is visible Signature + if (requestedSignature.isVisual()) { + logger.info("Creating visual siganture block"); - logger.debug("Signing @ " - + signer.getSigningDate().getTime().toString()); - // the signing date, needed for valid signature - // signature.setSignDate(signer.getSigningDate()); + SignatureProfileConfiguration signatureProfileConfiguration = pdfObject + .getStatus().getSignatureProfileConfiguration( + requestedSignature.getSignatureProfileID()); - signer.setPDSignature(signature); - SignatureOptions options = new SignatureOptions(); - options.setPreferedSignatureSize(0x1000); + if (tablePos == null) { + // ================================================================ + // PositioningStage (visual) -> find position or use + // fixed + // position - // Is visible Signature - if (requestedSignature.isVisual()) { - logger.info("Creating visual siganture block"); + String posString = pdfObject.getStatus() + .getSignParamter().getSignaturePosition(); - SignatureProfileConfiguration signatureProfileConfiguration = pdfObject - .getStatus().getSignatureProfileConfiguration( - requestedSignature.getSignatureProfileID()); + TablePos signaturePos = null; - if (tablePos == null) { - // ================================================================ - // PositioningStage (visual) -> find position or use fixed - // position + String signaturePosString = signatureProfileConfiguration + .getDefaultPositioning(); - String posString = pdfObject.getStatus().getSignParamter() - .getSignaturePosition(); + if (signaturePosString != null) { + logger.debug("using signature Positioning: " + + signaturePos); + signaturePos = new TablePos(signaturePosString); + } - TablePos signaturePos = null; + logger.debug("using Positioning: " + posString); - String signaturePosString = signatureProfileConfiguration - .getDefaultPositioning(); + if (posString != null) { + // Merge Signature Position + tablePos = new TablePos(posString, signaturePos); + } else { + // Fallback to signature Position! + tablePos = signaturePos; + } - if (signaturePosString != null) { - logger.debug("using signature Positioning: " - + signaturePos); - signaturePos = new TablePos(signaturePosString); + if (tablePos == null) { + // Last Fallback default position + tablePos = new TablePos(); + } } - - logger.debug("using Positioning: " + posString); - - if (posString != null) { - // Merge Signature Position - tablePos = new TablePos(posString, signaturePos); - } else { - // Fallback to signature Position! - tablePos = signaturePos; + boolean legacy32Position = signatureProfileConfiguration + .getLegacy32Positioning(); + + // create Table describtion + Table main = TableFactory.createSigTable( + signatureProfileSettings, MAIN, + pdfObject.getStatus(), requestedSignature); + + IPDFStamper stamper = StamperFactory + .createDefaultStamper(pdfObject.getStatus() + .getSettings()); + + IPDFVisualObject visualObject = stamper + .createVisualPDFObject(pdfObject, main); + + /* + * PDDocument originalDocument = PDDocument .load(new + * ByteArrayInputStream(pdfObject.getStatus() + * .getPdfObject().getOriginalDocument())); + */ + + PositioningInstruction positioningInstruction = Positioning + .determineTablePositioning(tablePos, "", doc, + visualObject, legacy32Position); + + logger.debug("Positioning: {}", + positioningInstruction.toString()); + + if (positioningInstruction.isMakeNewPage()) { + int last = doc.getNumberOfPages() - 1; + PDDocumentCatalog root = doc.getDocumentCatalog(); + PDPageNode rootPages = root.getPages(); + List kids = new ArrayList(); + rootPages.getAllKids(kids); + PDPage lastPage = kids.get(last); + rootPages.getCOSObject().setNeedToBeUpdate(true); + PDPage p = new PDPage(lastPage.findMediaBox()); + p.setResources(new PDResources()); + p.setRotation(lastPage.findRotation()); + doc.addPage(p); } - if (tablePos == null) { - // Last Fallback default position - tablePos = new TablePos(); + // handle rotated page + PDDocumentCatalog documentCatalog = doc + .getDocumentCatalog(); + PDPageNode documentPages = documentCatalog.getPages(); + List documentPagesKids = new ArrayList(); + documentPages.getAllKids(documentPagesKids); + int targetPageNumber = positioningInstruction.getPage(); + logger.debug("Target Page: " + targetPageNumber); + // rootPages.getAllKids(kids); + PDPage targetPage = documentPagesKids + .get(targetPageNumber - 1); + int rot = targetPage.findRotation(); + logger.debug("Page rotation: " + rot); + // positioningInstruction.setRotation(positioningInstruction.getRotation() + // + rot); + logger.debug("resulting Sign rotation: " + + positioningInstruction.getRotation()); + + SignaturePositionImpl position = new SignaturePositionImpl(); + position.setX(positioningInstruction.getX()); + position.setY(positioningInstruction.getY()); + position.setPage(positioningInstruction.getPage()); + position.setHeight(visualObject.getHeight()); + position.setWidth(visualObject.getWidth()); + + requestedSignature.setSignaturePosition(position); + + PDFAsVisualSignatureProperties properties = new PDFAsVisualSignatureProperties( + pdfObject.getStatus().getSettings(), pdfObject, + (PdfBoxVisualObject) visualObject, + positioningInstruction); + + properties.buildSignature(); + + /* + * ByteArrayOutputStream sigbos = new + * ByteArrayOutputStream(); + * sigbos.write(StreamUtils.inputStreamToByteArray + * (properties .getVisibleSignature())); sigbos.close(); + */ + + if (signaturePlaceholderData != null) { + // Placeholder found! + // replace placeholder + InputStream is = null; + try { + is = PADESPDFBOXSigner.class + .getResourceAsStream("/placeholder/empty.jpg"); + PDJpeg img = new PDJpeg(doc, is); + + img.getCOSObject().setNeedToBeUpdate(true); + + PDDocumentCatalog root = doc.getDocumentCatalog(); + PDPageNode rootPages = root.getPages(); + List kids = new ArrayList(); + rootPages.getAllKids(kids); + int pageNumber = positioningInstruction.getPage(); + // rootPages.getAllKids(kids); + PDPage page = kids.get(pageNumber - 1); + + logger.info("Placeholder name: " + + signaturePlaceholderData + .getPlaceholderName()); + COSDictionary xobjectsDictionary = (COSDictionary) page + .findResources().getCOSDictionary() + .getDictionaryObject(COSName.XOBJECT); + xobjectsDictionary.setItem(signaturePlaceholderData + .getPlaceholderName(), img); + xobjectsDictionary.setNeedToBeUpdate(true); + page.findResources().getCOSObject() + .setNeedToBeUpdate(true); + logger.info("Placeholder name: " + + signaturePlaceholderData + .getPlaceholderName()); + } finally { + IOUtils.closeQuietly(is); + } } - } - boolean legacy32Position = signatureProfileConfiguration - .getLegacy32Positioning(); - - // create Table describtion - Table main = TableFactory.createSigTable( - signatureProfileSettings, MAIN, pdfObject.getStatus(), - requestedSignature); - - IPDFStamper stamper = StamperFactory - .createDefaultStamper(pdfObject.getStatus() - .getSettings()); - - IPDFVisualObject visualObject = stamper.createVisualPDFObject( - pdfObject, main); - - /* - * PDDocument originalDocument = PDDocument .load(new - * ByteArrayInputStream(pdfObject.getStatus() - * .getPdfObject().getOriginalDocument())); - */ - - PositioningInstruction positioningInstruction = Positioning - .determineTablePositioning(tablePos, "", doc, - visualObject, legacy32Position); - - logger.debug("Positioning: {}" , positioningInstruction.toString()); - - if (positioningInstruction.isMakeNewPage()) { - int last = doc.getNumberOfPages() - 1; - PDDocumentCatalog root = doc.getDocumentCatalog(); - PDPageNode rootPages = root.getPages(); - List kids = new ArrayList(); - rootPages.getAllKids(kids); - PDPage lastPage = kids.get(last); - rootPages.getCOSObject().setNeedToBeUpdate(true); - PDPage p = new PDPage(lastPage.findMediaBox()); - p.setResources(new PDResources()); - p.setRotation(lastPage.findRotation()); - doc.addPage(p); - } - - // handle rotated page - PDDocumentCatalog documentCatalog = doc.getDocumentCatalog(); - PDPageNode documentPages = documentCatalog.getPages(); - List documentPagesKids = new ArrayList(); - documentPages.getAllKids(documentPagesKids); - int targetPageNumber = positioningInstruction.getPage(); - logger.debug("Target Page: " + targetPageNumber); - //rootPages.getAllKids(kids); - PDPage targetPage = documentPagesKids.get(targetPageNumber-1); - int rot = targetPage.findRotation(); - logger.debug("Page rotation: " + rot); - //positioningInstruction.setRotation(positioningInstruction.getRotation() + rot); - logger.debug("resulting Sign rotation: " + positioningInstruction.getRotation()); - - SignaturePositionImpl position = new SignaturePositionImpl(); - position.setX(positioningInstruction.getX()); - position.setY(positioningInstruction.getY()); - position.setPage(positioningInstruction.getPage()); - position.setHeight(visualObject.getHeight()); - position.setWidth(visualObject.getWidth()); - - requestedSignature.setSignaturePosition(position); - - PDFAsVisualSignatureProperties properties = new PDFAsVisualSignatureProperties( - pdfObject.getStatus().getSettings(), pdfObject, - (PdfBoxVisualObject) visualObject, - positioningInstruction); - - properties.buildSignature(); - - /* - * ByteArrayOutputStream sigbos = new ByteArrayOutputStream(); - * sigbos.write(StreamUtils.inputStreamToByteArray(properties - * .getVisibleSignature())); sigbos.close(); - */ - - if (signaturePlaceholderData != null) { - // Placeholder found! - // replace placeholder - InputStream is = PADESPDFBOXSigner.class - .getResourceAsStream("/placeholder/empty.jpg"); - PDJpeg img = new PDJpeg(doc, is); - img.getCOSObject().setNeedToBeUpdate(true); - - PDDocumentCatalog root = doc.getDocumentCatalog(); - PDPageNode rootPages = root.getPages(); - List kids = new ArrayList(); - rootPages.getAllKids(kids); - int pageNumber = positioningInstruction.getPage(); - //rootPages.getAllKids(kids); - PDPage page = kids.get(pageNumber-1); - - logger.info("Placeholder name: " - + signaturePlaceholderData.getPlaceholderName()); - COSDictionary xobjectsDictionary = (COSDictionary) page - .findResources().getCOSDictionary() - .getDictionaryObject(COSName.XOBJECT); - xobjectsDictionary.setItem( - signaturePlaceholderData.getPlaceholderName(), img); - xobjectsDictionary.setNeedToBeUpdate(true); - page.findResources().getCOSObject().setNeedToBeUpdate(true); - logger.info("Placeholder name: " - + signaturePlaceholderData.getPlaceholderName()); - } - if (signatureProfileSettings.isPDFA()) { - PDDocumentCatalog root = doc.getDocumentCatalog(); - COSBase base = root.getCOSDictionary().getItem( - COSName.OUTPUT_INTENTS); - if (base == null) { - InputStream colorProfile = PDDocumentCatalog.class - .getResourceAsStream("/icm/sRGB Color Space Profile.icm"); - try { - PDOutputIntent oi = new PDOutputIntent(doc, - colorProfile); - oi.setInfo("sRGB IEC61966-2.1"); - oi.setOutputCondition("sRGB IEC61966-2.1"); - oi.setOutputConditionIdentifier("sRGB IEC61966-2.1"); - oi.setRegistryName("http://www.color.org"); - - root.addOutputIntent(oi); - root.getCOSObject().setNeedToBeUpdate(true); - logger.info("added Output Intent"); - } catch (Throwable e) { - e.printStackTrace(); - throw new PdfAsException( - "Failed to add Output Intent", e); + if (signatureProfileSettings.isPDFA()) { + PDDocumentCatalog root = doc.getDocumentCatalog(); + COSBase base = root.getCOSDictionary().getItem( + COSName.OUTPUT_INTENTS); + if (base == null) { + InputStream colorProfile = null; + try { + colorProfile = PDDocumentCatalog.class + .getResourceAsStream("/icm/sRGB Color Space Profile.icm"); + + try { + PDOutputIntent oi = new PDOutputIntent(doc, + colorProfile); + oi.setInfo("sRGB IEC61966-2.1"); + oi.setOutputCondition("sRGB IEC61966-2.1"); + oi.setOutputConditionIdentifier("sRGB IEC61966-2.1"); + oi.setRegistryName("http://www.color.org"); + + root.addOutputIntent(oi); + root.getCOSObject().setNeedToBeUpdate(true); + logger.info("added Output Intent"); + } catch (Throwable e) { + e.printStackTrace(); + throw new PdfAsException( + "Failed to add Output Intent", e); + } + } finally { + IOUtils.closeQuietly(colorProfile); + } } } - } - // if (signatureProfileSettings.isPDFA()) { // Check for PDF-UA - // PDDocumentCatalog root = doc.getDocumentCatalog(); - // PDStructureTreeRoot treeRoot = root.getStructureTreeRoot(); - // if (treeRoot != null) { // Handle as PDF-UA - // logger.info("Tree Root: {}", treeRoot.toString()); - // PDStructureElement docElement = PDFBoxTaggingUtils - // .getDocumentElement(treeRoot); - // PDStructureElement sigBlock = new PDStructureElement( - // "Table", docElement); - // root.getCOSObject().setNeedToBeUpdate(true); - // docElement.getCOSObject().setNeedToBeUpdate(true); - // treeRoot.getCOSObject().setNeedToBeUpdate(true); - // sigBlock.setTitle("Signature Table"); - // } - // } - - options.setPage(positioningInstruction.getPage()); - options.setVisualSignature(properties.getVisibleSignature()); - } + // if (signatureProfileSettings.isPDFA()) { // Check for + // PDF-UA + // PDDocumentCatalog root = doc.getDocumentCatalog(); + // PDStructureTreeRoot treeRoot = + // root.getStructureTreeRoot(); + // if (treeRoot != null) { // Handle as PDF-UA + // logger.info("Tree Root: {}", treeRoot.toString()); + // PDStructureElement docElement = PDFBoxTaggingUtils + // .getDocumentElement(treeRoot); + // PDStructureElement sigBlock = new PDStructureElement( + // "Table", docElement); + // root.getCOSObject().setNeedToBeUpdate(true); + // docElement.getCOSObject().setNeedToBeUpdate(true); + // treeRoot.getCOSObject().setNeedToBeUpdate(true); + // sigBlock.setTitle("Signature Table"); + // } + // } + + options.setPage(positioningInstruction.getPage()); + options.setVisualSignature(properties.getVisibleSignature()); + } - doc.addSignature(signature, signer, options); + doc.addSignature(signature, signer, options); - String sigFieldName = signatureProfileSettings.getSignFieldValue(); + String sigFieldName = signatureProfileSettings + .getSignFieldValue(); - if (sigFieldName == null) { - sigFieldName = "PDF-AS Signatur"; - } + if (sigFieldName == null) { + sigFieldName = "PDF-AS Signatur"; + } - int count = PdfBoxUtils.countSignatures(doc, sigFieldName); - - sigFieldName = sigFieldName + count; - - PDAcroForm acroFormm = doc.getDocumentCatalog().getAcroForm(); - if (acroFormm != null) { - @SuppressWarnings("unchecked") - List fields = acroFormm.getFields(); - PDSignatureField signatureField = null; - - if (fields != null) { - for (PDField pdField : fields) { - if (pdField != null) { - if (pdField instanceof PDSignatureField) { - PDSignatureField tmpSigField = (PDSignatureField) pdField; - if (tmpSigField.getSignature() != null && - tmpSigField.getSignature().getDictionary() != null) { - if (tmpSigField.getSignature().getDictionary() - .equals(signature.getDictionary())) { - signatureField = (PDSignatureField) pdField; + int count = PdfBoxUtils.countSignatures(doc, sigFieldName); + + sigFieldName = sigFieldName + count; + + PDAcroForm acroFormm = doc.getDocumentCatalog().getAcroForm(); + if (acroFormm != null) { + @SuppressWarnings("unchecked") + List fields = acroFormm.getFields(); + PDSignatureField signatureField = null; + + if (fields != null) { + for (PDField pdField : fields) { + if (pdField != null) { + if (pdField instanceof PDSignatureField) { + PDSignatureField tmpSigField = (PDSignatureField) pdField; + if (tmpSigField.getSignature() != null + && tmpSigField.getSignature() + .getDictionary() != null) { + if (tmpSigField + .getSignature() + .getDictionary() + .equals(signature + .getDictionary())) { + signatureField = (PDSignatureField) pdField; + } } } } } + } else { + logger.warn("Failed to name Signature Field! [Cannot find Field list in acroForm!]"); + } + + if (signatureField != null) { + signatureField.setPartialName(sigFieldName); } } else { - logger.warn("Failed to name Signature Field! [Cannot find Field list in acroForm!]"); + logger.warn("Failed to name Signature Field! [Cannot find acroForm!]"); } - if (signatureField != null) { - signatureField.setPartialName(sigFieldName); + if (requestedSignature.isVisual()) { + + // if(requestedSignature.getSignaturePosition().) + /* + * PDAcroForm acroForm = + * doc.getDocumentCatalog().getAcroForm(); if (acroForm != + * null) { + * + * @SuppressWarnings("unchecked") List fields = + * acroForm.getFields(); PDSignatureField signatureField = + * null; + * + * if (fields != null) { for (PDField pdField : fields) { if + * (pdField instanceof PDSignatureField) { if + * (((PDSignatureField) pdField).getSignature() + * .getDictionary() .equals(signature.getDictionary())) { + * signatureField = (PDSignatureField) pdField; } } } } else + * { logger.warn( + * "Failed to apply rotation! [Cannot find Field list in acroForm!]" + * ); } + * + * if (signatureField != null) { if + * (signatureField.getWidget() != null) { if + * (signatureField.getWidget() + * .getAppearanceCharacteristics() == null) { + * PDAppearanceCharacteristicsDictionary dict = new + * PDAppearanceCharacteristicsDictionary( new + * COSDictionary()); signatureField.getWidget() + * .setAppearanceCharacteristics(dict); } + * + * if (signatureField.getWidget() + * .getAppearanceCharacteristics() != null) { + * signatureField.getWidget() + * .getAppearanceCharacteristics() .setRotation(90); } } } + * else { logger.warn( + * "Failed to apply rotation! [Cannot find signature Field!]" + * ); } } else { logger.warn( + * "Failed to apply rotation! [Cannot find acroForm!]" ); } + */ } - } else { - logger.warn("Failed to name Signature Field! [Cannot find acroForm!]"); - } - if (requestedSignature.isVisual()) { - - // if(requestedSignature.getSignaturePosition().) - /* - * PDAcroForm acroForm = doc.getDocumentCatalog().getAcroForm(); - * if (acroForm != null) { - * - * @SuppressWarnings("unchecked") List fields = - * acroForm.getFields(); PDSignatureField signatureField = null; - * - * if (fields != null) { for (PDField pdField : fields) { if - * (pdField instanceof PDSignatureField) { if - * (((PDSignatureField) pdField).getSignature() .getDictionary() - * .equals(signature.getDictionary())) { signatureField = - * (PDSignatureField) pdField; } } } } else { logger.warn( - * "Failed to apply rotation! [Cannot find Field list in acroForm!]" - * ); } - * - * if (signatureField != null) { if (signatureField.getWidget() - * != null) { if (signatureField.getWidget() - * .getAppearanceCharacteristics() == null) { - * PDAppearanceCharacteristicsDictionary dict = new - * PDAppearanceCharacteristicsDictionary( new COSDictionary()); - * signatureField.getWidget() - * .setAppearanceCharacteristics(dict); } - * - * if (signatureField.getWidget() - * .getAppearanceCharacteristics() != null) { - * signatureField.getWidget() .getAppearanceCharacteristics() - * .setRotation(90); } } } else { logger.warn( - * "Failed to apply rotation! [Cannot find signature Field!]"); - * } } else { - * logger.warn("Failed to apply rotation! [Cannot find acroForm!]" - * ); } - */ - } + FileInputStream tmpFileIs = null; - // pdfbox patched (FIS -> IS) - doc.saveIncremental(fis, fos); - fis.close(); - fos.flush(); - fos.close(); - fos = null; + try { + tmpFileIs = new FileInputStream(new File(fisTmpFile)); - fis = new FileInputStream(new File(fisTmpFile)); + doc.saveIncremental(tmpFileIs, tmpOutputStream); + tmpFileIs.close(); + } finally { + IOUtils.closeQuietly(tmpFileIs); + } + tmpOutputStream.flush(); + tmpOutputStream.close(); + } finally { + IOUtils.closeQuietly(tmpOutputStream); + } - // write to resulting output stream - // ByteArrayOutputStream bos = new ByteArrayOutputStream(); - // bos.write(); - // bos.close(); + FileInputStream readReadyFile = null; + try { + readReadyFile = new FileInputStream(new File(fisTmpFile)); - pdfObject - .setSignedDocument(StreamUtils.inputStreamToByteArray(fis)); - fis.close(); - fis = null; + // write to resulting output stream + // ByteArrayOutputStream bos = new ByteArrayOutputStream(); + // bos.write(); + // bos.close(); + + pdfObject.setSignedDocument(StreamUtils + .inputStreamToByteArray(readReadyFile)); + readReadyFile.close(); + } finally { + IOUtils.closeQuietly(readReadyFile); + } System.gc(); helper.deleteFile(fisTmpFile); -- cgit v1.2.3