Skip to content

Commit

Permalink
bug fixes for extract page action
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathan McMinn committed Oct 17, 2015
1 parent 220c204 commit 7db8ceb
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 94 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,17 @@ public class PDFExtractPageActionExecuter extends BasePDFActionExecuter {
@Override
protected void addParameterDefinitions(List<ParameterDefinition> paramList)
{
paramList.add(new ParameterDefinitionImpl(PDFToolkitConstants.PARAM_DESTINATION_FOLDER, DataTypeDefinition.NODE_REF, false, getParamDisplayLabel(PDFToolkitConstants.PARAM_DESTINATION_FOLDER)));
paramList.add(new ParameterDefinitionImpl(PDFToolkitConstants.PARAM_DESTINATION_FOLDER, DataTypeDefinition.NODE_REF, true, getParamDisplayLabel(PDFToolkitConstants.PARAM_DESTINATION_FOLDER)));
paramList.add(new ParameterDefinitionImpl(PDFToolkitConstants.PARAM_EXTRACT_PAGES, DataTypeDefinition.TEXT, true, getParamDisplayLabel(PDFToolkitConstants.PARAM_DELETE_PAGES)));
paramList.add(new ParameterDefinitionImpl(PDFToolkitConstants.PARAM_DESTINATION_NAME, DataTypeDefinition.TEXT, false, getParamDisplayLabel(PDFToolkitConstants.PARAM_DESTINATION_NAME)));
paramList.add(new ParameterDefinitionImpl(PDFToolkitConstants.PARAM_DESTINATION_NAME, DataTypeDefinition.TEXT, true, getParamDisplayLabel(PDFToolkitConstants.PARAM_DESTINATION_NAME)));

super.addParameterDefinitions(paramList);
}

@Override
protected void executeImpl(Action action, NodeRef actionedUponNodeRef)
{
NodeRef result = pdfToolkitService.deletePagesFromPDF(actionedUponNodeRef, action.getParameterValues());
NodeRef result = pdfToolkitService.extractPagesFromPDF(actionedUponNodeRef, action.getParameterValues());
action.setParameterValue(PARAM_RESULT, result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1125,96 +1125,6 @@ public NodeRef extractPagesFromPDF(NodeRef targetNodeRef, Map<String, Serializab
return subsetPDFDocument(targetNodeRef, params, pages, false);
}

private NodeRef subsetPDFDocument(NodeRef targetNodeRef, Map<String, Serializable> params, String pages, boolean delete)
{
InputStream is = null;
File tempDir = null;
ContentWriter writer = null;
PdfReader pdfReader = null;
NodeRef destinationNode = null;

try
{
ContentReader targetReader = getReader(targetNodeRef);
is = targetReader.getContentInputStream();

File alfTempDir = TempFileProvider.getTempDir();
tempDir = new File(alfTempDir.getPath() + File.separatorChar + targetNodeRef.getId());
tempDir.mkdir();

Boolean inplace = Boolean.valueOf(String.valueOf(params.get(PARAM_INPLACE)));

String fileName = getFilename(params, targetNodeRef);

File file = new File(tempDir, ffs.getFileInfo(targetNodeRef).getName());

pdfReader = new PdfReader(is);
Document doc = new Document(pdfReader.getPageSizeWithRotation(1));
PdfCopy copy = new PdfCopy(doc, new FileOutputStream(file));
doc.open();

List<Integer> pagelist = parsePageList(pages);

for (int pageNum = 1; pageNum <= pdfReader.getNumberOfPages(); pageNum++)
{
if (pagelist.contains(pageNum) && !delete) {
copy.addPage(copy.getImportedPage(pdfReader, pageNum));
}
}
doc.close();

destinationNode = createDestinationNode(fileName,
(NodeRef)params.get(PARAM_DESTINATION_FOLDER), targetNodeRef, inplace);
writer = cs.getWriter(destinationNode, ContentModel.PROP_CONTENT, true);

writer.setEncoding(targetReader.getEncoding());
writer.setMimetype(FILE_MIMETYPE);

// Put it in the repository
writer.putContent(file);

// Clean up
file.delete();

}
catch (IOException e)
{
throw new AlfrescoRuntimeException(e.getMessage(), e);
}
catch (DocumentException e)
{
throw new AlfrescoRuntimeException(e.getMessage(), e);
}
catch (Exception e)
{
throw new AlfrescoRuntimeException(e.getMessage(), e);
}
finally
{
if (pdfReader != null)
{
pdfReader.close();
}
if (is != null)
{
try
{
is.close();
}
catch (IOException e)
{
throw new AlfrescoRuntimeException(e.getMessage(), e);
}
}

if (tempDir != null)
{
tempDir.delete();
}
}
return destinationNode;
}

@Override
public NodeRef rotatePDF(NodeRef targetNodeRef, Map<String, Serializable> params)
{
Expand Down Expand Up @@ -1311,6 +1221,96 @@ public NodeRef rotatePDF(NodeRef targetNodeRef, Map<String, Serializable> params
return destinationNode;
}

private NodeRef subsetPDFDocument(NodeRef targetNodeRef, Map<String, Serializable> params, String pages, boolean delete)
{
InputStream is = null;
File tempDir = null;
ContentWriter writer = null;
PdfReader pdfReader = null;
NodeRef destinationNode = null;

try
{
ContentReader targetReader = getReader(targetNodeRef);
is = targetReader.getContentInputStream();

File alfTempDir = TempFileProvider.getTempDir();
tempDir = new File(alfTempDir.getPath() + File.separatorChar + targetNodeRef.getId());
tempDir.mkdir();

Boolean inplace = Boolean.valueOf(String.valueOf(params.get(PARAM_INPLACE)));

String fileName = getFilename(params, targetNodeRef);

File file = new File(tempDir, ffs.getFileInfo(targetNodeRef).getName());

pdfReader = new PdfReader(is);
Document doc = new Document(pdfReader.getPageSizeWithRotation(1));
PdfCopy copy = new PdfCopy(doc, new FileOutputStream(file));
doc.open();

List<Integer> pagelist = parsePageList(pages);

for (int pageNum = 1; pageNum <= pdfReader.getNumberOfPages(); pageNum++)
{
if (pagelist.contains(pageNum) && !delete) {
copy.addPage(copy.getImportedPage(pdfReader, pageNum));
}
}
doc.close();

destinationNode = createDestinationNode(fileName,
(NodeRef)params.get(PARAM_DESTINATION_FOLDER), targetNodeRef, inplace);
writer = cs.getWriter(destinationNode, ContentModel.PROP_CONTENT, true);

writer.setEncoding(targetReader.getEncoding());
writer.setMimetype(FILE_MIMETYPE);

// Put it in the repository
writer.putContent(file);

// Clean up
file.delete();

}
catch (IOException e)
{
throw new AlfrescoRuntimeException(e.getMessage(), e);
}
catch (DocumentException e)
{
throw new AlfrescoRuntimeException(e.getMessage(), e);
}
catch (Exception e)
{
throw new AlfrescoRuntimeException(e.getMessage(), e);
}
finally
{
if (pdfReader != null)
{
pdfReader.close();
}
if (is != null)
{
try
{
is.close();
}
catch (IOException e)
{
throw new AlfrescoRuntimeException(e.getMessage(), e);
}
}

if (tempDir != null)
{
tempDir.delete();
}
}
return destinationNode;
}

private ContentReader getReader(NodeRef nodeRef)
{
// first, make sure the node exists
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@
<field-visibility>
<show id="destination-name"/>
<show id="destination-folder"/>
<show id="delete-pages"/>
<show id="extract-pages"/>
</field-visibility>
<appearance>
</appearance>
Expand Down

0 comments on commit 7db8ceb

Please sign in to comment.