Added option to split PDF into multiple parts and merge into one PDF (#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>
This commit is contained in:
懒猫 2024-02-25 04:26:35 +08:00 committed by GitHub
parent af68c70239
commit c8a37245fa
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
32 changed files with 52 additions and 34 deletions

View file

@ -6,7 +6,8 @@
"parameters": {
"horizontalDivisions": 2,
"verticalDivisions": 2,
"fileInput": "automated"
"fileInput": "automated",
"merge": false
}
},
{

View file

@ -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()) {

View file

@ -53,8 +53,21 @@ public class SplitPdfBySectionsController {
// 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);
@ -65,9 +78,6 @@ public class SplitPdfBySectionsController {
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))) {
@ -145,4 +155,5 @@ public class SplitPdfBySectionsController {
return splitDocuments;
}
}

View file

@ -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;
}

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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=ライセンス

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -23,6 +23,9 @@
<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() {