Skip to content

Commit b900869

Browse files
author
Nathan McMinn
committed
Added code to decrypt the PDF
1 parent 632a593 commit b900869

File tree

1 file changed

+75
-1
lines changed

1 file changed

+75
-1
lines changed

pdf-toolkit-repo/src/main/java/org/alfresco/extension/pdftoolkit/service/PDFToolkitServiceImpl.java

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,81 @@ public void encryptPDF(NodeRef targetNodeRef, Map<String, Serializable> params)
311311
@Override
312312
public void decryptPDF(NodeRef targetNodeRef, Map<String, Serializable> params)
313313
{
314-
314+
PdfStamper stamp = null;
315+
File tempDir = null;
316+
ContentWriter writer = null;
317+
ContentReader targetReader = null;
318+
319+
try
320+
{
321+
// get the parameters
322+
String ownerPassword = (String)params.get(PARAM_OWNER_PASSWORD);
323+
Boolean inplace = Boolean.valueOf(String.valueOf(params.get(PARAM_INPLACE)));
324+
325+
// get temp file
326+
File alfTempDir = TempFileProvider.getTempDir();
327+
tempDir = new File(alfTempDir.getPath() + File.separatorChar + targetNodeRef.getId());
328+
tempDir.mkdir();
329+
File file = new File(tempDir, ffs.getFileInfo(targetNodeRef).getName());
330+
331+
// get the PDF input stream and create a reader for iText
332+
targetReader = getReader(targetNodeRef);
333+
PdfReader reader = new PdfReader(targetReader.getContentInputStream(), ownerPassword.getBytes());
334+
stamp = new PdfStamper(reader, new FileOutputStream(file));
335+
stamp.close();
336+
337+
String fileName = getFilename(params, targetNodeRef);
338+
339+
// write out to destination
340+
NodeRef destinationNode = createDestinationNode(fileName,
341+
(NodeRef)params.get(PARAM_DESTINATION_FOLDER), targetNodeRef, inplace);
342+
writer = cs.getWriter(destinationNode, ContentModel.PROP_CONTENT, true);
343+
344+
writer.setEncoding(targetReader.getEncoding());
345+
writer.setMimetype(FILE_MIMETYPE);
346+
writer.putContent(file);
347+
file.delete();
348+
349+
//if useAspect is true, store some additional info about the signature in the props
350+
if(useEncryptionAspect)
351+
{
352+
ns.removeAspect(destinationNode, PDFToolkitModel.ASPECT_ENCRYPTED);
353+
}
354+
}
355+
catch (IOException e)
356+
{
357+
throw new AlfrescoRuntimeException(e.getMessage(), e);
358+
}
359+
catch (DocumentException e)
360+
{
361+
throw new AlfrescoRuntimeException(e.getMessage(), e);
362+
}
363+
finally
364+
{
365+
if (tempDir != null)
366+
{
367+
try
368+
{
369+
tempDir.delete();
370+
}
371+
catch (Exception ex)
372+
{
373+
throw new AlfrescoRuntimeException(ex.getMessage(), ex);
374+
}
375+
}
376+
377+
if (stamp != null)
378+
{
379+
try
380+
{
381+
stamp.close();
382+
}
383+
catch (Exception ex)
384+
{
385+
throw new AlfrescoRuntimeException(ex.getMessage(), ex);
386+
}
387+
}
388+
}
315389

316390
}
317391

0 commit comments

Comments
 (0)