Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Format page numbering in split filename according to localized string #43

Merged
merged 1 commit into from
Oct 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -186,4 +186,7 @@ pdf-watermark.page.display-label=Apply to page(s)

pdftoolkit.form.pagescheme.odd=odd
pdftoolkit.form.pagescheme.even=even
pdftoolkit.form.pagescheme.all=all
pdftoolkit.form.pagescheme.all=all

pdftoolkit.split-page-numbering-pattern-multiple=_pgs{0}-{1}
pdftoolkit.split-page-numbering-pattern-single=_pg{0}
Original file line number Diff line number Diff line change
Expand Up @@ -186,4 +186,7 @@ pdf-watermark.page.display-label=Auf alle Seiten anwenden

pdftoolkit.form.pagescheme.odd=ungerade
pdftoolkit.form.pagescheme.even=gerade
pdftoolkit.form.pagescheme.all=alle
pdftoolkit.form.pagescheme.all=alle

pdftoolkit.split-page-numbering-pattern-multiple=_pgs{0}-{1}
pdftoolkit.split-page-numbering-pattern-single=_pg{0}
Original file line number Diff line number Diff line change
Expand Up @@ -186,4 +186,7 @@ pdf-watermark.page.display-label=Apply to page(s)

pdftoolkit.form.pagescheme.odd=odd
pdftoolkit.form.pagescheme.even=even
pdftoolkit.form.pagescheme.all=all
pdftoolkit.form.pagescheme.all=all

pdftoolkit.split-page-numbering-pattern-multiple=_pgs{0}-{1}
pdftoolkit.split-page-numbering-pattern-single=_pg{0}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.util.PDFMergerUtility;
import org.apache.pdfbox.util.Splitter;
import org.springframework.extensions.surf.util.I18NUtil;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
Expand All @@ -72,7 +73,9 @@

public class PDFToolkitServiceImpl extends PDFToolkitConstants implements PDFToolkitService
{

private final static String MSGID_PAGE_NUMBERING_PATTERN_MULTIPLE="pdftoolkit.split-page-numbering-pattern-multiple";
private final static String MSGID_PAGE_NUMBERING_PATTERN_SINGLE="pdftoolkit.split-page-numbering-pattern-single";

private ServiceRegistry serviceRegistry;
private NodeService ns;
private ContentService cs;
Expand Down Expand Up @@ -680,10 +683,6 @@ public NodeRef splitPDF(NodeRef targetNodeRef, Map<String, Serializable> params)

while (it.hasNext())
{
// Pulling together the right string split pages
String pagePlus = "";
String pg = "_pg";

// Get the split document and save it into the temp dir with new
// name
PDDocument splitpdf = (PDDocument)it.next();
Expand All @@ -693,15 +692,11 @@ public NodeRef splitPDF(NodeRef targetNodeRef, Map<String, Serializable> params)
if (splitFrequency > 0)
{
endPage = endPage + pagesInPDF;

pagePlus = "-" + endPage;
pg = "_pgs";

}

// put together the name and save the PDF
String fileNameSansExt = getFilenameSansExt(targetNodeRef, FILE_EXTENSION);
splitpdf.save(tempDir + "" + File.separatorChar + fileNameSansExt + pg + page + pagePlus + FILE_EXTENSION);
splitpdf.save(tempDir + "" + File.separatorChar + fileNameSansExt + formatPageNumbering(page, endPage) + FILE_EXTENSION);

// increment page count
if (splitFrequency > 0)
Expand Down Expand Up @@ -853,17 +848,15 @@ public NodeRef splitPDFAtPage(NodeRef targetNodeRef, Map<String, Serializable> p

int pagesInFirstPDF = firstPDF.getNumberOfPages();

String lastPage = "";
String pg = "_pg";
int lastPage = 0;

if (pagesInFirstPDF > 1)
{
pg = "_pgs";
lastPage = "-" + pagesInFirstPDF;
lastPage = pagesInFirstPDF;
}

String fileNameSansExt = getFilenameSansExt(targetNodeRef, FILE_EXTENSION);
firstPDF.save(tempDir + "" + File.separatorChar + fileNameSansExt + pg + page + lastPage + FILE_EXTENSION);
firstPDF.save(tempDir + "" + File.separatorChar + fileNameSansExt + formatPageNumbering(page, lastPage) + FILE_EXTENSION);

try
{
Expand Down Expand Up @@ -914,18 +907,16 @@ public NodeRef splitPDFAtPage(NodeRef targetNodeRef, Map<String, Serializable> p

if (pagesInSecondPDF > 1)
{
pg = "_pgs";
lastPage = "-" + (pagesInSecondPDF + pagesInFirstPDF);
lastPage = pagesInSecondPDF + pagesInFirstPDF;
}
else
{
pg = "_pg";
lastPage = "";
lastPage = 0;
}

// This is where we should save the appended PDF
// put together the name and save the PDF
secondPDF.save(tempDir + "" + File.separatorChar + fileNameSansExt + pg + splitPageNumber + lastPage + FILE_EXTENSION);
secondPDF.save(tempDir + "" + File.separatorChar + fileNameSansExt + formatPageNumbering(splitPageNumber, lastPage) + FILE_EXTENSION);

for (File file : tempDir.listFiles())
{
Expand Down Expand Up @@ -2097,6 +2088,27 @@ private float getCenterY(Rectangle r, Image img)

return y;
}

/**
* Format the page numbers according to the localized string in messages
*
* @param currentPage
* @param lastPage
* @return
*/
private String formatPageNumbering(int currentPage, int lastPage)
{
String text = "";
if (lastPage==0)
{
text = I18NUtil.getMessage(MSGID_PAGE_NUMBERING_PATTERN_SINGLE, new Object[]{currentPage});
}
else
{
text = I18NUtil.getMessage(MSGID_PAGE_NUMBERING_PATTERN_MULTIPLE, new Object[]{currentPage, lastPage});
}
return text;
}

/**
* @param serviceRegistry
Expand Down