Skip to content

Commit

Permalink
Merge pull request #2587 from jlawyerorg/feat/delete-single-page-in-pdf
Browse files Browse the repository at this point in the history
added button to delete single pdf pages in documentviewer
  • Loading branch information
j-dimension authored Sep 29, 2024
2 parents e618fa1 + 6a20643 commit 80f2af7
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,7 @@
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
Expand Down Expand Up @@ -1535,5 +1536,42 @@ private static BufferedImage rotateImage(BufferedImage originalImage, double deg

return rotatedImage;
}

public void removePages(int[] pageIndexes) {

try {
PDDocument inputPDF = PDDocument.load(new ByteArrayInputStream(content));

// Remove pages from highest index to lowest to avoid index shifting
Arrays.sort(pageIndexes);
for (int i = pageIndexes.length - 1; i >= 0; i--) {
int pageNumber = pageIndexes[i];
inputPDF.removePage(pageNumber);
}

ByteArrayOutputStream bout = new ByteArrayOutputStream();
inputPDF.save(bout);
inputPDF.close();
this.content = bout.toByteArray();
if (this.saveCallback != null) {
this.saveCallback.savePreview(documentId, fileName, content);
}

} catch (Exception ex) {
log.error("Could not remove PDF pages", ex);
JOptionPane.showMessageDialog(this, "Fehler beim Aktualisieren des PDFs: " + ex.getMessage(), com.jdimension.jlawyer.client.utils.DesktopUtils.POPUP_TITLE_ERROR, JOptionPane.ERROR_MESSAGE);
return;
}

// Clear and re-render the content
this.orgImage.clear();
this.pnlPages.removeAll();
this.renderedPages = 0;
this.totalPages = 0;
this.currentPage = 0;
this.renderContent(0, 0, MAX_RENDER_PAGES - 1, this.zoomFactor);

}


}
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@
<Group type="102" attributes="0">
<EmptySpace max="-2" attributes="0"/>
<Group type="103" groupAlignment="0" attributes="0">
<Component id="cmdRotateLeft" alignment="0" min="-2" max="-2" attributes="0"/>
<Component id="cmdRotateRight" alignment="0" min="-2" max="-2" attributes="0"/>
<Component id="lblPageNumber" alignment="1" min="-2" max="-2" attributes="0"/>
<Group type="103" groupAlignment="0" attributes="0">
<Component id="cmdRotateLeft" alignment="0" min="-2" max="-2" attributes="0"/>
<Component id="cmdRotateRight" alignment="0" min="-2" max="-2" attributes="0"/>
<Component id="lblPageNumber" alignment="1" min="-2" max="-2" attributes="0"/>
</Group>
<Component id="cmdDeletePage" alignment="0" min="-2" max="-2" attributes="0"/>
</Group>
<EmptySpace max="-2" attributes="0"/>
<Component id="lblPage" min="-2" max="-2" attributes="0"/>
Expand All @@ -41,7 +44,9 @@
<Component id="cmdRotateLeft" min="-2" max="-2" attributes="0"/>
<EmptySpace max="-2" attributes="0"/>
<Component id="cmdRotateRight" min="-2" max="-2" attributes="0"/>
<EmptySpace pref="82" max="32767" attributes="0"/>
<EmptySpace max="-2" attributes="0"/>
<Component id="cmdDeletePage" min="-2" max="-2" attributes="0"/>
<EmptySpace pref="50" max="32767" attributes="0"/>
</Group>
</Group>
</Group>
Expand Down Expand Up @@ -81,6 +86,17 @@
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="cmdRotateRightActionPerformed"/>
</Events>
</Component>
<Component class="javax.swing.JButton" name="cmdDeletePage">
<Properties>
<Property name="icon" type="javax.swing.Icon" editor="org.netbeans.modules.form.editors2.IconEditor">
<Image iconType="3" name="/icons16/material/outline_backspace_black_48dp.png"/>
</Property>
<Property name="toolTipText" type="java.lang.String" value="Ausgew&#xe4;hlte Seite entfernen"/>
</Properties>
<Events>
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="cmdDeletePageActionPerformed"/>
</Events>
</Component>
<Component class="javax.swing.JLabel" name="lblPageNumber">
<Properties>
<Property name="font" type="java.awt.Font" editor="org.netbeans.modules.form.editors2.FontEditor">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,7 @@ You should also get your employer (if you work as a programmer) or school,

import java.awt.Color;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.SwingConstants;
import javax.swing.border.LineBorder;
import themes.colors.DefaultColorTheme;
Expand Down Expand Up @@ -726,6 +727,7 @@ private void initComponents() {
lblPage = new javax.swing.JLabel();
cmdRotateLeft = new javax.swing.JButton();
cmdRotateRight = new javax.swing.JButton();
cmdDeletePage = new javax.swing.JButton();
lblPageNumber = new javax.swing.JLabel();

lblPage.setText("jLabel1");
Expand Down Expand Up @@ -755,6 +757,14 @@ public void actionPerformed(java.awt.event.ActionEvent evt) {
}
});

cmdDeletePage.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons16/material/outline_backspace_black_48dp.png"))); // NOI18N
cmdDeletePage.setToolTipText("Ausgewählte Seite entfernen");
cmdDeletePage.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cmdDeletePageActionPerformed(evt);
}
});

lblPageNumber.setFont(lblPageNumber.getFont().deriveFont(lblPageNumber.getFont().getStyle() | java.awt.Font.BOLD));
lblPageNumber.setForeground(new java.awt.Color(255, 255, 255));
lblPageNumber.setText("1");
Expand All @@ -766,9 +776,11 @@ public void actionPerformed(java.awt.event.ActionEvent evt) {
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(cmdRotateLeft)
.addComponent(cmdRotateRight)
.addComponent(lblPageNumber, javax.swing.GroupLayout.Alignment.TRAILING))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(cmdRotateLeft)
.addComponent(cmdRotateRight)
.addComponent(lblPageNumber, javax.swing.GroupLayout.Alignment.TRAILING))
.addComponent(cmdDeletePage))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(lblPage)
.addContainerGap(38, Short.MAX_VALUE))
Expand All @@ -785,7 +797,9 @@ public void actionPerformed(java.awt.event.ActionEvent evt) {
.addComponent(cmdRotateLeft)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(cmdRotateRight)
.addContainerGap(82, Short.MAX_VALUE))))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(cmdDeletePage)
.addContainerGap(50, Short.MAX_VALUE))))
);
}// </editor-fold>//GEN-END:initComponents

Expand All @@ -805,8 +819,16 @@ private void lblPageMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:even
this.highlightPage(false);
}//GEN-LAST:event_lblPageMouseExited

private void cmdDeletePageActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdDeletePageActionPerformed
int response = JOptionPane.showConfirmDialog(this, "Soll die Seite wirklich entfernt werden?", "Bestätigung", JOptionPane.YES_NO_OPTION);
if (response == JOptionPane.YES_OPTION) {
this.documentContainer.removePages(new int[]{this.pageIndex});
}
}//GEN-LAST:event_cmdDeletePageActionPerformed


// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton cmdDeletePage;
private javax.swing.JButton cmdRotateLeft;
private javax.swing.JButton cmdRotateRight;
private javax.swing.JLabel lblPage;
Expand Down

0 comments on commit 80f2af7

Please sign in to comment.