Skip to content

Commit

Permalink
Added option to split PDF into multiple parts and merge into one PDF (#…
Browse files Browse the repository at this point in the history
…841)

* Added option to split PDF into multiple parts and merge into one PDF

* Use the mergeDocuments method in MergeController to implement merging

---------

Co-authored-by: Eric <71648843+sbplat@users.noreply.github.com>
  • Loading branch information
lm93129 and sbplat committed Feb 24, 2024
1 parent af68c70 commit c8a3724
Show file tree
Hide file tree
Showing 32 changed files with 52 additions and 34 deletions.
5 changes: 3 additions & 2 deletions pipeline/defaultWebUIConfigs/split-rotate-auto-rename.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"parameters": {
"horizontalDivisions": 2,
"verticalDivisions": 2,
"fileInput": "automated"
"fileInput": "automated",
"merge": false
}
},
{
Expand All @@ -30,4 +31,4 @@
},
"outputDir": "{outputFolder}",
"outputFileName": "{filename}"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class MergeController {

private static final Logger logger = LoggerFactory.getLogger(MergeController.class);

private PDDocument mergeDocuments(List<PDDocument> documents) throws IOException {
public PDDocument mergeDocuments(List<PDDocument> documents) throws IOException {
PDDocument mergedDoc = new PDDocument();
for (PDDocument doc : documents) {
for (PDPage page : doc.getPages()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,21 @@ public ResponseEntity<byte[]> splitPdf(@ModelAttribute SplitPdfBySectionsRequest
// Process the PDF based on split parameters
int horiz = request.getHorizontalDivisions() + 1;
int verti = request.getVerticalDivisions() + 1;

boolean merge = request.isMerge();
List<PDDocument> splitDocuments = splitPdfPages(sourceDocument, verti, horiz);

String filename =
Filenames.toSimpleFileName(file.getOriginalFilename())
.replaceFirst("[.][^.]+$", "");
if (merge) {
MergeController mergeController = new MergeController();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
mergeController.mergeDocuments(splitDocuments).save(baos);
return WebResponseUtils.bytesToWebResponse(
baos.toByteArray(),
filename + "_split.pdf",
MediaType.APPLICATION_OCTET_STREAM);
}
for (PDDocument doc : splitDocuments) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
doc.save(baos);
Expand All @@ -65,9 +78,6 @@ public ResponseEntity<byte[]> splitPdf(@ModelAttribute SplitPdfBySectionsRequest
sourceDocument.close();

Path zipFile = Files.createTempFile("split_documents", ".zip");
String filename =
Filenames.toSimpleFileName(file.getOriginalFilename())
.replaceFirst("[.][^.]+$", "");
byte[] data;

try (ZipOutputStream zipOut = new ZipOutputStream(Files.newOutputStream(zipFile))) {
Expand Down Expand Up @@ -145,4 +155,5 @@ public List<PDDocument> splitPdfPages(

return splitDocuments;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,7 @@ public class SplitPdfBySectionsRequest extends PDFFile {

@Schema(description = "Number of vertical divisions for each PDF page", example = "2")
private int verticalDivisions;

@Schema(description = "Merge the split documents into a single PDF", example = "true")
private boolean merge;
}
2 changes: 1 addition & 1 deletion src/main/resources/messages_ar_AR.properties
Original file line number Diff line number Diff line change
Expand Up @@ -960,7 +960,7 @@ split-by-sections.vertical.label=Vertical Divisions
split-by-sections.horizontal.placeholder=Enter number of horizontal divisions
split-by-sections.vertical.placeholder=Enter number of vertical divisions
split-by-sections.submit=Split PDF

split-by-sections.merge=Merge Into One PDF

#licenses
licenses.nav=Licenses
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/messages_bg_BG.properties
Original file line number Diff line number Diff line change
Expand Up @@ -960,7 +960,7 @@ split-by-sections.vertical.label=Vertical Divisions
split-by-sections.horizontal.placeholder=Enter number of horizontal divisions
split-by-sections.vertical.placeholder=Enter number of vertical divisions
split-by-sections.submit=Split PDF

split-by-sections.merge=Merge Into One PDF

#licenses
licenses.nav=Licenses
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/messages_ca_CA.properties
Original file line number Diff line number Diff line change
Expand Up @@ -960,7 +960,7 @@ split-by-sections.vertical.label=Vertical Divisions
split-by-sections.horizontal.placeholder=Enter number of horizontal divisions
split-by-sections.vertical.placeholder=Enter number of vertical divisions
split-by-sections.submit=Split PDF

split-by-sections.merge=Merge Into One PDF

#licenses
licenses.nav=Licenses
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/messages_de_DE.properties
Original file line number Diff line number Diff line change
Expand Up @@ -960,7 +960,7 @@ split-by-sections.vertical.label=Vertikale Teiler
split-by-sections.horizontal.placeholder=Anzahl horizontaler Teiler eingeben
split-by-sections.vertical.placeholder=Anzahl vertikaler Teiler eingeben
split-by-sections.submit=PDF teilen

split-by-sections.merge=Merge Into One PDF

#licenses
licenses.nav=Lizenzen
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/messages_el_GR.properties
Original file line number Diff line number Diff line change
Expand Up @@ -960,7 +960,7 @@ split-by-sections.vertical.label=Vertical Divisions
split-by-sections.horizontal.placeholder=Enter number of horizontal divisions
split-by-sections.vertical.placeholder=Enter number of vertical divisions
split-by-sections.submit=Split PDF

split-by-sections.merge=Merge Into One PDF

#licenses
licenses.nav=Licenses
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/messages_en_GB.properties
Original file line number Diff line number Diff line change
Expand Up @@ -960,7 +960,7 @@ split-by-sections.vertical.label=Vertical Divisions
split-by-sections.horizontal.placeholder=Enter number of horizontal divisions
split-by-sections.vertical.placeholder=Enter number of vertical divisions
split-by-sections.submit=Split PDF

split-by-sections.merge=Merge Into One PDF

#licenses
licenses.nav=Licenses
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/messages_en_US.properties
Original file line number Diff line number Diff line change
Expand Up @@ -960,7 +960,7 @@ split-by-sections.vertical.label=Vertical Divisions
split-by-sections.horizontal.placeholder=Enter number of horizontal divisions
split-by-sections.vertical.placeholder=Enter number of vertical divisions
split-by-sections.submit=Split PDF

split-by-sections.merge=Merge Into One PDF

#licenses
licenses.nav=Licenses
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/messages_es_ES.properties
Original file line number Diff line number Diff line change
Expand Up @@ -960,7 +960,7 @@ split-by-sections.vertical.label=Divisiones Verticales
split-by-sections.horizontal.placeholder=Introduzca el número de divisiones horizontales
split-by-sections.vertical.placeholder=Introduzca el número de divisiones verticales
split-by-sections.submit=Dividir PDF

split-by-sections.merge=Merge Into One PDF

#licenses
licenses.nav=Licencias
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/messages_eu_ES.properties
Original file line number Diff line number Diff line change
Expand Up @@ -960,7 +960,7 @@ split-by-sections.vertical.label=Vertical Divisions
split-by-sections.horizontal.placeholder=Enter number of horizontal divisions
split-by-sections.vertical.placeholder=Enter number of vertical divisions
split-by-sections.submit=Split PDF

split-by-sections.merge=Merge Into One PDF

#licenses
licenses.nav=Licenses
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/messages_fr_FR.properties
Original file line number Diff line number Diff line change
Expand Up @@ -960,7 +960,7 @@ split-by-sections.vertical.label=Divisions verticales
split-by-sections.horizontal.placeholder=Saisir le nombre de divisions horizontales
split-by-sections.vertical.placeholder=Entrer le nombre de divisions verticales
split-by-sections.submit=Diviser le PDF

split-by-sections.merge=Merge Into One PDF

#licenses
licenses.nav=Licences
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/messages_hi_IN.properties
Original file line number Diff line number Diff line change
Expand Up @@ -960,7 +960,7 @@ split-by-sections.vertical.label=लंबवत विभाजन
split-by-sections.horizontal.placeholder=क्षैतिज विभाजन की संख्या दर्ज करें
split-by-sections.vertical.placeholder=लंबवत विभाजन की संख्या दर्ज करें
split-by-sections.submit=PDF को विभाजित करें

split-by-sections.merge=Merge Into One PDF

#licenses
licenses.nav=Licenses
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/messages_hu_HU.properties
Original file line number Diff line number Diff line change
Expand Up @@ -960,7 +960,7 @@ split-by-sections.vertical.label=Vízszintes szakaszok
split-by-sections.horizontal.placeholder=Adja meg a vízszintes szakaszok számát
split-by-sections.vertical.placeholder=Adja meg a függőleges szakaszok számát
split-by-sections.submit=Felosztás

split-by-sections.merge=Merge Into One PDF

#licenses
licenses.nav=Licenses
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/messages_id_ID.properties
Original file line number Diff line number Diff line change
Expand Up @@ -960,7 +960,7 @@ split-by-sections.vertical.label=Pembagian Vertikal
split-by-sections.horizontal.placeholder=Input angka untuk pembagian horizontal
split-by-sections.vertical.placeholder=Input angka untuk pembagian vertikal
split-by-sections.submit=Pisahkan PDF

split-by-sections.merge=Merge Into One PDF

#licenses
licenses.nav=Licenses
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/messages_it_IT.properties
Original file line number Diff line number Diff line change
Expand Up @@ -960,7 +960,7 @@ split-by-sections.vertical.label=Divisioni verticali
split-by-sections.horizontal.placeholder=Inserire il numero di divisioni orizzontali
split-by-sections.vertical.placeholder=Inserire il numero di divisioni verticali
split-by-sections.submit=Dividi PDF

split-by-sections.merge=Merge Into One PDF

#licenses
licenses.nav=Licenze
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/messages_ja_JP.properties
Original file line number Diff line number Diff line change
Expand Up @@ -960,7 +960,7 @@ split-by-sections.vertical.label=垂直方向
split-by-sections.horizontal.placeholder=水平方向の分割数を選択
split-by-sections.vertical.placeholder=垂直方向の分割数を選択
split-by-sections.submit=分割

split-by-sections.merge=1 つの PDF に結合するかどうか

#licenses
licenses.nav=ライセンス
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/messages_ko_KR.properties
Original file line number Diff line number Diff line change
Expand Up @@ -960,7 +960,7 @@ split-by-sections.vertical.label=Vertical Divisions
split-by-sections.horizontal.placeholder=Enter number of horizontal divisions
split-by-sections.vertical.placeholder=Enter number of vertical divisions
split-by-sections.submit=Split PDF

split-by-sections.merge=Merge Into One PDF

#licenses
licenses.nav=Licenses
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/messages_nl_NL.properties
Original file line number Diff line number Diff line change
Expand Up @@ -960,7 +960,7 @@ split-by-sections.vertical.label=Verticale secties
split-by-sections.horizontal.placeholder=Voer het aantal horizontale secties in
split-by-sections.vertical.placeholder=Voer het aantal verticale secties in
split-by-sections.submit=PDF splitsen

split-by-sections.merge=Merge Into One PDF

#licenses
licenses.nav=Licenties
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/messages_pl_PL.properties
Original file line number Diff line number Diff line change
Expand Up @@ -960,7 +960,7 @@ split-by-sections.vertical.label=Vertical Divisions
split-by-sections.horizontal.placeholder=Enter number of horizontal divisions
split-by-sections.vertical.placeholder=Enter number of vertical divisions
split-by-sections.submit=Split PDF

split-by-sections.merge=Merge Into One PDF

#licenses
licenses.nav=Licenses
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/messages_pt_BR.properties
Original file line number Diff line number Diff line change
Expand Up @@ -960,7 +960,7 @@ split-by-sections.vertical.label=Vertical Divisions
split-by-sections.horizontal.placeholder=Enter number of horizontal divisions
split-by-sections.vertical.placeholder=Enter number of vertical divisions
split-by-sections.submit=Split PDF

split-by-sections.merge=Merge Into One PDF

#licenses
licenses.nav=Licenses
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/messages_pt_PT.properties
Original file line number Diff line number Diff line change
Expand Up @@ -958,7 +958,7 @@ split-by-sections.vertical.label=Divisões Verticais
split-by-sections.horizontal.placeholder=Introduza o número de divisões horizontais
split-by-sections.vertical.placeholder=Introduza o número de divisões verticais
split-by-sections.submit=Dividir PDF

split-by-sections.merge=Merge Into One PDF

#licenses
licenses.nav=Licenças
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/messages_ro_RO.properties
Original file line number Diff line number Diff line change
Expand Up @@ -960,7 +960,7 @@ split-by-sections.vertical.label=Vertical Divisions
split-by-sections.horizontal.placeholder=Enter number of horizontal divisions
split-by-sections.vertical.placeholder=Enter number of vertical divisions
split-by-sections.submit=Split PDF

split-by-sections.merge=Merge Into One PDF

#licenses
licenses.nav=Licenses
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/messages_ru_RU.properties
Original file line number Diff line number Diff line change
Expand Up @@ -960,7 +960,7 @@ split-by-sections.vertical.label=Vertical Divisions
split-by-sections.horizontal.placeholder=Enter number of horizontal divisions
split-by-sections.vertical.placeholder=Enter number of vertical divisions
split-by-sections.submit=Split PDF

split-by-sections.merge=Merge Into One PDF

#licenses
licenses.nav=Licenses
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/messages_sr_LATN_RS.properties
Original file line number Diff line number Diff line change
Expand Up @@ -960,7 +960,7 @@ split-by-sections.vertical.label=Vertikalne podele
split-by-sections.horizontal.placeholder=Unesite broj horizontalnih podele
split-by-sections.vertical.placeholder=Unesite broj vertikalnih podele
split-by-sections.submit=Razdvoji PDF

split-by-sections.merge=Merge Into One PDF

#licenses
licenses.nav=Licenses
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/messages_sv_SE.properties
Original file line number Diff line number Diff line change
Expand Up @@ -960,7 +960,7 @@ split-by-sections.vertical.label=Vertical Divisions
split-by-sections.horizontal.placeholder=Enter number of horizontal divisions
split-by-sections.vertical.placeholder=Enter number of vertical divisions
split-by-sections.submit=Split PDF

split-by-sections.merge=Merge Into One PDF

#licenses
licenses.nav=Licenses
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/messages_tr_TR.properties
Original file line number Diff line number Diff line change
Expand Up @@ -960,7 +960,7 @@ split-by-sections.vertical.label=Vertical Divisions
split-by-sections.horizontal.placeholder=Enter number of horizontal divisions
split-by-sections.vertical.placeholder=Enter number of vertical divisions
split-by-sections.submit=Split PDF

split-by-sections.merge=Merge Into One PDF

#licenses
licenses.nav=Licenses
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/messages_zh_CN.properties
Original file line number Diff line number Diff line change
Expand Up @@ -960,7 +960,7 @@ split-by-sections.vertical.label=垂直分割
split-by-sections.horizontal.placeholder=输入水平分割数
split-by-sections.vertical.placeholder=输入垂直分割数
split-by-sections.submit=分割PDF

split-by-sections.merge=是否合并为一个pdf

#licenses
licenses.nav=Licenses
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/messages_zh_TW.properties
Original file line number Diff line number Diff line change
Expand Up @@ -960,7 +960,7 @@ split-by-sections.vertical.label=垂直劃分
split-by-sections.horizontal.placeholder=輸入水平劃分的數量
split-by-sections.vertical.placeholder=輸入垂直劃分的數量
split-by-sections.submit=分割 PDF

split-by-sections.merge=是否合併為一個pdf

#licenses
licenses.nav=Licenses
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/templates/split-pdf-by-sections.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ <h2 th:text="#{split-by-sections.header}"></h2>
<label for="verticalDivisions" th:text="#{split-by-sections.vertical.label}">Vertical Divisions</label>
<input type="number" id="verticalDivisions" name="verticalDivisions" class="form-control" min="0" max="300" required value="1" th:placeholder="#{split-by-sections.vertical.placeholder}">
<br>
<label for="verticalDivisions" th:text="#{split-by-sections.merge}">merge PDFs into one</label>
<input type="checkbox" id="merge" name="merge" th:placeholder="#{split-by-sections.merge}">
<br>
<div id="pdfVisualAid" class="pdf-visual-aid"></div>
<script>
function updateVisualAid() {
Expand Down

0 comments on commit c8a3724

Please sign in to comment.