Skip to content

Commit

Permalink
Merge pull request #2676 from jlawyerorg/feat/more-pdf-stuff
Browse files Browse the repository at this point in the history
Feat/more pdf stuff
  • Loading branch information
j-dimension authored Nov 24, 2024
2 parents 347bb99 + 0599306 commit ec8b4dc
Show file tree
Hide file tree
Showing 6 changed files with 280 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -908,13 +908,12 @@ public String getStepName() {

@Override
public void display() {

List<File> pdfFiles = (List<File>) data.get("export.sortedpdffiles");

ArchiveFileBean caseDto = (ArchiveFileBean) data.get("export.case");

this.lblProgress.setText("Dokumente werden zusammengeführt...");
this.pnlPreview.removeAll();
this.pnlPreview.revalidate(); // Stellen sicher, dass das Panel wirklich leer ist

if (this.wizard != null) {
this.wizard.enableButtons(false, false, true, true);
Expand All @@ -930,26 +929,33 @@ public void display() {

File tf = new File(tempFile);
byte[] tfBytes = FileUtils.readFile(tf);
// Aktualisiere die Bytes im data Container
this.data.put("pdf.bytes", tfBytes);

SwingUtilities.invokeLater(() -> {
PdfImagePanel pdfP = new PdfImagePanel(tf.getName(), tfBytes);
// Altes Panel entfernen
this.pnlPreview.removeAll();

// Neues Panel erstellen und hinzufügen
PdfImagePanel pdfP = new PdfImagePanel(tf.getName(), tfBytes);
this.pnlPreview.add(pdfP, BorderLayout.CENTER);
this.pnlPreview.revalidate();
this.pnlPreview.doLayout();
this.pnlPreview.updateUI();

// Panel-Größe setzen
int width = Math.max(290, this.pnlPreview.getWidth());
int height = Math.max(400, this.pnlPreview.getHeight());

pdfP.setSize(new Dimension(width, height));
pdfP.setMaximumSize(new Dimension(width, height));
pdfP.setPreferredSize(new Dimension(width, height));

// Layout aktualisieren
this.pnlPreview.revalidate();
this.pnlPreview.doLayout();
this.pnlPreview.updateUI();

// PDF-Inhalt anzeigen
pdfP.showContent(tfBytes);

this.lblProgress.setText("Datei: " + tempFile);

});

} catch (Exception e) {
Expand All @@ -969,7 +975,6 @@ public void display() {
log.error(t);
}
}).start();

}

@Override
Expand All @@ -978,108 +983,142 @@ public void setData(WizardDataContainer data) {
}

private void mergePDFsWithTOC(List<File> pdfFiles, String outputFile, ArchiveFileBean caseDto) throws Exception {
// Status der Checkboxen aus data container lesen
Boolean createToc = (Boolean)data.get("export.createToc");
Boolean createPageNumbers = (Boolean)data.get("export.createPageNumbers");

if(createToc == null) createToc = true; // Fallback wenn nicht gesetzt
if(createPageNumbers == null) createPageNumbers = true; // Fallback wenn nicht gesetzt

HashMap<String, Integer> startPages = new HashMap<>();
PDFMergerUtility merger = new PDFMergerUtility();
PDDocument mergedDoc = new PDDocument();

// Create TOC
PDDocument tocDoc = new PDDocument();

// first page with metadata
PDPage metaPage = new PDPage(PDRectangle.A4);
tocDoc.addPage(metaPage);
PDPageContentStream metaStream = new PDPageContentStream(tocDoc, metaPage);
metaStream.setFont(PDType1Font.HELVETICA_BOLD, 16);
metaStream.beginText();
metaStream.setLeading(14.5f);
metaStream.newLineAtOffset(50, 750);
metaStream.showText(caseDto.getFileNumber() + " " + caseDto.getName() + " (" + new SimpleDateFormat("dd.MM.yyyy").format(new Date()) + ")");
metaStream.newLine();
metaStream.newLine();
metaStream.endText();

// Set font for additional information
metaStream.setFont(PDType1Font.HELVETICA, 12);
metaStream.beginText();
metaStream.setLeading(14.5f);
metaStream.newLineAtOffset(50, 710);

// Add additional lines of text
metaStream.showText("wegen: " + caseDto.getReason());
metaStream.newLine();
metaStream.showText("Sachgebiet: " + caseDto.getSubjectField());
metaStream.newLine();
metaStream.showText("Anwalt / Anwältin: " + caseDto.getLawyer());
metaStream.newLine();
metaStream.showText("Sachbearbeiter(-in): " + caseDto.getAssistant());
metaStream.newLine();
metaStream.showText("erstellt: " + new SimpleDateFormat("dd.MM.yyyy").format(caseDto.getDateCreated()));
metaStream.newLine();
metaStream.showText("geändert: " + new SimpleDateFormat("dd.MM.yyyy").format(caseDto.getDateChanged()));
metaStream.endText();

metaStream.close();

// add empty placeholder pages for the TOC
for (int i = 0; i < pdfFiles.size(); i += 45) {
PDPage tocPage = new PDPage(PDRectangle.A4);
tocDoc.addPage(tocPage);
PDDocument tocDoc = null;
File tocTempFile = null;

// Nur TOC erstellen wenn Option gewählt
if (createToc) {
tocDoc = new PDDocument();
// first page with metadata
PDPage metaPage = new PDPage(PDRectangle.A4);
tocDoc.addPage(metaPage);

try (PDPageContentStream metaStream = new PDPageContentStream(tocDoc, metaPage)) {
metaStream.setFont(PDType1Font.HELVETICA_BOLD, 16);
metaStream.beginText();
metaStream.setLeading(14.5f);
metaStream.newLineAtOffset(50, 750);
metaStream.showText(caseDto.getFileNumber() + " " + caseDto.getName() + " (" + new SimpleDateFormat("dd.MM.yyyy").format(new Date()) + ")");
metaStream.newLine();
metaStream.newLine();
metaStream.endText();

// Set font for additional information
metaStream.setFont(PDType1Font.HELVETICA, 12);
metaStream.beginText();
metaStream.setLeading(14.5f);
metaStream.newLineAtOffset(50, 710);

// Add additional lines of text
metaStream.showText("wegen: " + caseDto.getReason());
metaStream.newLine();
metaStream.showText("Sachgebiet: " + caseDto.getSubjectField());
metaStream.newLine();
metaStream.showText("Anwalt / Anwältin: " + caseDto.getLawyer());
metaStream.newLine();
metaStream.showText("Sachbearbeiter(-in): " + caseDto.getAssistant());
metaStream.newLine();
metaStream.showText("erstellt: " + new SimpleDateFormat("dd.MM.yyyy").format(caseDto.getDateCreated()));
metaStream.newLine();
metaStream.showText("geändert: " + new SimpleDateFormat("dd.MM.yyyy").format(caseDto.getDateChanged()));
metaStream.endText();
}

}
// add empty placeholder pages for the TOC
for (int i = 0; i < pdfFiles.size(); i += 45) {
PDPage tocPage = new PDPage(PDRectangle.A4);
tocDoc.addPage(tocPage);
}

// Save TOC to temp file
File tocTempFile = createTempFileFromPDDocument(tocDoc);
tocDoc.close();
// Save TOC to temp file and close
try {
tocTempFile = createTempFileFromPDDocument(tocDoc);
} finally {
if (tocDoc != null) {
tocDoc.close();
}
}

// TOC als erste Datei hinzufügen
pdfFiles.add(0, tocTempFile);
}

int currentPageCount = 0;
pdfFiles.add(0, tocTempFile);
for (File file : pdfFiles) {
PDDocument doc = PDDocument.load(file);
startPages.put(file.getName(), currentPageCount);
merger.appendDocument(mergedDoc, doc);
currentPageCount += doc.getNumberOfPages();
doc.close();
PDDocument doc = null;
try {
doc = PDDocument.load(file);
startPages.put(file.getName(), currentPageCount);
merger.appendDocument(mergedDoc, doc);
currentPageCount += doc.getNumberOfPages();
} finally {
if (doc != null) {
doc.close();
}
}
}

// Dokumentinformationen setzen
PDDocumentInformation info = new PDDocumentInformation();
info.setTitle(caseDto.getFileNumber() + " " + caseDto.getName() + " (" + new SimpleDateFormat("dd.MM.yyyy").format(new Date()) + ")");
info.setAuthor(UserSettings.getInstance().getCurrentUser().getPrincipalId());
info.setCreationDate(GregorianCalendar.getInstance());
mergedDoc.setDocumentInformation(info);

PDDocumentOutline outline = new PDDocumentOutline();
mergedDoc.getDocumentCatalog().setDocumentOutline(outline);

int currentTocPage = 1;
// remove TOC document to avoid it from being added to the TOC itself
pdfFiles.remove(0);
for (int i = 0; i < pdfFiles.size(); i += 45) {
int end = Math.min(pdfFiles.size(), i + 45);
List<File> batch = pdfFiles.subList(i, end);
processTocPage(currentTocPage, mergedDoc, batch, startPages, caseDto, outline);
currentTocPage = currentTocPage + 1;
// Nur TOC-Einträge erstellen wenn Option gewählt
if (createToc) {
PDDocumentOutline outline = new PDDocumentOutline();
mergedDoc.getDocumentCatalog().setDocumentOutline(outline);

int currentTocPage = 1;
// remove TOC document to avoid it from being added to the TOC itself
pdfFiles.remove(0);
for (int i = 0; i < pdfFiles.size(); i += 45) {
int end = Math.min(pdfFiles.size(), i + 45);
List<File> batch = pdfFiles.subList(i, end);
processTocPage(currentTocPage, mergedDoc, batch, startPages, caseDto, outline);
currentTocPage = currentTocPage + 1;
}
}

int pageCount = mergedDoc.getNumberOfPages();
for (int i = 0; i < pageCount; i++) {
PDPage page = mergedDoc.getPage(i);
try (PDPageContentStream contentStream = new PDPageContentStream(mergedDoc, page, PDPageContentStream.AppendMode.APPEND, true, true)) {
contentStream.beginText();
contentStream.setFont(PDType1Font.HELVETICA, 10);

// Position the text at the bottom center of the page
float margin = 20;
float x = (page.getMediaBox().getWidth() - margin) / 2;
float y = margin;

contentStream.newLineAtOffset(x, y);
contentStream.showText("" + (i + 1) + " / " + pageCount);
contentStream.endText();
// Nur Seitenzahlen hinzufügen wenn Option gewählt
if (createPageNumbers) {
int pageCount = mergedDoc.getNumberOfPages();
for (int i = 0; i < pageCount; i++) {
PDPage page = mergedDoc.getPage(i);
try (PDPageContentStream contentStream = new PDPageContentStream(mergedDoc, page, PDPageContentStream.AppendMode.APPEND, true, true)) {
contentStream.beginText();
contentStream.setFont(PDType1Font.HELVETICA, 10);

float margin = 20;
float x = (page.getMediaBox().getWidth() - margin) / 2;
float y = margin;

contentStream.newLineAtOffset(x, y);
contentStream.showText("" + (i + 1) + " / " + pageCount);
contentStream.endText();
}
}
}

FileUtils.copyFile(createTempFileFromPDDocument(mergedDoc).getAbsolutePath(), outputFile, true);

mergedDoc.close();

// Cleanup
if (tocTempFile != null && tocTempFile.exists()) {
tocTempFile.delete();
}
}

private void processTocPage(int currentTocPage, PDDocument mergedDoc, List<File> pdfFiles, HashMap<String, Integer> startPages, ArchiveFileBean caseDto, PDDocumentOutline outline) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,14 @@
<Group type="102" attributes="0">
<EmptySpace max="-2" attributes="0"/>
<Group type="103" groupAlignment="0" attributes="0">
<Component id="jLabel1" alignment="1" pref="876" max="32767" attributes="0"/>
<Component id="jLabel1" alignment="1" pref="863" max="32767" attributes="0"/>
<Component id="jScrollPane1" alignment="0" max="32767" attributes="0"/>
<Group type="102" alignment="1" attributes="0">
<EmptySpace min="0" pref="0" max="32767" attributes="0"/>
<Component id="addPageNbrsCb" min="-2" max="-2" attributes="0"/>
<EmptySpace max="-2" attributes="0"/>
<Component id="addTocCb" min="-2" max="-2" attributes="0"/>
<EmptySpace type="unrelated" max="-2" attributes="0"/>
<Component id="jLabel2" min="-2" max="-2" attributes="0"/>
<EmptySpace max="-2" attributes="0"/>
<Component id="lblPages" min="-2" max="-2" attributes="0"/>
Expand All @@ -50,9 +54,11 @@
<Component id="lblFileSizeMax" alignment="3" min="-2" max="-2" attributes="0"/>
<Component id="lblPages" alignment="3" min="-2" max="-2" attributes="0"/>
<Component id="jLabel2" alignment="3" min="-2" max="-2" attributes="0"/>
<Component id="addTocCb" alignment="3" min="-2" max="-2" attributes="0"/>
<Component id="addPageNbrsCb" alignment="3" min="-2" max="-2" attributes="0"/>
</Group>
<EmptySpace max="-2" attributes="0"/>
<Component id="jScrollPane1" pref="505" max="32767" attributes="0"/>
<EmptySpace min="-2" pref="47" max="-2" attributes="0"/>
<Component id="jScrollPane1" pref="460" max="32767" attributes="0"/>
<EmptySpace max="-2" attributes="0"/>
</Group>
</Group>
Expand Down Expand Up @@ -133,5 +139,24 @@
<Property name="text" type="java.lang.String" value="Seiten:"/>
</Properties>
</Component>
<Component class="javax.swing.JCheckBox" name="addTocCb">
<Properties>
<Property name="selected" type="boolean" value="true"/>
<Property name="text" type="java.lang.String" value="Inhaltsverzeichnis"/>
<Property name="toolTipText" type="java.lang.String" value="Inhaltsverzeichnis wird hinzugef&#xfc;gt"/>
<Property name="actionCommand" type="java.lang.String" value=""/>
</Properties>
</Component>
<Component class="javax.swing.JCheckBox" name="addPageNbrsCb">
<Properties>
<Property name="selected" type="boolean" value="true"/>
<Property name="text" type="java.lang.String" value="Seitenzahlen"/>
<Property name="toolTipText" type="java.lang.String" value="Seitenzahlen werden hinzugef&#xfc;gt"/>
<Property name="actionCommand" type="java.lang.String" value=""/>
</Properties>
<Events>
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="addPageNbrsCbActionPerformed"/>
</Events>
</Component>
</SubComponents>
</Form>
Loading

0 comments on commit ec8b4dc

Please sign in to comment.